Python Geohash (Legacy)
This is a legacy version (0.8.5) of a Python library for fast and accurate geohashing, providing functions to encode and decode geohashes to and from latitude/longitude coordinates. It originated from a Google Code project and was last updated in 2014. For actively maintained geohashing functionality, consider the 'pygeohash' library.
Warnings
- breaking This library (python-geohash 0.8.5) is largely unmaintained since its last update in 2014. Users are strongly advised to consider more actively developed and modern alternatives like the 'pygeohash' library (lowercase 'p' in package name) for current projects, which offers Python 3 support, zero dependencies, and performance optimizations.
- gotcha Installation issues with the optional C extension are common on modern operating systems (e.g., macOS with newer Python versions), potentially leading to `command 'g++' failed with exit status 1` errors during `pip install`. The library can fall back to a pure Python implementation, but this might result in a silent performance degradation or complete installation failure.
- gotcha The `decode()` function returns raw float values for latitude and longitude. Some other geohash libraries might return a tuple with error margins or a custom object, which could lead to incompatibility if migrating between libraries or expecting a specific structured output.
- gotcha Geohashes, by nature, can sometimes represent geographically close points with different prefixes if they fall on opposite sides of a geohash grid boundary. This can affect proximity search logic based solely on prefix matching.
Install
-
pip install python-geohash
Imports
- encode
import geohash; geohash.encode(lat, lng)
- decode
import geohash; geohash.decode(geohash_string)
- encode_uint64
import geohash; geohash.encode_uint64(lat, lng)
- decode_uint64
import geohash; geohash.decode_uint64(uint64_hash)
Quickstart
import geohash
# Encode coordinates to a geohash string with default precision (12 characters)
latitude, longitude = 42.6, -5.6
geohash_string = geohash.encode(latitude, longitude)
print(f"Encoded geohash: {geohash_string}")
# Encode with custom precision
short_geohash = geohash.encode(latitude, longitude, precision=5)
print(f"Short geohash (precision 5): {short_geohash}")
# Decode a geohash string back to coordinates
decoded_lat, decoded_lng = geohash.decode(geohash_string)
print(f"Decoded coordinates: ({decoded_lat}, {decoded_lng})")
# Example with uint64 representation
uint64_hash = geohash.encode_uint64(latitude, longitude)
print(f"Encoded uint64 geohash: {uint64_hash}")
decoded_lat_uint64, decoded_lng_uint64 = geohash.decode_uint64(uint64_hash)
print(f"Decoded uint64 coordinates: ({decoded_lat_uint64}, {decoded_lng_uint64})")