SPARQL/SERVICE - around and box
The service allows to search for items with coordinates located within a certain radius of the center or within a certain bounding box.
Most of the examples below use a map view by using #defaultView:Map
at the top of the query. You may switch to table display to see the underlying data.
The property most commonly used for locations is coordinate location (P625).
Search around point
editExample Airports within 100km from Berlin:
# Airports within 100km from Berlin
#defaultView:Map
SELECT ?place ?placeLabel ?location ?dist
WHERE {
# Berlin coordinates
wd:Q64 wdt:P625 ?berlinLoc .
SERVICE wikibase:around {
?place wdt:P625 ?location .
bd:serviceParam wikibase:center ?berlinLoc .
bd:serviceParam wikibase:radius "100" .
bd:serviceParam wikibase:distance ?dist.
}
FILTER EXISTS {
# Is an airport
?place wdt:P31/wdt:P279* wd:Q1248784 .
}
SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}
ORDER BY ASC(?dist)
The first line of the around
service call must have format ?item
predicate
?location
, where the result of the search will bind ?item
to items within the specified location and ?location
to their coordinates. The parameters supported are:
Predicate | Meaning |
---|---|
wikibase:center | The point around which search is performed. Must be bound for search to work. |
wikibase:radius | Distance from the center. Currently the distance is always in kilometers, other units are not supported yet. |
wikibase:globe | The globe which is being searched. Optional, default it's Earth (wd:Q2). |
wikibase:distance | The variable receiving distance information |
Search within box
editExample of box search Schools between San Jose, CA and Sacramento, CA:
# Schools between San Jose, CA and Sacramento, CA
#defaultView:Map
SELECT ?place ?placeLabel ?location
WHERE {
wd:Q16553 wdt:P625 ?SJloc.
wd:Q18013 wdt:P625 ?SCloc.
SERVICE wikibase:box {
?place wdt:P625 ?location.
bd:serviceParam wikibase:cornerSouthWest ?SJloc.
bd:serviceParam wikibase:cornerNorthEast ?SCloc.
}
?place wdt:P31/wdt:P279* wd:Q3914.
SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}
Coordinates may be specified directly:
# Schools between San Jose, CA and Sacramento, CA
#same as previous
#defaultView:Map
SELECT ?place ?placeLabel ?location
WHERE {
SERVICE wikibase:box {
?place wdt:P625 ?location.
bd:serviceParam wikibase:cornerWest "Point(-121.872777777 37.304166666)"^^geo:wktLiteral.
bd:serviceParam wikibase:cornerEast "Point(-121.486111111 38.575277777)"^^geo:wktLiteral.
}
?place wdt:P31/wdt:P279* wd:Q3914.
SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}
The first line of the box
service call must have format ?item
predicate
?location
, where the result of the search will bind ?item
to items within the specified location and ?location
to their coordinates. The parameters supported are:
Predicate | Meaning |
---|---|
wikibase:cornerSouthWest | The south-west corner of the box. |
wikibase:cornerNorthEast | The north-east corner of the box. |
wikibase:cornerWest | The western corner of the box. |
wikibase:cornerEast | The eastern corner of the box. |
wikibase:globe | The globe which is being searched. Optional, default it's Earth (wd:Q2). |
wikibase:cornerSouthWest
and wikibase:cornerNorthEast
should be used together, as well as wikibase:cornerWest
and wikibase:cornerEast
, and can not be mixed. If wikibase:cornerWest
and wikibase:cornerEast
predicates are used, then the points are assumed to be the coordinates of the diagonal of the box, and the corners are derived accordingly.
Distance function
editThe function geof:distance
returns distance between two points, in kilometers. Example usage:
# Airports within 100km from Berlin
SELECT ?place ?placeLabel ?location ?dist
WHERE {
# Berlin coordinates
wd:Q64 wdt:P625 ?berlinLoc.
SERVICE wikibase:around {
?place wdt:P625 ?location.
bd:serviceParam wikibase:center ?berlinLoc.
bd:serviceParam wikibase:radius "100".
}
# Is an airport
?place wdt:P31/wdt:P279* wd:Q1248784.
SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
BIND(geof:distance(?berlinLoc, ?location) as ?dist)
}
ORDER BY ?dist
# Places around 0°,0°
SELECT ?place ?placeLabel ?location ?dist
WHERE {
SERVICE wikibase:around {
?place wdt:P625 ?location.
bd:serviceParam wikibase:center "Point(0 0)"^^geo:wktLiteral.
bd:serviceParam wikibase:radius "250".
}
SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
BIND(geof:distance("Point(0 0)"^^geo:wktLiteral, ?location) as ?dist)
}
ORDER BY ?dist
References
edit