YouTip LogoYouTip

Redis Geo

Redis GEO is mainly used to store geographic location information and perform operations on the stored information. This feature was added in Redis 3.2. Redis GEO commands are: * **geoadd**: Adds a geographic location coordinate. * **geopos**: Gets the coordinates of a geographic location. * **geodist**: Calculates the distance between two locations. * **georadius**: Returns geographic location members within a specified radius from a given longitude and latitude coordinate. * **georadiusbymember**: Returns geographic location members within a specified radius from a given stored member. * **geohash**: Returns the geohash value of one or more location members. ### geoadd geoadd is used to store the specified geographic spatial location. It can add one or more longitude, latitude, and member to the specified key. The geoadd command syntax is as follows: GEOADD key longitude latitude member [longitude latitude member ...] In the following example, the key is Sicily, and Palermo and Catania are the 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. It returns nil for non-existent members. The geopos command syntax 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 command syntax 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, the default unit. * **km**: Kilometers. * **mi**: Miles. * **ft**: Feet. > Calculate the distance between Palermo and Catania: ## 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 takes a given longitude and latitude as the center, and returns all location elements within the given maximum distance from the center. The georadiusbymember command is like the GEORADIUS command, it can also find elements within the specified range. However, the center point of georadiusbymember is determined by the given location member, rather than using longitude and latitude. The georadius and georadiusbymember command syntax is as follows: GEORADIUS key longitude latitude radius m|km|ft|mi [ASC|DESC] GEORADIUSBYMEMBER key member radius m|km|ft|mi [ASC|DESC] Parameter description: * **m**: Meters, the default unit. * **km**: Kilometers. * **mi**: Miles. * **ft**: Feet. * **WITHDIST**: When returning the location elements, also return the distance between the location elements and the center. * **WITHCOORD**: Also return the longitude and latitude of the location elements. * **WITHHASH**: Return the sorted set score of the location element in the form of a 52-bit signed integer. This option is mainly used for low-level applications or debugging, and its actual use is not significant. * **COUNT**: Limit the number of returned records. * **ASC**: Sort the search results from nearest to farthest. * **DESC**: Sort the search results from farthest to nearest. georadius example: ## 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: ## 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)
← R ExamplesR Scatterplots Charts β†’