geopy

2.4.1 · active · verified Mon Apr 06

geopy is a Python client for numerous popular geocoding web services, including OpenStreetMap Nominatim, Google Geocoding API, and Bing Maps. It simplifies the process for Python developers to locate the coordinates of addresses, cities, countries, and landmarks globally, as well as perform reverse geocoding. The library is actively maintained, with its current stable version being 2.4.1, and releases occur regularly to add features and address issues. It supports Python versions 3.7 through 3.12.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use the Nominatim geocoder to convert an address string into geographical coordinates (latitude and longitude) and vice-versa. It highlights the necessity of providing a `user_agent` for Nominatim, as recommended by its usage policy, and safely retrieves it from an environment variable.

import os
from geopy.geocoders import Nominatim

# For Nominatim, a 'user_agent' is required.
# In a production environment, use a unique, descriptive string for your application.
geolocator = Nominatim(user_agent=os.environ.get('GEOPY_USER_AGENT', 'my_geocoding_app'))

# Geocode an address
address = "175 5th Avenue NYC"
location = geolocator.geocode(address)

if location:
    print(f"Address: {location.address}")
    print(f"Latitude: {location.latitude}, Longitude: {location.longitude}")
else:
    print(f"Could not find location for: {address}")

# Reverse geocode coordinates
coordinates = "52.509669, 13.376294" # Berlin
reversed_location = geolocator.reverse(coordinates)

if reversed_location:
    print(f"\nCoordinates: {coordinates}")
    print(f"Address: {reversed_location.address}")
else:
    print(f"Could not find address for: {coordinates}")

view raw JSON →