1

I need to loop through a GeoJSON file, where for each "feature", find the city where the track started:

with open('input.geojson') as f:
    gj = geojson.load(f)

for track in gj['features']:
    #OK
    print track['geometry']['coordinates'][0]

    #How to query Nominatim to find the city that lat,lon belongs to?

    #Next step : edit 'name' in gj

with open('dump.geojson', 'w') as f:
   dump(gj, f, indent=2)

Would someone have some code handy?

1

You can try using Geopy.

from geopy.geocoders import Nominatim

geolocator = Nominatim(user_agent='my_app')

with open('input.geojson') as f:
    geojson = json.load(f)

for i, track in enumerate(geojson['features']):
    lat, lon = track['geometry']['coordinates'][0]
    location = geolocator.reverse((lat, lon))
    # full address as returned by Nominatim
    address = location.address
    # city or country
    city = location.raw['address']['city']
    country = location.raw['address']['country']
    # update geojson
    geojson['features'][i]['properties']['name'] = city

with open('dump.geojson', 'w') as f:
    json.dump(geojson, f, indent=True)
0

You can use Reverse Geocoding as in below example:
https://wiki.openstreetmap.org/wiki/Nominatim#Example

1) XML response:
Request:

 https://nominatim.openstreetmap.org/reverse?format=xml&lat=52.5487429714954&lon=-1.81602098644987&zoom=18&addressdetails=1

Result:

 <reversegeocode timestamp="Fri, 06 Nov 09 16:33:54 +0000" querystring="...">
    <result place_id="1620612" osm_type="node" osm_id="452010817">
      135, Pilkington Avenue, Wylde Green, City of Birmingham, West Midlands (county), B72, United Kingdom
    </result>
    <addressparts>
      <house_number>135</house_number>
      <road>Pilkington Avenue</road>
      <village>Wylde Green</village>
      <town>Sutton Coldfield</town>
      <city>City of Birmingham</city>
      <county>West Midlands (county)</county>
      <postcode>B72</postcode>
      <country>United Kingdom</country>
      <country_code>gb</country_code>
    </addressparts>   
  </reversegeocode>

Reference:
https://wiki.openstreetmap.org/wiki/Nominatim#Example

2) JSON response:
Request:
https://nominatim.openstreetmap.org/reverse?format=jsonv2&lat=-34.44076&lon=-58.70521

Response:

{
  "place_id":"134140761",
  "licence":"Data © OpenStreetMap contributors, ODbL 1.0. http:\/\/www.openstreetmap.org\/copyright",
  "osm_type":"way",
  "osm_id":"280940520",
"lat":"-34.4391708",
  "lon":"-58.7064573",
  "place_rank":"26",
  "category":"highway",
  "type":"motorway",
  "importance":"0.1",
  "addresstype":"road",
  "display_name":"Autopista Pedro Eugenio Aramburu, El Triángulo, Partido de Malvinas Argentinas, Buenos Aires, 1.619, Argentina",
  "name":"Autopista Pedro Eugenio Aramburu",
  "address":{
    "road":"Autopista Pedro Eugenio Aramburu",
    "village":"El Triángulo",
    "state_district":"Partido de Malvinas Argentinas",
    "state":"Buenos Aires",
    "postcode":"1.619",
    "country":"Argentina",
    "country_code":"ar"
  },
  "boundingbox":["-34.44159","-34.4370994","-58.7086067","-58.7044712"]
}

https://wiki.openstreetmap.org/wiki/Nominatim#Example_with_format.3Djsonv2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.