analytics
Get full address from geo coordinates using Python for free
·3 min read

Piotr
Founder

Recently, I have been tasked to retrieve full addresses from geo coordinates for over 4k records. Nominatim is a great tool for this task, but it has a limit of 1 request per second.
You can read this on Medium here.
IMPORT LIBRARIES
python
1import pandas as pd2from geopy.geocoders import Nominatim3from geopy.extra.rate_limiter import RateLimiter
IMPORT DATA
python
1df = pd.read_csv("france_ria_locations.csv")

I imported csv file with geo coordinates to pandas DataFrame and I already had coords pair as a string. So, we will need to convert it to a tuple of floats, but first let's initialize Nominatim geocoder.
python
1# Initialize the geocoder2geolocator = Nominatim(user_agent="myGeocoder")
and create a rate limiter:
python
1# Create a rate limiter2geocode = RateLimiter(geolocator.reverse, min_delay_seconds=1)
CONVERT COORDS TO TUPLE OF FLOATS
python
1## convert 'coords' from string to a tuple of floats2df['coords'] = df['coords'].apply(lambda x: tuple(map(float, x.strip('()').split(','))))
The next step is to add 'location' column to DataFrame by applying Nominatim geocoder to 'coords' column.
python
1# Add 'location' column to dataframe by applying geocode to 'coords' column2df['location'] = df['coords'].apply(geocode)
SHAPING THE DATAFRAME
python
1# Add 'address', 'city' and 'zip' columns2df['address'] = df['location'].apply(lambda loc: loc.raw['address']['road'] if 'road' in loc.raw['address'] else None)3df['city'] = df['location'].apply(lambda loc: loc.raw['address']['town'] if 'town' in loc.raw['address'] else None)4df['zip'] = df['location'].apply(lambda loc: loc.raw['address']['postcode'] if 'postcode' in loc.raw['address'] else None)

SAVE TO CSV
python
1# Save to csv2df.to_csv('france_ria_locations_with_address.csv', index=False)
Happy coding!
Piotr

