A simple, fast and accurate boundary based reverse geocoding library in python
Project description
Gazetteer
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
- 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 of 210+ countries
How to use gazetteer
Installation
pip install python-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
If the given coordinates are not found or found inside a water boundary like sea/ocean, it should return None instead of LocationBaseModel
How it works
Data Management
- Boundary Data is sourced from Geoboundaries
- Data is stored in two places,
sqlitedb and acsvfile - A simplified boundary is stored inside
sqlitedb in the format ofWKB(Well Known Library) - The metadata for locations is stored as
csv
Computation
- Uses
KDTreenearest neighbour algorithm fromscipyto 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
- Geoboundaries - A big shout out for making their amazing dataset free and open-source
- Ajay Thampi - A part of this implementation was inspired by his reverse_geocoder
License
This project is licensed under the GNU Lesser General Public License (LGPL v2.1). See the LICENSE file for details.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file python_gazetteer-0.1.1.tar.gz.
File metadata
- Download URL: python_gazetteer-0.1.1.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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
715d0c814907e6922e21b2f768f36c711a0641e9f4cf222ea779cc36dccd2ede
|
|
| MD5 |
ff45f09b05254fb4f2e74698bfa394a0
|
|
| BLAKE2b-256 |
a693d5a5642d23d36ebaa9d79153f8602c2698a807c8002d2a30f3c9fe342edd
|
File details
Details for the file python_gazetteer-0.1.1-py3-none-any.whl.
File metadata
- Download URL: python_gazetteer-0.1.1-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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b334efc8429a234f052817e917dba98bb93f67050315360acce32532689493e5
|
|
| MD5 |
171af4f950ad40f701676144af8324f8
|
|
| BLAKE2b-256 |
cf257a9ef5d27c15507d29c2eb1e0d8406a8a70fea007f59c6d483b4241dbc36
|