Skip to main content

Given a geopoint, find the nearest city using PostGIS (reverse geocode).

Project description

Simple PostGIS Reverse Geocoder

HOT

Given a geopoint, find the nearest city using PostGIS (reverse geocode).

Publish Docs Publish Test Package version Downloads License


📖 Documentation: https://hotosm.github.io/pg-nearest-city/

🖥️ Source Code: https://github.com/hotosm/pg-nearest-city


Why do we need this?

This package was developed primarily as a basic reverse geocoder for use within web frameworks (APIs) that have an existing PostGIS connection to utilise.

  • The reverse geocoding package in Python here is probably the original and canonincal implementation using K-D tree.
    • However, it's a bit outdated now, with numerous unattended pull requests and uses an unfavourable multiprocessing-based approach.
  • The package here is an excellent revamp of the package above, an likely the best choice in many scenarios.

The K-D tree implementation in Python is performant (see benchmarks) and an excellent choice for scripts.

However, it does leave a large memory footprint of approximately 160Mb to load the K-D tree in memory (see benchmarks).

Once computed, the K-D tree remains in memory! This is an unacceptable compromise for a web server, for such a small amount of functionality, particularly if the web server is run via a container orchestrator as replicas with minimal memory.

As we already have a Postgres database running alongside our webserver, an approach to simply query via pre-loaded data via PostGIS is much more memory efficient (~2Mb) and has an acceptable performance penalty (see benchmarks).

[!NOTE] We don't discuss web based geocoding services here, such as Nominatim, as simple offline reverse-geocoding has two purposes:

  • Reduced latency, when very precise locations are not required.
  • Reduced load on free services such as Nominatim (particularly when running in automated tests frequently).

Priorities

  • Lightweight package size.
  • Minimal memory footprint.
  • Reasonably good performance.

How This Package Works

  • geonames.org data.
  • Voronoi polygons based on geopoints.
  • Gzipped data bundled with package.
  • Query the Voronois.

Benchmarks

  • todo

Testing

Run the tests with:

docker compose run --rm code pytest

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pg_nearest_city-0.0.0.tar.gz (13.2 MB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pg_nearest_city-0.0.0-py3-none-any.whl (13.1 MB view details)

Uploaded Python 3

File details

Details for the file pg_nearest_city-0.0.0.tar.gz.

File metadata

  • Download URL: pg_nearest_city-0.0.0.tar.gz
  • Upload date:
  • Size: 13.2 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.5.18

File hashes

Hashes for pg_nearest_city-0.0.0.tar.gz
Algorithm Hash digest
SHA256 5d163f37e618497a6ec684102cf8f8bf9378e0ef6a2794f3f0967a82af990809
MD5 671d9e6fd8c802776286ac8927383772
BLAKE2b-256 b3b2c1154d3f1eadb47ee5fff2f766cc98fbc439d1db276898a9b65497a29d16

See more details on using hashes here.

File details

Details for the file pg_nearest_city-0.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for pg_nearest_city-0.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 445b73dffed1fb6d135f3ed14fec97924aa7e70a1093e9afeea17a25c54f608e
MD5 27b5a5baa7c215fff233e0192348a1fc
BLAKE2b-256 b15cc1440b3c1651bbcc553e0e431f373b328e6f265e0465f19addfbf0039857

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page