Install & Compatibility
Where this runs
tested against v1.1 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
muslpy 3.10–3.95 runs
installs and imports cleanly · install 0.0s · import 0.000s · 23.5MB
glibcpy 3.10–3.95 runs
installs and imports cleanly · install 2.7s · import 0.000s · 24MB
21MB installed
● package 21MB
Code
Verified usage
Verified import paths — ran on the pinned version, not inferred.
encode
✓ from geohash2 import encode
Function to encode latitude and longitude into a geohash string.
decode
✓ from geohash2 import decode
Function to decode a geohash string into a (latitude, longitude) tuple of strings.
decode_exactly
✓ from geohash2 import decode_exactly
Function to decode a geohash string into an exact (latitude, longitude, lat_error, lon_error) tuple of floats.
Demonstrates encoding latitude and longitude into geohash strings, including specifying precision. Also shows decoding a geohash back into coordinates, with options for approximate string output or exact float output with error margins.
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}")
Debug
Known issues
breakingThis '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.fixEnsure you are installing 'geohash2' and using `import geohash2` or `from geohash2 import ...` instead of the older 'geohash' package and its import patterns.
affects: N/A (issue with original 'geohash' package, 'geohash2' fixes it)
gotchaThe `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.fixUse `float(latitude_str)` and `float(longitude_str)` for numeric calculations after using `decode()`, or use `decode_exactly()` if float output and error margins are needed directly.
affects: 1.1
gotchaGeohashes 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.fixWhen performing proximity searches or calculating distances, do not solely rely on geohash prefix matching for edge cases. Consider using more robust spatial indexing methods or calculating actual distances (e.g., Haversine formula) for nearby points.
affects: All (inherent to Geohash algorithm)
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'geohash2'
The `geohash2` library has not been installed in the Python environment.
fixInstall the library using pip: `pip install geohash2`
ImportError: No module named 'geohash'
This often occurs when `geohash2` is installed, but the user attempts to import the original, Python 2-centric `geohash` library or uses `import geohash` instead of `import geohash2`.
fixEnsure you are installing `geohash2` and importing it correctly: `import geohash2` or `from geohash2 import encode, decode`.
AttributeError: 'module' object has no attribute 'bbox'
The `geohash2` library does not directly expose a `bbox` function. This error might arise when migrating from other geohash libraries that do have such a function, or if attempting to use a method not present in `geohash2`'s API.
fixFor bounding box functionality, you might need to implement it manually based on `decode_exactly` or use a different geospatial library that provides this feature, like `pygeodesy`'s `bounds` function if a similar module is used. Alternatively, if trying to get the cell's center and error margins, use `geohash2.decode_exactly()`.
TypeError: can't multiply sequence by non-int of type 'float' (or similar errors when performing math on decoded coordinates)
The `geohash2.decode()` function returns latitude and longitude as strings, not floats. Attempting mathematical operations directly on these string values will lead to type errors.
fixConvert the returned string coordinates to floats before performing any mathematical operations: `lat_str, lon_str = geohash2.decode('ezs42'); latitude = float(lat_str); longitude = float(lon_str)`. Alternatively, use `geohash2.decode_exactly()` which returns float values directly along with error margins: `latitude, longitude, lat_error, lon_error = geohash2.decode_exactly('ezs42')`. Upgrade
Version history
1.1latest on PyPI · released Jul 6, 2017
Audit
Dependencies
No dependency data recorded yet.