4

I am using django-cities and GeoPy. I am trying to get city name, id and country name, id based on given latitude and longitude.

Normally this is how you can get city and country using GeoPy.

from geopy.geocoders import Nominatim

geolocator = Nominatim(user_agent='test', timeout=3)
location = geolocator.reverse('52.5094982,13.3765983')
loc_dict = location.raw
print(loc_dict['address'])

and this is the output:

{'suburb': 'Tiergarten', 'country_code': 'de', 'country': 'Deutschland', 'postcode': '10117', 'attraction': 'Potsdamer Platz', 'road': 'Potsdamer Platz', 'city': 'Berlin', 'city_district': 'Mitte', 'state': 'Berlin'}

In Django this is how I'm trying to get them.

Normally it should work, my problem is that GeoPy returns country: Deutschland whereas in django-cities it's Germany. And this happens to cities as well, GeoPy gives me București and django-cities Bucharest.

How can i get city name and country name like in django-cities? If i get the name like in django-cities i can get the id.

1 Answer 1

3

As per the doc, you can specify the language as a str type in the parameters of the reverse() method. By default it is language=False, that matches the language of the country for which the geocoder is applied, request for Ukraine => response in Ukrainian, for Netherlands => in Dutch etc.

Preferred language in which to return results. Either uses standard RFC2616 accept-language string or a simple comma-separated list of language codes.

New in version 1.0.0.

Let's emphasis that it must be written in lower case.

So, this request

from geopy.geocoders import Nominatim

geolocator = Nominatim(user_agent='test', timeout=3)
location = geolocator.reverse('52.5094982,13.3765983', language='en')
print(location.raw)

leads to this

{'place_id': 194316915, 'licence': 'Data © OpenStreetMap contributors, ODbL 1.0. https://osm.org/copyright', 'osm_type': 'way', 'osm_id': 404598740, 'lat': '52.5094969779106', 'lon': '13.376577040484365', 'display_name': 'Potsdamer Platz, Tiergarten, Mitte, Berlin, 10117, Germany', 'address': {'road': 'Potsdamer Platz', 'suburb': 'Tiergarten', 'borough': 'Mitte', 'city': 'Berlin', 'state': 'Berlin', 'postcode': '10117', 'country': 'Germany', 'country_code': 'de'}, 'boundingbox': ['52.5094194', '52.5095951', '13.3765714', '13.3765815']}

where specifically 'country': 'Germany' inside the 'address' key.

3
  • it still returns Deutschland
    – alex
    Commented Oct 18, 2017 at 11:27
  • it is in english, the only thing is that instead of Deutschland i want Germany, instead of Danmark i want Denmark, it seems it return every city and country exactly how they are written in their languange
    – alex
    Commented Oct 18, 2017 at 11:31
  • @NitaAlexandru Try with lower case language code (see updated answer)
    – JGH
    Commented Oct 18, 2017 at 11:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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