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
```lang=sparql
?wd wdt:P625 ?location .
BIND(geof:lookup('World Countries Outline.map', ?location, 'iso') as ?iso)
```
```lang=sparql
SERVICE wikibase:geolookup {
bd:serviceParam wikibase:data 'World Countries Outline.map' . # this is the .map page on Commons in the data namespace
bd:serviceParam wikibase:globe wd:Q2 . # The globe which is being searched. Optional, default it's Earth (wd:Q2)
?place wdt:P625 ?location . # ?location specifies the point to lookup
?wd wikibase:property 'wikidataId' . # assuming geojson contains wikidata IDs in properties, extract it (as a string) into ?wd
?iso wikibase:property 'isoCode' . # more than one property can be extracted from the same geojson feature
}
```
=== 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