DEV Community

Leul Dereje Bonsa
Leul Dereje Bonsa

Posted on

Geo coding Country Names in Python

Have you ever needed to work with Geo spatial data in your data analysis or mapping projects, but found yourself struggling to convert country names into geographic coordinates? That's exactly the challenge I faced recently while working on a project for a client. I needed to plot a map of their global operations, but all I had was a list of country names.After some research, I discovered that Python offers powerful tools for GEo-codding country names and obtaining their corresponding latitude and longitude coordinates. With the help of the geopy and pycountry libraries, I was able to quickly and easily convert the country names into coordinates, and plot them on a map using a mapping library like folium and plotly.
Geo-coding is the process of converting a human-readable address or location name into geographic coordinates (latitude and longitude) that can be used in maps and other Geo-spatial applications.
To get started, first make sure you have both geopy and pycountry installed in your Python environment. You can install them using pip by running pip install geopy pycountry in your terminal or command prompt.

  • Next, create a function that takes a country name as input and returns its latitude and longitude coordinates. Here's an example function that uses the Nominatim geocoder from geopy to look up the country's coordinates:
from geopy.geocoders import Nominatim
import pycountry

def get_coordinates(country):
        country_obj = pycountry.countries.get(name=country)
        geolocator = Nominatim(user_agent="my-application")
        location = geolocator.geocode(
        return location.latitude, location.longitude
    except AttributeError:
        return None, None

Enter fullscreen mode Exit fullscreen mode

In this function, we first use pycountryto look up the Countryobject for the given country name. We then create a new instance of the Nominatimgeocoder from geopy, passing in a custom user agent string. Finally, we use the geocodemethod of the geocoder to look up the coordinates for the country, and return them as a tuple.

Note that in the geocodemethod, we pass in instead of just country. This is because Nominatimrequires the input to be in the format of "City, State, Country" or "Street, City, State, Country". By passing in the country name as the only argument, we're more likely to get accurate results for countries with common or ambiguous names.

  • To use this function on a pandas DataFrame, you can use the apply method as follows:
import pandas as pd

df = pd.DataFrame({'country': ['United States', 'France', 'China']})
df[['latitude', 'longitude']] = df['country'].apply(get_coordinates).apply(pd.Series)

Enter fullscreen mode Exit fullscreen mode

In this code, we first create a new pandas Data Frame with a column named "country". We then use the apply method to applythe get_coordinatesfunction to each value in the "country" column. Finally, we use the applymethod again, this time with pd.Seriesas the argument, to split the resulting tuples of latitude and longitude into separate columns.

If you need to handle special cases such as "World" or "International", you can modify the get_coordinatesfunction accordingly. For example, you can assign the coordinates of the Antarctic for "World", and the coordinates of the United Nations Headquarters in New York for "International".

That's it! With these tools and techniques, you should be able to geocode country names and work with geospatial data in Python.

Top comments (0)