[GIS] Proper query of OSM APIs

nominatimopenstreetmappolygonpolygon-creation

I'm pretty new to maps and geographic systems. Maybe you can help me out with this OSM problems. I'm working on web application in WebGL, and I have two major questions:

I would like to know how to correctly query for city boundaries polygon. I've tried to use JSONP and http.get requests to get data form 'http://polygons.openstreetmap.fr/get_geojson.py?id=1582777' but I can't get it because of CORS policy. This server seems to be JSONP disabled. Does any other kind of similar API exists?

To get proper relations of my address I'm querying 'nominatim.openstreetmap.org' multiple times, every time constructing a new URL. Query for address to get city, city to get country, etc. This works but feels really tiresome. How do I query nominatim, knowing street address and getting all info that I need (city, country, continent)?

Can you give me some hints?

Best Answer

NOTE: CORS is not a gis.stackexchange topic, see Stackoverflow for solutions (ex. NGINX solutions)... The real problem will be stable IDs at Openstreetmap: you have no guarantee that tomorrow they will continue to use the same ID, this (unbelievably) is still an open problem for them, see the Permanent ID open proposal

Answer to the good question

The best way to get realiable limits is to check at Wikidata, where are many ways, from wiki-API to quering by SparQL. So:

  1. Get your "City ID" at Wikidata. For instance, Berlin is Q64.

  2. At Wikidata full JSON of Q64 or other API, get the value of the field "OSM relation ID", that is the field of P402... At the exemplified API, something as entities.Q64.claims.P402.mainsnak.datavalue.value. In this case the value will be 62422.

  3. Use the returned value in an OpenStreetMap API, as Overpass or the URL in your example, that is, http://polygons.openstreetmap.fr/get_geojson.py?id=62422 Nominatin also offers good JSON dataset about cities, https://nominatim.openstreetmap.org/details.php?osmtype=R&osmid=62422&class=boundary&format=json


NOTE: the procedure above is generic, you can use it for any geospatial feature, as a hayway, like the federal motorway 555 Autobahn in Germany, that is the Q17061 Wikidata entity, that is pointing to the OSM relation ID 23092.

Related Question