Geocoding in Python with geopy
Geocoding is the process of converting addresses (e.g. a street or place address) into geographic coordinates (like latitude and longitude). You can geocode by providing a single place description at a time or multiple of them simultaneously in a table. The output locations are geographic features with attributes that can be used for mapping or spatial analysis.
You can quickly find various kinds of locations through geocoding. The types of locations that you can search for include points of interest or names from a gazetteer, like mountains, bridges, and stores and addresses, which can come in a variety of styles and formats, including street intersections, house numbers with street names, and postal codes.
In this tutorial, I will show you how to perform geocoding in Python with the help of Geopy library. Let's install these libraries with Pip if you have already Anaconda environment setup.
pip install geopandas
pip install geopy
Geopy python library can be used to geolocate a signle address. Geopy has different Geocoding services that you can choose from, including Google Maps, ArcGIS, AzureMaps, Bing, etc. Some of them require API keys, while others do not need.
We will use Nominatim Geocoding service, which is built on top of OpenStreetMap data. Let's Geocode a single address, London Eye in London.
from geopy.geocoders import Nominatim
geolocator = geolocator = Nominatim(user_agent="myGeocoder")
location = geolocator.geocode("London Eye, London, UK")
We create geolocator that holds the Geocoding service, Nominatim. Then we pass the locator we created to geocode any address, in this example, the London Eye address.
print("Latitude = {}, Longitude = {}".format(location.latitude, location.longitude))
Now, we can print out the coordinates of the location we have created.
Latitude = 51.5033416, Longitude = -0.11967649999999999
location_rev = geolocator.reverse("16.79906325, 96.14942259240468")
print(location_rev)
Shwedagon Pagoda, Southern Stairway, ဒဂုံ, Western District, Yangon, ရန်ကုန်တိုင်းဒေသကြီး, 11191, မြန်မာ
In the next tutorial, we will leran more how to geocode many addresses from Pandas Dataframe.