
Geocoding is the process of converting an address into latitude and longitude coordinates. Reverse geocoding is the process of converting latitude and longitude coordinates into an address. In Python, there are several libraries available to perform geocoding and reverse geocoding, including Geopy and Google Maps API. In this tutorial, we’ll be using Geopy to geocode and reverse geocode addresses using the OpenStreetMap Nominatim API.
Step 1: Installing Geopy
The first step is to install the Geopy library. You can do this using conda
1 2 3 |
(base) geoknight@pop-os:~$conda create -n spatial-dev.guru python=3.10 (base) geoknight@pop-os:~$conda activate spatial-dev.guru (spatial-dev.guru) geoknight@pop-os:~$conda install -c conda-forge geopy |
Step 2: Importing the Geopy Library
Next, we need to import the Geopy library in our Python code:
1 2 |
from geopy.geocoders import Nominatim |
We’re using the Nominatim geocoder, which is an OpenStreetMap service that can be used for geocoding and reverse geocoding addresses.
Step 3: Creating a Geocoder Object
To use the Nominatim geocoder, we need to create a geocoder object. We can do this by calling the Nominatim function and passing a user agent string:
1 2 |
geolocator = Nominatim(user_agent="my_app") |
You can replace “my_app” with any string that identifies your application.
Step 4: Geocoding an Address
Now that we have a geocoder object, we can use it to geocode an address. We can do this by calling the geocode function and passing an address string:
1 2 |
location = geolocator.geocode("1600 Pennsylvania Ave NW, Washington, DC 20500") |
This will return a Location object that contains information about the geocoded address, including its latitude and longitude:
1 2 |
print(location.latitude, location.longitude) |
Output:
1 2 |
38.8976633 -77.0365739 |
Step 5: Reverse Geocoding
To perform reverse geocoding, we can call the reverse function and pass latitude and longitude coordinates:
1 2 |
address = geolocator.reverse("38.8976633, -77.0365739") |
This will return a Location object that contains information about the reverse geocoded address, including its address string:
1 2 |
print(address.address) |
Output:
1 2 |
The White House, 1600, Pennsylvania Avenue Northwest, Golden Triangle, Washington, District of Columbia, 20500, United States of America |
Step 6: Handling Errors
Sometimes, the geocoder may not be able to find a matching address or coordinates. In this case, the geocode or reverse function will return None. To handle this case, we can check if the location or address variable is None before accessing its latitude, longitude, or address:
1 2 3 4 5 6 7 8 9 10 |
if location is not None: print(location.latitude, location.longitude) else: print("Address not found") if address is not None: print(address.address) else: print("Coordinates not found") |
This will print “Address not found” or “Coordinates not found” if the geocoder is unable to find a matching address or coordinates.
Complete Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
from geopy.geocoders import Nominatim # Creating a geocoder object geolocator = Nominatim(user_agent="my_app") # Geocoding an address location = geolocator.geocode("1600 Pennsylvania Ave NW, Washington, DC 20500") if location is not None: print(location.latitude, location.longitude) else: print("Address not found") # Reverse geocoding coordinates address = geolocator.reverse("38.8976633, -77.0365739") if address is not None: print(address.address) else: print("Coordinates not found") |