Easy Tutorial
❮ Connection Select Lists Lset ❯

Redis GEO

Redis GEO is primarily used to store geographic location information and perform operations on this stored information. This feature was introduced in Redis 3.2.

Redis GEO operation methods include:

geoadd

geoadd is used to store specified geographic spatial locations, allowing the addition of one or more longitude, latitude, and location name (member) to a specified key.

The syntax format for geoadd is as follows:

GEOADD key longitude latitude member [longitude latitude member ...]

In the following example, the key is Sicily, and Palermo and Catania are location names:

Example

redis> GEOADD Sicily 13.361389 38.115556 "Palermo" 15.087269 37.502669 "Catania"
(integer) 2
redis> GEODIST Sicily Palermo Catania
"166274.1516"
redis> GEORADIUS Sicily 15 37 100 km
1) "Catania"
redis> GEORADIUS Sicily 15 37 200 km
1) "Palermo"
2) "Catania"
redis>

geopos

geopos is used to return the positions (longitude and latitude) of all specified members from the given key. Non-existent members return nil.

The syntax format for geopos is as follows:

GEOPOS key member [member ...]

Example

redis> GEOADD Sicily 13.361389 38.115556 "Palermo" 15.087269 37.502669 "Catania"
(integer) 2
redis> GEOPOS Sicily Palermo Catania NonExisting
1) 1) "13.36138933897018433"
   2) "38.11555639549629859"
2) 1) "15.08726745843887329"
   2) "37.50266842333162032"
3) (nil)
redis>

geodist

geodist is used to return the distance between two specified locations.

The syntax format for geodist is as follows:

GEODIST key member1 member2 [m|km|ft|mi]

member1 and member2 are the two geographic locations.

The last distance unit parameter options are:

Example

redis> GEOADD Sicily 13.361389 38.115556 "Palermo" 15.087269 37.502669 "Catania"
(integer) 2
redis> GEODIST Sicily Palermo Catania
"166274.1516"
redis> GEODIST Sicily Palermo Catania km
"166.2742"
redis> GEODIST Sicily Palermo Catania mi
"103.3182"
redis> GEODIST Sicily Foo Bar
(nil)
redis>

georadius, georadiusbymember

georadius returns all location elements within a specified maximum distance from a given longitude and latitude.

georadiusbymember, like GEORADIUS, finds elements within a specified range, but the center point is determined by a given location element rather than by longitude and latitude.

The syntax format for georadius and georadiusbymember is as follows:

GEORADIUS key longitude latitude radius m|km|ft|mi [WITHCOORD] [WITHDIST] [WITHHASH] [COUNT count] [ASC|DESC] [STORE key] [STOREDIST key]

GEORADIUSBYMEMBER key member radius m|km|ft|mi [WITHCOORD] [WITHDIST] [WITHHASH] [COUNT count] [ASC|DESC] [STORE key] [STOREDIST key]

Parameter Explanation:

GEORADIUS Example:

redis> GEOADD Sicily 13.361389 38.115556 "Palermo" 15.087269 37.502669 "Catania"
(integer) 2
redis> GEORADIUS Sicily 15 37 200 km WITHDIST
1) 1) "Palermo"
   2) "190.4424"
2) 1) "Catania"
   2) "56.4413"
redis> GEORADIUS Sicily 15 37 200 km WITHCOORD
1) 1) "Palermo"
   2) 1) "13.36138933897018433"
      2) "38.11555639549629859"
2) 1) "Catania"
   2) 1) "15.08726745843887329"
      2) "37.50266842333162032"
redis> GEORADIUS Sicily 15 37 200 km WITHDIST WITHCOORD
1) 1) "Palermo"
   2) "190.4424"
   3) 1) "13.36138933897018433"
      2) "38.11555639549629859"
2) 1) "Catania"
   2) "56.4413"
   3) 1) "15.08726745843887329"
      2) "37.50266842333162032"
redis>

GEORADIUSBYMEMBER Example:

redis> GEOADD Sicily 13.583333 37.316667 "Agrigento"
(integer) 1
redis> GEOADD Sicily 13.361389 38.115556 "Palermo" 15.087269 37.502669 "Catania"
(integer) 2
redis> GEORADIUSBYMEMBER Sicily Agrigento 100 km
1) "Agrigento"
2) "Palermo"
redis>

GEOHASH

Redis GEO uses geohash to store geographic coordinates.

GEOHASH is used to retrieve the geohash values of one or more location elements.

GEOHASH syntax format is as follows:

GEOHASH key member [member ...]

Example:

redis> GEOADD Sicily 13.361389 38.115556 "Palermo" 15.087269 37.502669 "Catania"
(integer) 2
redis> GEOHASH Sicily Palermo Catania
1) "sqc8b49rny0"
2) "sqdtr74hyu0"
redis>
❮ Connection Select Lists Lset ❯