Geolocation

Libraries for geocoding addresses and working with latitudes and longitudes.

Addresses become coordinates with geopy, whose Python geolocation API covers many geocoding services. GeoPandas analyzes map data, and GeoDjango serves it.

How to choose:

  • Addresses to coordinates, and coordinates back to addresses: geopy
  • The distance between two points: geopy
  • Geographic data in tables and files, like shapefiles and GeoJSON: GeoPandas
  • Web apps that store and query geographic data: GeoDjango
  • A visitor's country or city from their IP address, in a Django project: GeoDjango
  • Building, encoding, and validating GeoJSON objects by hand: geojson

Listed in editorial order. Click a column header to re-sort the whole list.

Press / to search. Tags open their category. Click a row for details.

Search and filter

Results

Row number Tags
Python tools for geographic data (GeoSeries/GeoDataFrame) built on pandas.
geopandas/github.com/geopandas/geopandas /5,288 stars /
Python Geocoding Toolbox.
geopy/github.com/geopy/geopy /4,870 stars /
Python bindings and utilities for GeoJSON.
jazzband/github.com/jazzband/geojson /1,001 stars /
(part of Django) A world-class geographic web framework.
django/github.com/django/django /91,444 stars /

Geolocation guide

geopy is a client for geocoding services, not a service itself, and each service has its own terms of use, quotas, and pricing. Every geocoder has a geocode() method that turns an address into a location, and most also have reverse() for the other way around. For OpenStreetMap's Nominatim, set a user_agent that names your app and follow its usage policy. The policy bans heavy use, auto-complete, and systematic queries. It also asks you to cache results and show attribution, and it discourages bulk geocoding. To geocode a DataFrame, wrap the call in RateLimiter, which adds delays between requests and retries failed ones. Check first that your service allows bulk requests at all. For distances, geopy.distance.distance computes the geodesic distance between two points.

GeoPandas adds geometry columns to pandas, so you can do in Python what would otherwise take a spatial database such as PostGIS. Load a shapefile, GeoJSON, or GeoPackage with read_file(), which detects the file type and returns a GeoDataFrame, and write it back with to_file(). For a table of latitudes and longitudes, build the points with points_from_xy() and set crs="EPSG:4326". To geocode a column of addresses, GeoPandas calls geopy for you, so the geocoding service's terms still apply.

GeoDjango is a contrib module that ships with Django. It adds model fields for geometries, spatial queries to the ORM, and geometry editing to the admin. Its tutorial assumes you already know Django. Run it on PostGIS, which its docs recommend as the most mature and feature-rich open source spatial database. To load a shapefile, ogrinspect writes the model and a LayerMapping imports the rows. For IP geolocation, GeoIP2 looks up a country or city in a MaxMind or DB-IP database file you download.

geojson has a class for every object in the GeoJSON spec, and geojson.dumps() and geojson.loads() wrap the standard json functions to encode and decode them. Check an object with its is_valid property and errors() method. To encode your own classes the same way, give them a __geo_interface__, which GeoPandas' from_features() also accepts.

Latitude and longitude are angles, so measure distance and area in a projected coordinate system, in meters or feet. In GeoPandas, you always need one for those operations, so reproject with to_crs() first. GeoDjango calls choosing a geometry field's SRID an important decision, since projected systems ease distance calculations. geopy's distance works on an ellipsoidal model of the earth, so it takes latitudes and longitudes as they are.

Know a project that belongs here?

Tell us what it does and why it stands out.