Introduction to Geopy: Using Your Latitude & Longitude Data in Python
By: Eunjoo Byeon
Tags: Python, Programming, Tutorial, Data Science, Data Analysis

Introduction to Geopy: Using Your Latitude & Longitude Data in Python

Getting Address, Postal Code, Distance, and More

Longitude and latitude are great measures to obtain relatively precise locations of the observations, but oftentimes these coordinates alone do not tell enough stories. How can we easily convert these measures to something more meaningful?

Geopy

We can do many things with these coordinates using the Geopy library. Geopy can calculate the distance between different points, it can also make an API call on our behalf to get information about these coordinates.

Getting Address Information

First, we will talk about getting information such as postal code (zip code), city, neighborhoods, etc. Geopy can make API requests to Nominatim (a geocoding software used for OpenStreetMap), Google Maps, Bing Maps, and many others. One thing to note is that Geopy does not provide this info, but it simply helps the user connects to them, so it’s important for users to understand the terms, pricing, and limitations of each API. Nominatim, popular for small-scale projects, is a free option that allows a limited number of requests. Please see here for Nominatim’s API use policy.

If you need to use other APIs, you will likely need to obtain an API key directly from the API providers.

Installing Geopy

The first thing to do is installing Geopy.

pip install geopy

Establishing Connection

We need to establish a connection to APIs by setting up the geocoder. First import the geocoder you want to use, and initiate it.

from geopy.geocoders import Nominatim

geocoder = Nominatim(user_agent = 'your_app_name')

For other APIs, you may need to put in other parameters such as username/password, API keys, etc. Please refer to the document.

Rate Limiter (optional)

Just like when we make API calls directly ourselves, we want to make sure that we are not overloading the server-side with our requests. Rate limiter allows you to add some delays in between the calls.

from geopy.extra.rate_limiter import RateLimiter

geocode = RateLimiter(geocoder.geocode, min_delay_seconds = 1, return_value_on_exception = None)

# adding 1 second padding between calls

Getting Address Information

Now that we have established the connection, let’s try to retrieve information using the coordinates.

location = geocode.reverse((lat, long))

# returns geopy Location object

Location objects have instances of address, altitude, latitude, longitude, point. address returns a concatenated string of all information that belongs to the address. This may or may not include information like an amenity, road, neighborhood, city block, suburb, county, city, state, postcode, country, country code. We can look more directly into the structure of information by using the raw instance.

location.raw

# returns a dictionary

Again, each API will have a different architecture, so it’s a good idea to always check the raw version. From this raw dictionary we can pull the information we would like. In this case, let’s extract postal-code, which is nested under address .

zipcode = location.raw['address']['postcode']

Getting Latitude and Longitude

We noticed above that we are using the reverse function of our geocoder object. That’s because the non-reversed, general use of this geocoder is to get the actual coordinates from the address. This can be handy if we want to assess the geographical effect or to quantify the categorical locations.

# after initiating geocoder

location = geocode(address)

# returns location object with longitude, latitude and altitude instances

(location.latitude, location.longitude)

Getting Distance

Another important variable we can extract from the coordinates is the distance between different locations. This is a simple task using Geopy. Geopy offers both the great-circle distance and the geodesic distance.