Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?)

like image 698
lubar Avatar asked Jul 09 '12 07:07

lubar


2 Answers

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.

like image 185
Kroenig Avatar answered Sep 18 '22 15:09

Kroenig


Use usaddress by DataMade. Here's the GitHub repo.

It works like this usaddress.parse('123 Main St. Suite 100 Chicago, IL') and returns this array

[('123', 'AddressNumber'), ('Main', 'StreetName'), ('St.', 'StreetNamePostType'), ('Suite', 'OccupancyType'), ('100', 'OccupancyIdentifier'), ('Chicago,', 'PlaceName'), ('IL', 'StateName')]

like image 41
stevevance Avatar answered Sep 21 '22 15:09

stevevance