g4vroche
g4vroche

Reputation: 527

Get the city at given coordinates in Nominatim

I need to retrieve a city at given coordinates using Nominatim. By city, I mean the relevant OSM relation/node with all its properties, not just the country name (Actually, I really care about osm_id, osm_type, name and coordinates)

EDIT: Added context

I run a platform where users can attach posts to places. I use OSM as primary database to help users retrieve places. Once a post is created, I reference the place in my business database. I want to be able to aggregate posts at city and country level. So I represent those with dedicated tables and typical relationship: place n-1 city n-1 country.

Currently I know I can fetch the maximum level of information (zoom=18) with a query like this :

http://nominatim.openstreetmap.org/reverse?format=xml&accept-language=fr&lat=43.8338&lon=4.3596&zoom=18&addressdetails=1

which produce relveant informations including the city name :

  <reversegeocode timestamp="Thu, 02 Oct 14 15:16:11 +0000" attribution="Data © OpenStreetMap contributors, ODbL 1.0. http://www.openstreetmap.org/copyright" querystring="format=xml&accept-language=fr&lat=43.8338&lon=4.3596&zoom=18&addressdetails=1">
    <result place_id="6643770" osm_type="node" osm_id="680342651" lat="43.833464" lon="4.3596107">3, Rue de la République, Écusson, La Placette, Nîmes, Nimes, Gard, Languedoc-Roussillon, France métropolitaine, 30000;30900, France</result>
    <addressparts>
            <house_number>3</house_number>
            <road>Rue de la République</road>
            <neighbourhood>Écusson</neighbourhood>
            <suburb>La Placette</suburb>
            <city>Nîmes</city>
            <county>Nimes</county>
            <state>Languedoc-Roussillon</state>
            <country>France</country>
            <postcode>30000;30900</postcode>
            <country_code>fr</country_code>
    </addressparts>
 </reversegeocode>

I get the city as a string in the city tag. But I have to proceed to another query if I want to get OSM point for that city :

http://nominatim.openstreetmap.org/search?format=xml&city=N%C3%AEmes&=state=France+m%C3%A9tropolitaine&country=France&limit=1

What is the bestway to optimize this ?

Since I run my own instance of Nominatim, I could probably go for SQL if that is the best option, but I would have to create a new endpoint in Nominatim with all maintenance complications...

I need the same functionality for countries. For countries I figured out that the parameter zoom=1 should do the job in any case. Tough, there is no reliable counterpart for cities.

Upvotes: 2

Views: 3944

Answers (1)

MaM
MaM

Reputation: 2069

Ok so you get lat/lon of places via nominatim (reverse geocode). Now you want to construct the hierachies (location -> city / country) to link your posts to a dedicated location. There seem to be 2 ways in my opinon:

Online:

I see (currently) no alternative to your approach :-/ Be aware that you use voluntered services. You might also consider to lookup via Overpass API and a small bbox of your lon/lat.

Another thought would be to use the Wikipedia data on places so you can parse the hierachies quiet easy.

Offline:

Beside sending API queries you might want to setup a own workflow. E.g. a PostGIS DB with imported Shapefiles of boundaries. This gives you full control and doesn't stress public ressources. Be aware that the quality / coverage with boundaries isn't always good at OSM.

Both approaches might allow you to do a preprocessing, so your final request can run against a local DB store.

P.S: I'm not a lawyer, but this sounds like you created a mixed DB that relys on a fundamental part at OSM material. Sus it might be possible that you need to apply the ODbL also on your DB: https://wiki.openstreetmap.org/wiki/Legal_FAQ

Upvotes: 2

Related Questions