Geo coding Country Names in Python
Tags: geocoding, python, geospatial, dataanalysis
Published: 2023-04-06 09:43:29

---
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](https://github.com/geopy/geopy) and [pycountry](https://pypi.org/project/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](https://python-visualization.github.io/folium/) and [plotly]
(https://plotly.com/).
**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](https://github.com/geopy/geopy) and [pycountry](https://pypi.org/project/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):
    try:
        country_obj = pycountry.countries.get(name=country)
        geolocator = Nominatim(user_agent="my-application")
        location = geolocator.geocode(country_obj.name)
        return location.latitude, location.longitude
    except AttributeError:
        return None, None

```

In this function, we first use `pycountry `to look up the `Country `object for the given country name. We then create a new instance of the `Nominatim `geocoder from `geopy`, passing in a custom user agent string. Finally, we use the `geocode `method of the geocoder to look up the coordinates for the country, and return them as a tuple.

Note that in the `geocode `method, we pass in `country_obj.name` instead of just `country`. This is because `Nominatim `requires 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)

```
In this code, we first create a new pandas Data Frame with a column named "country". We then use the apply method to `apply `the `get_coordinates `function to each value in the "country" column. Finally, we use the `apply `method again, this time with `pd.Series `as 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_coordinates `function 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.
---