411 REFERENCE · Wikibooks
SPARQL/SERVICE - around and box
Reference material by Wikibooks contributors. This retains its source identity and is not 411 news reporting.
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 625.
Search around point
Example 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:
PredicateMeaningwikibase:centerThe point around which search is performed. Must be bound for search to work.wikibase:radiusDistance from the center. Currently the distance is always in kilometers, other units are not supported yet.wikibase:globeThe globe which is being searched. Optional, default it's Earth (wd:Q2).wikibase:distanceThe variable receiving distance information
Search within box
Example 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:
PredicateMeaningwikibase:cornerSouthWestThe south-west corner of the box.wikibase:cornerNorthEastThe north-east corner of the box.wikibase:cornerWestThe western corner of the box.wikibase:cornerEastThe eastern corner of the box.wikibase:globeThe 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
The 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
Wikidata query service/User Manual