Converting Latitude and Longitude into Geohash using Python

ATKims
2 min readJan 8, 2023

There are several reasons why you might want to convert latitude and longitude into geohash:

  1. Geohashes are a convenient way to represent geographic coordinates as a single string, which can be useful for storing or indexing location data.
  2. Geohashes are hierarchical, meaning that longer geohashes provide more precision and shorter geohashes provide less precision. This can be useful when you want to group locations together based on their proximity to each other.
  3. Geohashes can be used to efficiently query spatial data stored in databases or other data storage systems.
  4. Geohashes can be used to perform fast proximity searches, such as finding all points within a certain radius of a given point.

To convert latitude and longitude coordinates into a geohash, you can use the geohash library in Python.

Here is an example of how to use the geohash library to convert a pair of latitude and longitude coordinates into a geohash

import geohash
# Set the latitude and longitude coordinates
latitude = 37.421542
longitude = -122.085589
# Encode the coordinates into a geohash
geohash_code = geohash.encode(latitude, longitude, precision=12)
print(geohash_code)

The output of this code will be a string containing the geohash code for the given latitude and longitude coordinates. The precision of the geohash code can be adjusted by changing the value of the precision parameter. A higher precision will result in a longer geohash code, but will also provide a higher level of accuracy.

You can also use the geohash library to decode a geohash code back into latitude and longitude coordinates. Here is an example:

# Set the geohash code
geohash_code = '9q8yyzzzzzzz'
# Decode the geohash into latitude and longitude coordinates
latitude, longitude = geohash.decode(geohash_code)
print(latitude, longitude)

The output of this code will be the latitude and longitude coordinates that correspond to the given geohash code.

--

--

ATKims

Director of a data agency specialising in public sector work with many years in leadership. I've MSCs in Computer Science and Criminology.