Redis GEO
Redis GEO is mainly used to store geographic location information and operate on the stored information. This feature was added in Redis version 3.2.
Redis GEO operation methods include:
- geoadd: Add the coordinates of a geographic location.
- geopos: Get the coordinates of a geographic location.
- geodist: Calculate the distance between two locations.
- georadius: Get the set of geographic locations within a specified range based on the longitude and latitude coordinates given by the user.
- georadiusbymember: Get the set of geographic locations within a specified range based on a location stored in the location set.
- geohash: Return the geohash value of one or more location objects.
geoadd
geoadd is used to store specified geospatial positions. It can add one or more longitudes, latitudes, and location names (members) to the specified key.
The syntax format of 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
(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 names (members) from the given key. Non-existent ones return nil.
The syntax format of geopos is as follows:
GEOPOS key member [member ...]
Example
(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 given locations.
The syntax format of geodist is as follows:
GEODIST key member1 member2 [m|km|ft|mi]
member1 and member2 are the two geographic locations.
Explanation of the last distance unit parameter:
- m: meters, default unit.
- km: kilometers.
- mi: miles.
- ft: feet. Calculate the distance between Palermo and Catania:
Example
(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 takes the given longitude and latitude as the center, and returns all location elements in the key whose distance from the center does not exceed the given maximum distance.
georadiusbymember is the same as the GEORADIUS command; both can find elements located within a specified range. However, the center point of georadiusbymember is determined by the given location element, rather than using longitude and latitude to determine the center point.
The syntax formats of georadius and georadiusbymember are 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 description:
- m: meters, default unit.
- km: kilometers.
- mi: miles.
- ft: feet.
- WITHDIST: While returning location elements, also return the distance between the location element and the center.
- WITHCOORD: Also return the longitude and latitude of the location element.
- WITHHASH: Return the sorted set score of the location element after original geohash encoding, in the form of a 52-bit signed integer. This option is mainly used for low-level applications or debugging, and its practical use is limited.
- COUNT limits the number of records returned.
- ASC: Sort search results from near to far by distance.
- DESC: Sort search results from far to near.
georadius example:
Example
(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:
Example
(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 save the coordinates of geographic locations.
geohash is used to get the geohash value of one or more location elements.
The syntax format of geohash is as follows:
GEOHASH key member [member ...]
Example:
Example
(integer) 2
redis> GEOHASH Sicily Palermo Catania
1) "sqc8b49rny0"
2) "sqdtr74hyu0"
redis>