Skip to main content

A simple, fast and accurate boundary based reverse geocoding library in python

Project description

Gazetteer

CI Python License Documentation Status

A simple, fully offline, accurate boundary based reverse geocoding library in python

Docs - https://gazetteer.readthedocs.io/en/latest/

Reverse Geocoding

It is a process of finding valid address out of coordinates (lat, long), that includes county,state,country etc..

Advantage over other libraries

  • This is a boundary based geocoding library which provides significantly more accurate result than the point based computing counterparts
  • Provide data upto 3 administrative divisions
    • ADM0 - Country
    • ADM1 - State
    • ADM2/ADM3 - Towns, Cities, Regions (boundaries which does not have ADM3, ADM2 (districts) will be provided)
  • Boundary validation is computed on the fly, which makes it extremely fast and efficient
  • No max usage, rate limits since everything runs on yours device fully offline
  • No boundary ambiguity or invalid address near border regions, since it is validated by boundary itself
  • Covers over 145,000+ boundaries

How to use gazetteer

Installation

pip install gazetteer

Usage

from gazetteer import Gazetteer #import the library
gz = Gazetteer() #gazetteer instance
coordinates = [(-74.0060,40.7128),(76.2673,9.9312)] # list of tuples, in order longitude,latitude
for data in gz.search(coordinates): # gz.search() returns a generator
    print(data) # GeocoderResultBaseModel

Result

lat=40.7128 lon=-74.006 result=LocationBaseModel(lat=40.77488420403987, lon=-73.97077035729478, name='New York', admin1='United States', admin2='New York')
lat=9.9312 lon=76.2673 result=LocationBaseModel(lat=9.96268442863286, lon=76.32995967426373, name='Kanayannur', admin1='India', admin2='Kerala')

Return Base Model

from pydantic import BaseModel, Field
class LocationBaseModel(BaseModel):
    lat: float = Field(..., description="Centroid latitude of the nearest neighbor")
    lon: float = Field(..., description="Centroid longitude of the nearest neighbor")
    name: str = Field(..., description="Name of the nearest ADM3(eg towns,cities)/ADM2(eg districts) neighbour")
    admin1: str = Field(..., description="Name of the primary administrative division ADM0 (country)")
    admin2: str = Field(
        ...,
        description="Name of the secondary administrative division ADM1 (eg: state)",
    )


class GeocoderResultBaseModel(BaseModel):
    lat: float = Field(..., description="Given latitude")
    lon: float = Field(..., description="Given longitude")
    result: LocationBaseModel

For large dataset (100000+) coordinate pairs use

from gazetteer import Gazetteer
gz = Gazetteer(mode=2)

This switches to multiprocessing mode, utilizing all cores in your system

How it works

Data Management

  • Boundary Data is sourced from Geoboundaries
  • Data is stored in two places, sqlite db and a csv file
  • A simplified boundary is stored inside sqlite db in the format of WKB(Well Known Library)
  • The metadata for locations is stored as csv

Computation

  • Uses KDTree nearest neighbour algorithm from scipy to find the closest boundary point
  • Validates the nearest neighbour using the boundary provided in the sqlite

Note: Regions that doesn't have an available ADM3 boundary will return ADM2 instead as the nearest neighbour

Acknowledgements

License

This project is licensed under the GNU Lesser General Public License (LGPL v2.1). See the LICENSE file for details.

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

python_gazetteer-0.1.0.tar.gz (66.9 MB view details)

Uploaded Source

Built Distribution

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

python_gazetteer-0.1.0-py3-none-any.whl (66.8 MB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: python_gazetteer-0.1.0.tar.gz
  • Upload date:
  • Size: 66.9 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.9.18 {"installer":{"name":"uv","version":"0.9.18","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for python_gazetteer-0.1.0.tar.gz
Algorithm Hash digest
SHA256 93ea078c8e1d626c2efee31ee185e5426030bceab5eed170627f68c501768a9b
MD5 e1661d195c259ec0cca411bf049baea6
BLAKE2b-256 bbac2fc83ad6e81698842eaaf96f45bc8a0d08cc9864ac2d21af31221ab8f19e

See more details on using hashes here.

File details

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

File metadata

  • Download URL: python_gazetteer-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 66.8 MB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.9.18 {"installer":{"name":"uv","version":"0.9.18","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for python_gazetteer-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 506800c016316c411a2c726c771f3b221445b45f09a34cd47c7d9eb6bd8431c9
MD5 1e4c4a3e99175760321f84ac296a01e7
BLAKE2b-256 3390f7798ac03d477674921754bd4ea8ba1e70befa451f9572785fd396cda891

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