Reverse Geocoding with GeoNames in Python

How to get Location names from raw Latitude/Longitude values

Sifat Muhammad Abdullah
Analytics Vidhya
3 min readMay 20, 2020

--

What is Reverse Geocoding?

The process of finding the name of an address or a location for a given latitude/longitude pair. It is an essential module in many location related data science applications.

I was trying to find a way to get the location names in English for a set of latitude/longitude pairs. I found some blogs using Nominatim which is also used by OpenStreetMap(OSM) for geocoding and reverse-geocoding purposes. But, Nominatim returns location names in local language (for instance, for China in Chinese, Bangladesh in Bengali etc). Sure, you can easily translate them to English in python, but there is also another way using GeoNames which is free for use and returns location names in English.

In this tutorial, we will use GeoNames class from geopy library to perform reverse-geocoding.

Install Library

At first, we need to install geopy library from terminal with the following command.

Now, our coding part starts.

Import Class

We just need to import one class for this tutorial.

Initialization

In order to initialize GeoNames, we have to do the following:
1. Create a new user account here.
2. In the logged in state, go to this link to activate free web services.
3. Click “Click here to enable” link below the page

So, our account has been granted free web services from GeoNames.

Now, we initialize by providing our GeoNames account username in place of ‘your_username’ like the following:

Find Location Name

Next, we enter latitude, longitude values to get the desired location info.

The above code will return a list type object. We can do the following to get name of the location:

Thus, in only a few lines of code, we can get the location name of a particular address.

Free Data Sources

GeoNames also open sourced their Data Sources on several countries. One can easily download these according to one’s needs.

Limitations

Although GeoNames is free for general purpose use, it does have some limitations. There are hourly and daily limits per application(identified by username). So, with one username, we can perform a certain number of requests(function calls). For reverse-geocoding, we can perform about 333 requests in one hour. If you want to use for commercial purposes, you can purchase credits too. But for small scale projects, the free version is good enough.

Conclusion

We can now easily find our desired location names from given latitude/longitude pairs. For ease of use, here is the final code.

I hope you find this article helpful :) .

All the Best!

--

--