Geohash2
Geohash2 is a Python module providing functions to encode and decode geographic coordinates (latitude and longitude) into Geohash strings and vice-versa. It is a Python 3-compatible fork of the original 'geohash' library, initially released in July 2017. The library appears to be in maintenance mode with no recent updates, but remains functional for its core purpose.
Warnings
- breaking This 'geohash2' package specifically addresses Python 3 compatibility issues found in the original 'geohash' library. If migrating from the original 'geohash' (version 1.0 or older), direct `import geohash` statements might fail due to Python 3 syntax differences.
- gotcha The `decode()` function returns latitude and longitude as strings, which may require explicit type conversion (e.g., `float()`) for mathematical operations. The `decode_exactly()` function returns floats, along with latitude and longitude error margins.
- gotcha Geohashes do not guarantee spatial proximity based on common prefixes across certain boundaries (e.g., the 180-degree meridian, the Equator, or poles). Locations very close physically might have entirely different geohash prefixes due to how the grid is subdivided.
Install
-
pip install geohash2
Imports
- encode
from geohash2 import encode
- decode
from geohash2 import decode
- decode_exactly
from geohash2 import decode_exactly
Quickstart
import geohash2
# Encode coordinates to a geohash
latitude = 42.6
longitude = -5.6
geohash_code = geohash2.encode(latitude, longitude)
print(f"Geohash for {latitude}, {longitude}: {geohash_code}")
# Encode with specified precision
geohash_precision_5 = geohash2.encode(latitude, longitude, precision=5)
print(f"Geohash (precision 5) for {latitude}, {longitude}: {geohash_precision_5}")
# Decode a geohash to coordinates (returns strings)
decoded_coords_str = geohash2.decode('ezs42')
print(f"Coordinate for Geohash ezs42: {decoded_coords_str}")
# Decode a geohash exactly (returns floats with error margins)
decoded_exact_coords = geohash2.decode_exactly('ezs42')
print(f"Exact coordinate for Geohash ezs42: {decoded_exact_coords}")