polygon-geohasher
raw JSON → 0.0.2 verified Fri May 01 auth: no python
A lightweight wrapper over Shapely that returns the set of geohashes that form a Polygon. Current version is 0.0.2. The library has low release cadence (last release in 2023). It converts polygons to geohashes and vice versa, with optimizations like bbox gating and prepared geometry predicates.
pip install polygon-geohasher Common errors
error ImportError: cannot import name 'polygon_to_geohashes' from 'polygon_geohasher' ↓
cause Older version (<0.0.2) did not expose top-level imports; you need to import from polygon_geohasher.polygon_geohasher
fix
Upgrade to latest version: pip install --upgrade polygon-geohasher, or use old import: from polygon_geohasher.polygon_geohasher import polygon_to_geohashes
error TypeError: 'Polygon' object is not iterable ↓
cause Passing a single polygon instead of a list to geohashes_to_polygon; this function expects a list/iterable of geohash strings.
fix
Pass a list: geohashes_to_polygon([geohash_string])
error AttributeError: module 'polygon_geohasher' has no attribute 'polygon_to_geohashes' ↓
cause Using very old version (before 0.0.2) or incorrectly installed package.
fix
pip install --upgrade polygon-geohasher and check import path: from polygon_geohasher import polygon_to_geohashes
Warnings
gotcha The 'precision' parameter controls geohash length; higher precision yields smaller, more geohashes. Default is 5. Using too high precision (e.g., 12) may return an extremely large set and cause memory issues. ↓
fix Choose precision based on your polygon size; typical values: 5 (approx 4.9km x 4.9km), 6 (1.2km), 7 (152m).
deprecated The function 'polygon_to_geohashes' was renamed in 0.0.2? No, but 'cascaded_union' was replaced by 'unary_union' internally. User code using 'polygon_to_geohashes' is safe. ↓
fix No action needed, but if you relied on internal behavior, note that unary_union is used instead of cascaded_union.
gotcha If input polygon is invalid (e.g., self-intersecting), the conversion may raise a Shapely exception or produce unexpected results. The library does not validate polygons. ↓
fix Use shapely.validation.make_valid() or buffer(0) to fix invalid geometries before passing to polygon_to_geohashes.
Imports
- polygon_to_geohashes wrong
from polygon_geohasher.polygon_geohasher import polygon_to_geohashescorrectfrom polygon_geohasher import polygon_to_geohashes - geohash_to_polygon
from polygon_geohasher import geohash_to_polygon - geohashes_to_polygon
from polygon_geohasher import geohashes_to_polygon
Quickstart
from shapely.geometry import Polygon
from polygon_geohasher import polygon_to_geohashes, geohash_to_polygon, geohashes_to_polygon
# Create a simple polygon (a square near London)
polygon = Polygon([(-0.1, 51.5), (-0.1, 51.6), (0.1, 51.6), (0.1, 51.5)])
# Convert polygon to geohashes (default precision 5)
geohashes = polygon_to_geohashes(polygon, precision=5)
print('Geohashes:', geohashes[:5], '...') # prints first 5
# Convert single geohash to polygon (bounding box)
if geohashes:
poly = geohash_to_polygon(geohashes[0])
print('Polygon from geohash:', poly.wkt)
# Convert list of geohashes to polygon
poly_union = geohashes_to_polygon(list(geohashes))
print('Union polygon:', poly_union.wkt)