Python module for interacting with geohashes
Actively maintained. Last release 7 days ago, commits in 12 of the last 26 weeks. A safe choice.
pip install pygeohashPyGeoHash
A typed Python library for working with geohashes, with a compiled C core and prebuilt wheels for mainstream Linux, macOS, and Windows environments.
Full docs at pygeohash.mcginniscommawill.com
What is PyGeoHash?
PyGeoHash is a Python module that provides functions for encoding and decoding geohashes to and from latitude and longitude coordinates, along with utilities for performing calculations and approximations with them.
It was originally based on Leonard Norrgård's geohash module, but now adds more functionality while supporting Python 3, and is optimized for performance.
Why PyGeoHash?
- Installs across mainstream platforms: Prebuilt wheels cover Linux x86_64 and arm64, macOS Intel and Apple Silicon, and Windows. Alpine/musl users build from source.
- Compiled core: Geohash encoding and decoding run through the bundled CPython C extension.
- Typed package: PyGeoHash ships a
py.typedmarker and type stubs for the C extension, with strict mypy checks in CI. - Zero runtime dependencies in the base install: Mapping features remain
available separately through
pygeohash[viz]. - Geohash utilities beyond encode/decode: Work with neighbors, bounding boxes, approximate distances, statistical helpers, and optional maps.
- Radius coverage:
geohashes_in_radius(lat, lon, radius_m, precision=6)selects proximity candidate cells, including across the antimeridian and near poles (new in 3.6.0). - Map-tile interoperability: Convert geohashes to tile, quadkey, and integer forms (new in 3.6.0).
Installation
# Basic installation
pip install pygeohash
# With visualization support
pip install pygeohash[viz]
# With the cross-library benchmark comparison dependencies
pip install pygeohash[benchmark]
Quick Start
import pygeohash as pgh
# Encode coordinates to geohash
geohash = pgh.encode(latitude=42.6, longitude=-5.6)
print(geohash) # 'ezs42e44yx96'
# Control precision
short_geohash = pgh.encode(latitude=42.6, longitude=-5.6, precision=5)
print(short_geohash) # 'ezs42'
# Decode geohash to coordinates
lat, lng = pgh.decode(geohash="ezs42")
print(lat, lng) # 42.60498046875 -5.60302734375
# Encode or decode many items in one call (list, tuple, array.array, NumPy, generators)
geohashes = pgh.encode_many([42.6, 57.64911], [-5.6, 10.40744], precision=5)
print(geohashes) # ['ezs42', 'u4prs']
points = pgh.decode_many(geohashes) # list of LatLong, same as [pgh.decode(g) for g in geohashes]
# Calculate approximate distance between geohashes (in meters)
distance = pgh.geohash_approximate_distance(geohash_1="bcd3u", geohash_2="bc83n")
print(distance) # 625441
# Get adjacent geohash
adjacent = pgh.get_adjacent(geohash="kd3ybyu", direction="right")
print(adjacent) # 'kd3ybyv'