redis geo geographic location system application warfare case

Keywords: Database Redis

Proper noun
geographic geography
Radius radius range; radius (distance); circular area measured by radius

redis GEO IMPLEMENTATION

The redis GEO implementation mainly includes the following two technologies:

1. Use geohash to save the coordinates of geographic location.
2. Use sort set to save the set of geographical location. (emphasis: sort set-based storage)

List of redis GEO methods

  1. geoadd

Meaning:

Adds the specified geospatial location (latitude, longitude, name) to the specified key.

Syntax:

GEOADD key longitude latitude member [longitude latitude member]

Realization:

geoadd city_clusters 116.405285 39.904989 beijing  121.472644 31.231706 shanghai
  1. geopos

Meaning:

Returns the location (longitude and latitude) of all given location elements from key

Syntax:

GEOPOS key member [member ...]

Realization:

127.0.0.1:6379> geopos city_clusters beijing shanghai
1) 1) "116.40528291463851929"
   2) "39.9049884229125027"
2) 1) "121.47264629602432251"
   2) "31.23170490709807012"
  1. geodist

Meaning:

Returns the distance between two given positions.
Remarks:
The parameter unit of the specified unit must be one of the following units:
m denotes in meters.
km denotes kilometers.
mi means miles.
ft stands for feet.
If the user does not explicitly specify unit parameters, GEODIST defaults to use meters as units.

Syntax:

GEODIST key member1 member2 [unit]

Realization:

127.0.0.1:6379> geodist city_clusters beijing shanghai km
"1067.5980"
127.0.0.1:6379> geodist city_clusters beijing shanghai
"1067597.9668"
  1. geohash

Meaning:

Returns the Geohash representation of one or more location elements.
Function: Convert two-dimensional longitude and latitude into strings.

Syntax:

GEOHASH key member [member ...]

Realization:

127.0.0.1:6379> geohash city_clusters beijing
1) "wx4g0b7xrt0"
127.0.0.1:6379> geohash city_clusters beijing shanghai
1) "wx4g0b7xrt0"
2) "wtw3sjt9vg0"
  1. georadius

Meaning:

With the given latitude and longitude as the center, the return key contains all the location elements whose distance from the center does not exceed the given maximum distance.
Remarks:
Scope can use one of the following units:
m denotes in meters.
km denotes kilometers.
mi means miles.
ft stands for feet.
Given the following options, the command returns additional information:
WITHDIST: While returning the location element, the distance between the location element and the center is also returned. The unit of distance is consistent with the unit of range given by the user.
WITHCOORD: Returns the longitude and dimension of the location element as well.
WITHHASH: In the form of 52-bit signed integers, returns the ordered set score of location elements encoded by the original geohash. This option is mainly used for underlying applications or debugging, but it does not play a significant role in practice.

COUNT limits the number of records returned

The command returns unordered location elements by default. With the following two parameters, the user can specify how to sort the returned location elements:
ASC: Return location elements from near to far according to the location of the center.
DESC: Returns location elements from far to near according to the location of the center.

Syntax:

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

Realization:

 georadius city_clusters 114.546569 30.496705 1000 km withdist withcoord withhash  count 3 asc  
1) 1) "wuhan"
   2) "0.0002"
   3) (integer) 4051940381831537
   4) 1) "114.54657107591629028"
      2) "30.49670420836107354"
2) 1) "ezhou"
   2) "34.4580"
   3) (integer) 4051965036178493
   4) 1) "114.88938957452774048"
      2) "30.40377626121739496"
3) 1) "shanghai"
   2) "666.1872"
   3) (integer) 4054803464817068
   4) 1) "121.47264629602432251"
      2) "31.23170490709807012
  1. georadiusbymember

Meaning:

Like the GEORADIUS command, elements within a specified range can be found, but the central point of GEORADIUSBYMEMBER is determined by the given location element, not by the longitude and latitude of the input, as in GEORADIUS.
Remarks:
Same georadius

Syntax:

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

Realization:

 georadiusbymember city_clusters ezhou 1000 km withcoord withdist withhash count 10 asc
1) 1) "ezhou"
   2) "0.0000"
   3) (integer) 4051965036178493
   4) 1) "114.88938957452774048"
      2) "30.40377626121739496"
2) 1) "wuhan"
   2) "34.4578"
   3) (integer) 4051940381831537
   4) 1) "114.54657107591629028"
      2) "30.49670420836107354"
3) 1) "shanghai"
   2) "635.4470"
   3) (integer) 4054803464817068
   4) 1) "121.47264629602432251"
      2) "31.23170490709807012"

Posted by fitzromeo on Sat, 04 May 2019 00:40:38 -0700