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.
The Redis GEO operations are:
- geoadd: adds the coordinates of a geographic location.
- geopos: gets the coordinates of a geographic location.
- geodist: calculates the distance between two locations.
- georadius: gets a collection of geographic locations within a specified range based on the longitude and latitude coordinates given by the user.
- georadiusbymember: gets a collection of geographic locations within a specified range based on a location stored in the location set.
- geohash: returns the geohash value of one or more location objects.
geoadd
geoadd is used to store specified geospatial locations; it can add one or more longitude, latitude, and member name to the specified key.
The geoadd syntax format is as follows:
GEOADD key longitude latitude member [longitude latitude member ...]
In the example below, 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; returns nil for those that do not exist.
The geopos syntax format 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 given locations.
The geodist syntax format is as follows:
GEODIST key member1 member2 [m|km|ft|mi]
member1 and member2 are the two geographic locations.
Description of the last distance unit parameter:
- m: meters, the default unit.
- km: kilometers.
- mi: miles.
- ft: feet.