Pygeohash

3.2.2 · active · verified Fri Apr 10

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 is a simple, lightweight, and dependency-free library, with core operations implemented in C for high performance. The current version is 3.2.2, and it maintains an active release cadence, having undergone a major rewrite to v3.0.0 in early 2025, focusing on performance enhancements and a license change, followed by several minor updates.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to encode latitude/longitude coordinates into geohashes, control precision, decode geohashes back to coordinates, calculate approximate distances between geohashes, and find adjacent geohashes using the `pygeohash` library.

import pygeohash as pgh

# Encode coordinates to a geohash string
latitude = 42.6
longitude = -5.6
geohash_full = pgh.encode(latitude, longitude)
print(f"Full geohash: {geohash_full}") # e.g., 'ezs42e44yx96'

# Encode with custom precision (e.g., 5 characters)
geohash_short = pgh.encode(latitude, longitude, precision=5)
print(f"Short geohash: {geohash_short}") # e.g., 'ezs42'

# Decode a geohash string back to coordinates
decoded_lat, decoded_lng = pgh.decode(geohash_short)
print(f"Decoded: Latitude={decoded_lat}, Longitude={decoded_lng}") # e.g., Latitude=42.6, Longitude=-5.6

# Calculate approximate distance between two geohashes (in meters)
distance = pgh.geohash_approximate_distance('bcd3u', 'bc83n')
print(f"Approximate distance: {distance} meters") # e.g., 625441

# Find an adjacent geohash
adjacent_geohash = pgh.get_adjacent(geohash='kd3ybyu', direction='right')
print(f"Adjacent geohash (right): {adjacent_geohash}") # e.g., 'kd3ybyv'

view raw JSON →