Return individual address components (city, state, etc.) from GeoPy geocoder

lubar picture lubar · Jul 9, 2012 · Viewed 16.1k times · Source

I'm using GeoPy to geocode addresses to lat,lng. I would also like to extract the itemized address components (street, city, state, zip) for each address.

GeoPy returns a string with the address -- but I can't find a reliable way to separate each component. For example:

123 Main Street, Los Angeles, CA 90034, USA =>
{street: '123 Main Street', city: 'Los Angeles', state: 'CA', zip: 90034, country: 'USA'}

The Google geocoding API does return these individual components... is there a way to get these from GeoPy? (or a different geocoding tool?)

Answer

Kroenig picture Kroenig · Nov 7, 2016

You can also get the individual address components from the Nominatim() geocoder (which is the standard open source geocoder from geopy).

from geopy.geocoders import Nominatim

# address is a String e.g. 'Berlin, Germany'
# addressdetails=True does the magic and gives you also the details
location = geolocator.geocode(address, addressdetails=True)

print(location.raw)

gives

{'type': 'house',
 'class': 'place',
 'licence': 'Data © OpenStreetMap contributors, ODbL 1.0. http://www.openstreetmap.org/copyright',
 'display_name': '2, Stralauer Allee, Fhain, Friedrichshain-Kreuzberg, Berlin, 10245, Deutschland',
 'place_id': '35120946',
 'osm_id': '2825035484',
 'lon': '13.4489063',
 'osm_type': 'node',
 'address': {'country_code': 'de',
             'road': 'Stralauer Allee',
             'postcode': '10245',
             'house_number': '2',
             'state': 'Berlin',
             'country': 'Deutschland',
             'suburb': 'Fhain',
             'city_district': 'Friedrichshain-Kreuzberg'},
 'lat': '52.5018003',
 'importance': 0.421,
 'boundingbox': ['52.5017503', '52.5018503', '13.4488563', '13.4489563']}

with

location.raw['address']

you get the dictionary with the components only.

Take a look at geopy documentation for more parameters or Nominatim for all address components.