Python Forum
[SOLVED] [Nominatim] How to get "town" from reply?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[SOLVED] [Nominatim] How to get "town" from reply?
#1
Hello,

I use Nominatim to reverse geolocate by providing lat+lon and getting the full adress back.

Before I resort to regex for parsing, is there a better way to extract just the town from the reply?

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

for track in gj['features']:
    print(track['properties']['name'])
    print(track['properties']['description'])

    coords = track['geometry']['coordinates'][0]
    #Flip coords: Nominatim expects lat, lon
    coords = coords[::-1]
    #Must look like "lat,lon"
    location = "{}, {}".format(coords[0], coords[1])

    geolocator = Nominatim(user_agent="my-application")
    location = geolocator.reverse(location)
    #OK BUT TOO MUCH INFOS print(location.address)
    #OK BUT HOW TO EXTRACT TOWN? print(location.raw)
    #BAD city = location.raw['town']
    #BAD city = location.items("town")
    #BAD print(location.city)
    break
Thank you.

--

Edit: Without knowing what I was doing, I found how to extract that part:

print(location.raw['address']['town'])
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Issue with geolocator = Nominatim bborusz2 3 5,891 Dec-04-2020, 05:57 PM
Last Post: bborusz2
  need help, newbie in town. fburgos 1 2,051 Mar-18-2018, 04:37 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020