Data Science

Get Continent Names from Coordinates Using Python

Extracting location info from latitude + longitude coordinates

Benedict Neo
bitgrit Data Science Publication

--

Photo by NASA on Unsplash

I recently competed in a datathon where we built this streamlit app to visualize gas turbine engines.

In one of the challenges, I wanted to get continent names from latitude and longitude coordinates.

I went on to Google and found that it was surprisingly non-straightforward and required using two Python packages to achieve what I needed.

Without further ado, here’s the code I used to get the continent name from the coordinates.

Code for this article → Deepnote

Use Python? Here are some Python F-strings Tricks You Should Know.

Install libraries

First, let’s install the necessary packages.

Here doing _ = basically tells python to throw the installation outputs into the throwaway variable _

Load libraries

  • geopy — A Python client that enables locating the coordinates of addresses, cities, countries, and landmarks across the globe using third-party geocoders and other data sources
  • pycountry-convert — Extension of Python package pycountry providing conversion functionalities
  • tqdm — progress bar for Python

Data

Get the dataset in this gist.

Here is an example of latitude and longitude coordinates in our dataset

Geocoders

Let’s use the geopy library to find the location of this coordinate.

Let’s break down what Nominatim is and why a user_agent parameter is important.

  • Nominatim is a geocoding service that powers the library.
  • user_agent is an HTTP request header sent with each request.
  • Nominatim runs on donated servers with limited capacity, so specifying a user-agent field allows Open Street Map to easily track who is using their service. I have it as <APP_NAME> here.
  • If you have a lot of queries, Nominatim asks that the user_agent also contains your email address (or that you use the email parameter)

Once you do that, you can use the reverse method to return an address

Let’s view the raw output.

Looks like that lat long coordinate corresponds to a location in Arkhangelsk Oblast, Russia.

Let’s index the address field, which has the information we want.

Getting continent information

With the country code, we can get the continent code with pycountry convert.

We’ll use the country_alpha2_to_continent_code function, which converts a country code ISO 3166–1 alpha-2 to a continent code.

Here we take the country code previously and pass that into the function.

Note: it only takes uppercase values

Get the full continent name.

I don’t want the code value but a full name, so I created this function that maps the code to the corresponding full name of the continent.

Wrapping everything into a function

The goal is to run the reverse function on multiple coordinates in a data frame, but too many requests to Nomatim will spit out a Too Many Requests 429 HTTP error

So based on the docs, we need a rate limiter.

Rate Limiter

The rate limiter class allows us to perform bulk operations by adding delays to our calls.

This delay is created with the min_delay_seconds=1 argument, which adds a delay of 1 second between each pair of geolocator.geocode calls.

When reverse returns None

Also, for some weird reason, when geopy.reverse returns None, the coordinates are in Antarctica, which I checked using online conversion tools. So I account for that in the function.

Now it’s time to apply our function to the data frame.

Apply function to the data frame

Here we unpack the output into two columns named COUNTRY and CONTINENT

We use progress_apply which is a function by tqdm to provide progress on Pandas’ apply function.

To the list results to output as columns, we set result_type = "expand"

Now our data frame has two new columns!

Let’s have some fun plotting with the continent names.

Our coordinates are color coded!

That’s all for this article.

Let me know in the comments below if there’s a faster way to get continent names from latitude-longitude coordinates!

Thanks for reading!

Like my writing? Join Medium with my referral link. You’ll be supporting me directly 🤗

--

--