Redis GEOHASH – How to get Geohash string of multiple members of geospatial value

5 years ago Lalit Bhagtani 0

In this tutorial, we will learn about how to get a Geohash string of one or more elements of a geospatial value stored at the key. For this we will use a Redis GEOHASH command.

GEOHASH Command

This command is used to return a valid Geohash string of one or more specified element in the geospatial value stored at a key. A geospatial value is represented by the sorted set value, which is populated by using GEOADD command.

Redis represents positions ( longitude, latitude ) of geospatial element by using a variation of Geohash technique where latitude and longitude bits are interleaved in order to form an unique 52 bit integer. The encoding is also different compared to the standard because the initial min and max coordinates used during the encoding and decoding process are different. However, this command returns a standard Geohash string value.

A standard Geohash string has the following properties :-

  1. It consists of 11 characters.
  2. It is possible to use them in geohash.org.
  3. Strings with a similar prefix are nearby, but the contrary is not true, it is possible that strings with different prefixes are nearby too.
  4. They can be shortened removing characters from the right. It will lose precision but will still point to the same area.

An array of nil is returned when key does not exist and error is returned when key exist but value stored at the key is not of sorted set datatype which is populated using GEOADD command.

The syntax of redis GEOHASH command is as follows :-

Syntax :-

redis host:post> GEOHASH <keyname> <member> [member]

Output :- 

- (array) value, representing the list of Geohash string.
- Error, if key exist and value stored at the key is not a sorted set populated using GEOADD command.

Example :-

Redis GEOHASH

References :-

  1. GEOHASH Command Docs

That’s all for how to get a Geohash string of one or more elements of a geospatial value stored in redis datastore. If you liked it, please share your thoughts in comments section and share it with others too.

<- Geo Commands