How to use Geopy to geocode locations

In this article, I’ll introduce you to a Python library called Geopy. It is a geocoding package

What is Geocoding?

Geopy is a Python client for many common web services for geocoding, making it easy to locate the coordinates of an address, a city, or a country for Python developers, and vice versa.

It uses third-party geocoders and other data sources to find the coordinates of addresses, towns, continents, and landmarks around the globe.

Geocoding is the method of translating a position definition (such as a physical address or a location name) into a latitude and longitude pair for that location on the surface of the Earth.

Geocoding Services for Enterprise Applications

There are many Geocoding services available in the market. I have tried GeocodeAPI and they work really well for enterprise applications. They have multiple endpoints to get lat-long from address, reverse geocoding, and auto-complete address.

They provide 10,000 free requests per day, which is great if you are just starting to create your application. You can get more details about their features and pricing from this page.

Installation

We can install and use Geopy right out of the box:

pip install geopy

And we’ll import the package, along with a few other important packages:

from geopy.geocoders import Nominatim
import pandas as pd

Create a new user agent

Nominatim is a tool to search OpenStreetMap data by name and address (geocoding).

OpenStreetMap is a map of the world, which has been created as an open-source software by people on the internet. So we’ll make a new api object to access the nominatim client:

app = Nominatim(user_agent="JournalDev")

Displaying the location of a region

So now we are ready to get location details of any place in the world. We can use the app client we created to access it:

location = app.geocode("Kolkata, India")
location

which gives us the output as:

Location(Kolkata, Howrah, West Bengal, 711101, India, (22.5414185, 88.35769124388872, 0.0))

We can instead get a dictionary, which we can then use as a dataframe !

However, for some reason when converting the dictionary into dataframe, it gives rise to some spurious tuples, so we need to handle that:

location = app.geocode("Kolkata, India").raw
pd.DataFrame(location).head(1)

which gives us:

Geopy Output Location Coordinates
Geopy Output Location Coordinates

Automatic fetching an address list

So we could do this one at a time for each address, but in large institutions, you’d generally need thousands of addresses fetched at once.

Let’s make a list of addresses:

address_list = ['Goregaon, Mumbai', 'Kota, Rajasthan','New York', 'Alaska']
#add more as you wish

Now we’ll run a loop. Often the openstreetmap api won’t allow a bot to access their data continuously because their servers will crash. So we need to put wait timers to avoid errors:

for address in address_list:
  try:
    time.sleep(1)
    loc = app.geocode(address).raw
    df = df.append(loc, ignore_index=True)
  except Exception as e:
    print(e)

Now this will append the data to our original dataframe:

Geopy Output Location Coordinate Lists
Geopy Output Location Coordinate Lists

Reverse searching a coordinate

It also allows the user to reverse search a pair of coordinates ( in decimal degrees):

app.reverse((37.234332396,-115.80666344), language='hi').raw

will give us (in our language of choice):

Geopy In Hindi
Geopy In Hindi

Finding distance

We can find the distance between two places in km or miles, using:

from geopy.distance import geodesic
kolkata = (df['lat'][0],df['lon'][0])
goregaon = (df['lat'][1],df['lon'][1])
print("In kms : ",geodesic(kolkata,goregaon).km)
print("In miles : ",geodesic(kolkata,goregaon).miles)

which gives us:

In kms : 1655.7023089773734

In miles : 1028.805717719377

Ending Note

If you liked reading this article and want to read more, follow me as an author. Until then, keep coding!