English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Redis GEO is mainly used to store geographic location information and operate on the stored information, this feature is available in Redis 32 Version added.
Redis GEO operation methods include:
geoadd: Adds the coordinates of a geographic location.
geopos: Retrieves the coordinates of a geographic location.
geodist: Calculates the distance between two locations.
georadius: Retrieves a set of geographic locations within a specified range based on the given longitude and latitude coordinates.
georadiusbymember: Retrieves a set of geographic locations within a specified range based on a location stored in a location collection.
geohash: Returns the geohash value of one or more location objects.
geoadd is used to store specified geographic spatial locations and can add one or more longitudes (longitude), latitudes (latitude), 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 examples, the key is Sicily, and Palermo and Catania are the names of locations :
redis> GEOADD Sicily 13361389 38115556 "Palermo" 15.087269 37502669 "Catania" (integer) 2 redis> GEODIST Sicily Palermo Catania "1662741516" redis> GEORADIUS Sicily 15 37 100 km 1) "Catania" redis> GEORADIUS Sicily 15 37 200 km 1) "Palermo" 2) "Catania" redis>
geopos is used to return the positions (longitude and latitude) of all specified names (members) from the given key, and returns nil if they do not exist.
The syntax format of geopos is as follows:
GEOPOS key member [member ...]
redis> GEOADD Sicily 13361389 38115556 "Palermo" 15.087269 37502669 "Catania" (integer) 2 redis> GEOPOS Sicily Palermo Catania NonExisting 1) 1) "1336138933897018433" 2) "3811555639549629859" 2) 1) "15.08726745843887329" 2) "3750266842333162032" 3) (nil) redis>
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 member2 For two geographical locations.
Last distance unit parameter description:
m: Meter, default unit.
km: Kilometer.
mi: Mile.
ft: Foot.
Calculate the distance between Palermo and Catania:
redis> GEOADD Sicily 13361389 38115556 "Palermo" 15.087269 37502669 "Catania" (integer) 2 redis> GEODIST Sicily Palermo Catania "1662741516" redis> GEODIST Sicily Palermo Catania km "1662742" redis> GEODIST Sicily Palermo Catania mi "1033182" redis> GEODIST Sicily Foo Bar (nil) redis>
georadius returns all position elements within the maximum distance from the center point given by the given longitude and latitude.
georadiusbymember and GEORADIUS commands are the same, both can find elements within a specified range, but the center point of georadiusbymember is determined by the given position element, not determined by longitude and latitude.
The syntax format of 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 description:
m: Meter, default unit.
km: Kilometer.
mi: Mile.
ft: Foot.
WITHDIST: When returning the position element, also return the distance between the position element and the center.
WITHCOORD: Also return the longitude and latitude of the position element.
WITHHASH: With 52 The form of signed integer, returns the ordered set score of the position element after the original geohash encoding. This option is mainly used for low-level applications or debugging, and its actual effect is not significant.
COUNT Limits the number of records returned.
ASC: Sort the search results from near to far.
DESC: Sort the search results from far to near.
georadius example:
redis> GEOADD Sicily 13361389 38115556 "Palermo" 15.087269 37502669 "Catania" (integer) 2 redis> GEORADIUS Sicily 15 37 200 km WITHDIST 1) 1) "Palermo" 2) "190.4424" 2) 1) "Catania" 2) "564413" redis> GEORADIUS Sicily 15 37 200 km WITHCOORD 1) 1) "Palermo" 2) 1) "1336138933897018433" 2) "3811555639549629859" 2) 1) "Catania" 2) 1) "15.08726745843887329" 2) "3750266842333162032" redis> GEORADIUS Sicily 15 37 200 km WITHDIST WITHCOORD 1) 1) "Palermo" 2) "190.4424" 3) 1) "1336138933897018433" 2) "3811555639549629859" 2) 1) "Catania" 2) "564413" 3) 1) "15.08726745843887329" 2) "3750266842333162032" redis>
georadiusbymember example:
redis> GEOADD Sicily 13583333 37316667 "Agrigento" (integer) 1 redis> GEOADD Sicily 13361389 38115556 "Palermo" 15.087269 37502669 "Catania" (integer) 2 redis> GEORADIUSBYMEMBER Sicily Agrigento 100 km 1) "Agrigento" 2) "Palermo" redis>
Redis GEO uses geohash to save geographical coordinates.
geohash is used to get the geohash value of one or more location elements.
The geohash syntax format is as follows:
GEOHASH key member [member ...]
Example:
redis> GEOADD Sicily 13361389 38115556 "Palermo" 15.087269 37502669 "Catania" (integer) 2 redis> GEOHASH Sicily Palermo Catania 1) "sqc8b49rny0" 2) "sqdtr74hyu0" redis>