Given a geopoint, find the nearest city using PostGIS (reverse geocode).
📖 Documentation: https://hotosm.github.io/pg-nearest-city/
🖥️ Source Code: https://github.com/hotosm/pg-nearest-city
We wanted a basic reverse geocoder for web APIs that already have a PostGIS connection: given a point, return the nearest city and country.
There are good alternatives:
- reverse-geocoder is the original Python package, using a K-D tree. It's a bit outdated now, and keeps around 260MB in memory, which is a lot for a web server.
- reverse_geocode is a great revamp of that package, and likely the best choice if you don't have PostGIS.
Compared to these, pg-nearest-city:
- Is around 450x faster (45ms down to 0.1ms).
- Uses around 8MB of memory, instead of 260MB.
- Has a one-time cost of 30s to 4 minutes to load the data into your database. You could do this at web server startup.
Note
We don't cover web services like Nominatim here. An offline lookup has lower latency, avoids load on free services (particularly in automated tests), and means one less external dependency.
- Cities come from GeoNames cities500 (population over 500).
- Country boundaries come from geoBoundaries CGAZ.
- Our own shapes replace CGAZ for territories and disputed areas.
- Geometry is simplified and subdivided, to keep the package small and queries fast.
- A query finds the nearest country polygon within 5km, then the nearest city in that country.
This is a small offline lookup, so treat results as approximate:
- Simplified borders can shift by up to around 100m. A point right on a border may return the neighbouring country.
- You get the nearest city in the matched country. That may not be the place containing the point, and could be far away in sparsely populated areas.
- Points over 5km from any country return
None. That covers open sea, plus some remote atolls and very small islands. - Disputed areas use one set of boundaries, which you may see drawn differently elsewhere. Cities keep their GeoNames country, so the nearest city can be distant there. For example, a point in Crimea returns Russia, with the nearest Russian city across the Kerch Strait.
As a guide, we agree with Nominatim on around 90% of a 312 point test set. Many
of the differences are territories, where we return Hong Kong (HK) and
Nominatim returns China (CN).
pip install pg-nearest-cityNote
Coordinates are always (lon, lat), matching PostGIS.
from pg_nearest_city import AsyncNearestCity
# Existing code to get a db connection, say from an API endpoint
db = await get_db_connection()
async with AsyncNearestCity(db) as geocoder:
location = await geocoder.query(-74.0060, 40.7128)
print(location.city)
# "New York City"
print(location.country)
# "US"from pg_nearest_city import NearestCity
# Existing code to get a db connection, say from an API endpoint
db = get_db_connection()
with NearestCity(db) as geocoder:
location = geocoder.query(-74.0060, 40.7128)
print(location.city)
# "New York City"
print(location.country)
# "US"If you don't already have a psycopg connection, pass a DbConfig:
from pg_nearest_city import DbConfig, AsyncNearestCity
db_config = DbConfig(
dbname="db1",
user="user1",
password="pass1",
host="localhost",
port="5432",
)
async with AsyncNearestCity(db_config) as geocoder:
location = await geocoder.query(-74.0060, 40.7128)Or set these environment variables, and use AsyncNearestCity() with no
arguments:
PGNEAREST_DB_NAME=cities
PGNEAREST_DB_USER=cities
PGNEAREST_DB_PASSWORD=somepassword
PGNEAREST_DB_HOST=localhost
PGNEAREST_DB_PORT=5432docker compose run --rm test pytestOr locally, with a running PostGIS instance:
PGNEAREST_DB_USER=myuser PGNEAREST_DB_NAME=postgres uv run pytest tests/ -vYou only need this for new GeoNames or CGAZ data, or to change our boundaries.
pgnearest-load downloads the sources, builds everything in PostGIS, and
exports compressed CSVs. It takes around 20 minutes.
uv run pgnearest-load --db-name postgres --db-user myuser --output-dir ./output
# Full rebuild, dropping all tables first
uv run pgnearest-load ... --clean| Flag | Description |
|---|---|
--compression {auto,gzip,bz2,xz,zstd} |
Compression for exported files |
--no-cache |
Download to temp dir, no persist |
--clean |
Drop all project tables first |
--skip-steps / --only-steps |
Step prefixes to skip or isolate |
--list-steps |
Print pipeline steps and exit |
--country |
Filter to a country (e.g. IT) |
The output files are:
country.csv.<ext>: subdivided country polygons.geocoding.csv.<ext>: cities, with their country and coordinates.cities_500_simple.txt.<ext>: simplified city data.
pg_nearest_city/db/boundary_overrides.csv.xz holds the shapes we use instead
of CGAZ: territories, small enclaves and disputed areas. Each row is an alpha-2
code and a WKB MultiPolygon. Edit the file and rebuild to change one.
Country names are in pg_nearest_city/db/country_names.csv.
The bundled data is built from:
- geoBoundaries (CC BY 4.0). Runfola D et al. (2020) geoBoundaries: A global database of political administrative boundaries. PLoS ONE 15(4): e0231866.
- GeoNames (CC BY 4.0).
- Natural Earth (public domain).
- © OpenStreetMap contributors (ODbL), via Overpass and Overture Maps.
