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.1.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.1.0-py3-none-any.whl (13.1 MB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: pg_nearest_city-0.1.0.tar.gz
  • Upload date:
  • Size: 13.2 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: pdm/2.9.3 CPython/3.12.3

File hashes

Hashes for pg_nearest_city-0.1.0.tar.gz
Algorithm Hash digest
SHA256 09ae565c52544ebbd13734e972af9aa1eb885ca817994540a9eefdc45fb4fac7
MD5 3bffd6fd2634a597945d2594d7635c80
BLAKE2b-256 acb4a263d8896a34ab1ef703f6f918dd920d8bde3b87f30908a2fd087725fed8

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pg_nearest_city-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 12376d55a02dc0439b305d48092ab77e62a594ac6b41aed60a6bb33bbdea8240
MD5 1a3ebede8d52fc8cb219385d0d32c1b5
BLAKE2b-256 f9e82f7580348cf0108f55c9d6cde625070d0e709aa98574e65cbd06e9a98a5a

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