I think it would be a very valuable addition to have a generic polygon lookup function for the WDQS.
Use cases
- Given a geo point, tell me which country/region/zip code/tectonic plate/voting district it belongs to
- Historic geo lookups - use older map data
- Non-earth lookups - regions of the moon/mars/...
Usage as a service
SELECT * WHERE { ?wd wdt:P625 ?location . SERVICE wikibase:geolookup { # --- INPUT --- # this is the .map page on Commons in the data namespace bd:serviceParam wikibase:data 'World Countries Outline.map' . # The globe which is being searched. Optional, default it's Earth (wd:Q2) bd:serviceParam wikibase:globe wd:Q2 . # ?location specifies the point to lookup bd:serviceParam wikibase:location ?location . # --- OUTPUT --- # Assigns geojson's wikidataId property to ?countryWd ?countryWd wikibase:property 'wikidataId' . # more than one property can be extracted from the same geojson feature ?countryIso wikibase:property 'isoCode' . } }
Algorithm
- Download commons:data:World Countries Outline.map page
- Create (and cache) an RTree from all closed polygons. Should also handle multipolygons with holes.
- For all ?location points, find the first polygon that contains it.
- extract all requested properties from the found polygon