Geospatial search for Wikidata Query Service

May 14, 2016 0 By addshore

Geospatial search is up and running for the Wikidata Query Service! This allows you to search for items with coordinates that are located within a certain radius or within a bounding box.

Along side the the map that can be used to display results for the query service this really is a great tool for quickly visualizing coverage.

Search around a point

The below query selects all items that are within a 100km radius circle of Berlin. Checkout the in-line comments to see exactly that is happening.

# Select the ItemId, label and coordinate location
SELECT ?place ?placeLabel ?location WHERE {
 # Select the coordinate location(P625) of Berlin(Q64) as the centeral coordinate ?mainLoc
 wd:Q64 wdt:P625 ?mainLoc . 
 # Use the around service
 SERVICE wikibase:around { 
 # Looking for items with coordinate locations(P625)
 ?place wdt:P625 ?location . 
 # That are in a circle with a centre of ?mainLoc(The coordinate location of Berlin)
 bd:serviceParam wikibase:center ?mainLoc . 
 # Where the circle has a radius of 100km
 bd:serviceParam wikibase:radius "100" . 
 }
 # Use the label service to get the English label
 SERVICE wikibase:label {
 bd:serviceParam wikibase:language "en" . 
 }
}

This query currently matches 9468 results and when the results are rendered on the built in map for the query service they look like this.

WDQS Geospatial Query 100km of Berlin

Of course you can build a more complex query to refine the result further, for example to only show airports within the circle. The extra code along with the resulting image can be seen below.

  # Is an airport
  ?place wdt:P31/wdt:P279* wd:Q1248784

WDQS Geospatial Query Airports 100km of Berlin

Search within a bounding box

The below query selects all items between San Jose, CA and Sacramento, CA using a bounding box.

SELECT * WHERE {
  # Select the two corners for the box, San Jose & Sacramento
  wd:Q16553 wdt:P625 ?SJloc .
  wd:Q18013 wdt:P625 ?SCloc .
  # Use the box service
  SERVICE wikibase:box {
      # Looking for items with coordinate locations(P625)
      ?place wdt:P625 ?location .
      # Set the south west and north east corners of the box
      bd:serviceParam wikibase:cornerSouthWest ?SJloc .
      bd:serviceParam wikibase:cornerNorthEast ?SCloc .
    }
}

All of the items from within this box can be seen in the image below, but similarly with the circle you can add more to the query to refine the results.

WDQS Geospatial Query CA box

You can find more details about the Geospatial queries on the mediawiki.org help page @ https://www.mediawiki.org/wiki/Wikidata_query_service/User_Manual#Geospatial _search