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
```lang=sparql
SELECT * WHERE {
?wd wdt:P625 ?location .
SERVICE wikibase:geolookup {
bd:serviceParam wikibase:data 'World Countries Outline.map' . # this is the .map page on Commons in the data namespace# --- INPUT ---
bd:serviceParam wikibase:globe wd:Q2 . # The globe which is being searched. Optional, default it's Earth (wd:Q2)# this is the .map page on Commons in the data namespace
bd:serviceParam wikibase:location ?location . # INPUT: ?location specifdata 'World Countries the point to lookupOutline.map' .
?countryWd wikibase:property 'wikidataId' . # Assigns geojson's wikidataId property to ?countryWd# The globe which is being searched. Optional, default it's Earth (wd:Q2)
?countryIso wikibase:property 'isoCode' . # more than one property can be extracted from the same geojson feature
} }
```
=== Usage as abd:serviceParam wikibase:globe wd:Q2 .
# ?location specifies the point to lookup
**NOTE**: @smalyshev does not recommend this approach, as functions are not well suited for external data initialization, like services. bd:serviceParam wikibase:location ?location .
# --- OUTPUT ---
```lang=sparql # Assigns geojson's wikidataId property to ?countryWd
?wd wdt:P625 ?location .countryWd wikibase:property 'wikidataId' .
# more than one property can be extracted from the same geojson feature
BIND(geof:lookup('World Countries Outline.map', ?location,countryIso wikibase:property 'iso') as ?iso)Code' .
} }
```
=== 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