In this post, I’ll tell you how to geolocate your analysis using the Geopy. Geopy is a Python 2 and 3 library, that provides connections to the most popular geocoding services.
Why bother to geolocate your data? Because if you use latitude and longitude data, you can visualize your analysis on a map and derive more insights from your data.
“Each geolocation service you might use, such as Google Maps, Bing Maps, or Yahoo BOSS, has its own class in geopy.geocoders abstracting the service’s API.“
In this post, you will learn how to get the latitude and longitude coordinates from an address using a geocode method, and how to get an address from its coordinates using a reverse method.
First, you can install geopy with pip from your terminal.
pip install geopy
Google Maps API: You need a key to call the API. You can request your KEY here. The API allows you 2,500 calls per day. It works very well with bad data.
Nominatim API: Allows you 10,000 calls per day and its policy allows you to make at most 1 request per second. Make sure you add delays between each call to their API.
From address to coordinates
You need the following code to import the library in Python.
from geopy.geocoders import Nominatim from geopy.geocoders import GoogleV3 API_KEY = 'put-here-the-key-you-get-from-google-maps' #Creates a geolocator for Nominatim geolocatorNominatim = Nominatim(country_bias='Chile', timeout = 1.5) #Creates a geolocator for GoogleMaps geolocatorGoogleV3 = GoogleV3(api_key = API_KEY) def getNominatimGeolocation(address): "" A function to resolves geolocation from an address using the Nominatim API. "" location = geolocatorNominatim.geocode(address, exactly_one = True) if location: return (location) else: return None def getGoogleV3Geolocation(address): "" A function to resolves the geolocation from an address using Google Maps API. "" location = geolocatorGoogleV3.geocode(address, exactly_one = True, timeout=10) if location: return (location) else: return None
To test the code above you just need to use an address or a list of them. I’ll show you how to do it with a pandas DataFrame in Python. This is giving you the full address, its latitude, and longitude.
loc = getGoogleV3Geolocation('175 5th Avenue NYC') print(loc.address) print((loc.latitude, loc.longitude))
The results will be:
Flatiron Building, 175, 5th Avenue, Flatiron, New York, NYC, New York
From coordinates to full address
And if you need to do the opposite, to get the full address from its latitude and longitude you just call the reverse method of the geolocator.
from geopy.geocoders import Nominatim geolocator = Nominatim() location = geolocator.reverse("52.509669, 13.376294") print(location.address) print((location.latitude, location.longitude))
The result will be…
Potsdamer Platz, Mitte, Berlin, 10117, Deutschland, European Union
You might want to create a function to make this reverse operation repeatable, you can use the previous code as a starting point.
Having your data geolocated will be easy now to plot it into a map, and from here to use any tool of your preference to visualize your analysis.
I've worked in BI, DWH, and Data Mining. MSc in Data Science. Experience in multiple BI and Data Science tools always thinking how to solve information needs and add value to organisations from the data available. Experience with Business Objects, Pentaho, Informatica Power Center, SSAS, SSIS, SSRS, MS SQL Server from 2000 to 2017, and other DBMS, Tableau, Hadoop, Python, R, SQL. Predicting modelling. My interest are in Information Systems, Data Modeling, Predictive and Descriptive Analysis, Machine Learning, Data Visualization, Open Data. Specialties: Data modeling, data warehousing, data mining, performance management, business intelligence.