Most of the time, datasets are incomplete and often require pre-processing to make them usable. Imagine, We have some datasets with only an address column without latitude and longitude columns, and we want to represent this data geographically. To do that, we need to add geographic information to these data. These cannot be possible manually. We have to create a script to convert all those addresses to geographic information – Latitude and Longitude – to map their locations, which is known as Geocoding.
Geocoding is the computational process of transforming a physical address description to a location on the Earth’s surface (spatial representation in numerical coordinates)
Wikipedia
In this article, I will show you how to perform geocoding in Python with the help of Geopy library. Geopy has different Geocoding services that you can choose from, including Google Maps, ArcGIS, AzureMaps, Bing, etc. Some of them require API keys, while others can be accessed freely.
To perform geocoding, we need to install the following Geopy library using pip,
pip install geopy
Geocoding Single Address
In this example, we use Nominatim Geocoding service, which is built on top of OpenStreetMap data.
Let us Geocode a single address, the Taj Mahal in Agra, India.
locator = Nominatim(user_agent="myGeocoder")
location = locator.geocode("Taj Mahal, Agra, India")
First line of the above code will create locator
that holds the Geocoding service, Nominatim. In second line, we call geocode method of the locator service and pass any address to it, in this example, the Taj Mahal address.
print("Latitude = {}, Longitude = {}".format(location.latitude, location.longitude))
Above line will print out the coordinates of the location we have created.
Latitude = 48.85614465, Longitude = 2.29782039332223
That’s it.
So, the complete code will look like this,
import geopy
from geopy.geocoders import Nominatim
from geopy.extra.rate_limiter import RateLimiter
locator = Nominatim(user_agent="myGeocoder")
geocode = RateLimiter(locator.geocode, min_delay_seconds=1)
address = "Taj Mahal, Agra, India"
location = locator.geocode(address)
print("Latitude = {}, Longitude = {}".format(location.latitude, location.longitude))
Now, try some different addresses of your own.