Geohash is a public domain Geocoding system created by Gustavo Niemeyer in 2008. It is a simple way to encode latitudes and longitudes into short alphanumeric strings.
For example, the coordinate pair 28.6132,77.2291 (area surrounding the India Gate in New Delhi) can be represented in geohash as ttnfv2u
.
Each extra character added to the Geohash adds precision to the location represented by the Geohash.
Geohashing guarantees that points are spatially closer if their Geohashes share a longer prefix.
For example, Geohashes 2pxqm
v1 and 2pxqm
m5 are always spatially closer as they share the prefix 2pxqm
.
However, the reverse is not true: two points can be very close or nearby, but their geohashes may not share any prefix.
Geohash is a hierarchical spatial index. This means that to represent a point, the world is recursively divided into smaller and smaller grids, with each additional bit, until the desired precision is attained.
To create a Geohash for our point (shown as a Red dot), let’s first divide the world into two halves with a vertical line and give each half a binary value of either 0 or 1.
Now, we can divide that half again along the Equator and add the second bit. Then, repeat this subdivision, alternating between longitude and latitude, until the remaining area is within the desired precision.
At our desired precision, our Geohash will look like a long binary string:
01010101101010101010101010111
Finally, we encode the resulting binary sequence as alphanumeric characters (Base-32 alphabet encoding is used where characters can be 0 to 9 and A to Z, excluding “A”, “I”, “L” and “O”).
Each 5 bits is converted to one character that looks something like ttnfv2u
.
Representing Location- A Geohash is a simple way to represent and store a location in a database – it can also be shared on social media as urls since it is easier to share, remember, and store as compared to latitudes and longitudes.
Finding Neighbours- Using geohashes, you can efficiently find the nearest neighbors of a point through very simple string comparisons and efficient searching of indexes (since shared prefixes denote geographic proximity). Indexes are used to find nearby locations, and identify places of interest, shops, hotels in an area.
Geohashing is widely used to represent a location and is supported by popular databases such as MySQL. It is available in Redis as Redis Geohash. Geohashes are also supported by Google Firebase and AWS Dynamo database, to name a few.
Besides that, there are libraries available for all the major languages:
geohash-java is a popular library for Java
node-geohash for NodeJS
NGeohash is a popular Geohash library in C#