当前位置:网站首页>Redis data structure (VIII) -- Geo

Redis data structure (VIII) -- Geo

2022-06-12 06:18:00 leo_ messi94

brief introduction

GEO,geographic Abbreviation , Geographic information . This type of , Is the two-dimensional coordinates of the element , On the map is latitude and longitude .redis Based on this type , Longitude and latitude settings are provided , Inquire about , Range queries , Distance inquiry , Longitude and latitude hash Wait for the operation .

  • Cannot add , City data is usually downloaded , Directly through java The program is imported once .
  • Effective longitude from -180° To 180°. Effective latitude from -85.05112878° To 85.05112878°.
  • When the coordinate position is beyond the specified range , This command will return an error .
  • Added data , Can't add again .

principle , Operation mode

This data will be stored in sorted set The purpose is to facilitate the use of GEORADIUS perhaps GEORADIUSBYMEMBER Command to query the radius of data

sorted set Use one called Geohash Technology to fill . The bits of longitude and latitude are staggered , To form a unique 52 An integer . We know , One sorted set Of double score Can represent a 52 An integer , Without losing accuracy .

The radius check format allows for this query 1 + 8 One area needs to cover the entire radius , And discard the radius outside the element . By calculating the range of the area , By calculating the coverage , The score of the sorting set of the less important part , And calculate the score range for each area sorted set Query in

Earth mode used (earth model)

This is just assuming that the earth is a sphere , Because the distance formula used is Haversine The formula . This formula only applies to the earth , Not a perfect sphere . When used on social networking sites and most other applications that require query radius , These deviations are not a problem . however , In the worst case, the deviation may be 0.5%, Therefore, some geographically critical applications still need to be considered carefully

command

1.geoadd: The specified geographical location space ( Longitude and latitude 、 name ) Add to specified key in .

  • Format :GEOADD key longitude latitude member [longitude latitude member …]
  • Return value : Add to sorted set The number of elements , But not including updated score The elements of
  • Complexity : Each element added is O(log(N)) ,N yes sorted set The number of elements
redis> GEOADD china 13.361389 38.115556 "shanghai" 15.087269 37.502669 "beijing"
(integer) 2
redis> GEODIST china shanghai beijing
"166274.15156960039"
redis> GEORADIUS china 15 37 100 km
1) "shanghai"
redis> GEORADIUS china 15 37 200 km
1) "shanghai"
2) "beijing"

2.geodist: Returns the distance between two given positions

  • If one of the two positions does not exist , Then the command returns a null value .
  • Parameters for specified units unit Must be one of the following units :
    • m Expressed in meters .
    • km Expressed in kilometers .
    • mi In miles .
    • ft In feet .
  • If the user does not explicitly specify the unit parameter , that GEODIST The default is meters .
  • GEODIST The command assumes that the earth is perfectly spherical when calculating distance , In extreme cases , This assumption will lead to 0.5% The error of the
  • Return value : The calculated distance is returned as a double precision floating-point number . If the given location element does not exist , Then the command returns a null value
redis> GEOADD Sicily 13.361389 38.115556 "Palermo" 15.087269 37.502669 "Catania"
(integer) 2
redis> GEODIST Sicily Palermo Catania
"166274.15156960039"
redis> GEODIST Sicily Palermo Catania km
"166.27415156960038"
redis> GEODIST Sicily Palermo Catania mi
"103.31822459492736"
redis> GEODIST Sicily Foo Bar
(nil)

3.geohash: Returns the... Of one or more positional elements Geohash Express

  • Format :GEOHASH key member [member …]
  • Usually using elements that represent positions uses different techniques , Use Geohash Location 52 Dot integer code . Because the initial minimum and maximum coordinates used in the encoding and decoding process are different , The coding is also different from the standard . This command returns a standard Geohash, On Wikipedia and geohash.org Websites have relevant descriptions
  • Return value : An array , Each item in the array is a geohash . Command returned geohash The location of the element corresponds to the location given by the user one by one
  • Complexity :O(log(N)) for each member requested, where N is the number of elements in the sorted set
redis> GEOADD Sicily 13.361389 38.115556 "Palermo" 15.087269 37.502669 "Catania"
(integer) 2
redis> GEOHASH Sicily Palermo Catania
1) "sqc8b49rny0"
2) "sqdtr74hyu0"

geohash String properties

The command will return 11 A character Geohash character string , So there is no precision Geohash, Loss compared with , Use the inside 52 Who said . Back to geohashes It has the following characteristics :

  1. They can shorten the characters from the right . It will lose accuracy , But it will still point to the same region .
  2. It can be geohash.org Site use , website http://geohash.org/. Query example :http://geohash.org/sqdtr74hyu0.
  3. A prefix string similar to is near , But the opposite is not true , It's possible , Use different prefixes around the string

4.geopos: from key Returns the location of all the positioning elements ( Longitude and latitude )

  • Format :GEOPOS key member [member …]
  • Given a sorted set Represents the spatial index of the , Intensive use geoadd command , It is often useful to obtain the coordinates of a specified member . When the spatial index is filled through geoadd Convert the coordinates of into a 52 position Geohash, Therefore, the coordinates returned may not be exactly the same as those of the added element , But small mistakes may be made .
  • because GEOPOS The command accepts a variable number of positional elements as input , So even if the user only gives a location element , The command also returns an array reply
  • Return value :GEOPOS Command returns an array , Each item in the array consists of two elements : The first element is the longitude of the positioning element , The second element is the latitude of the location element . When the given location element does not exist , The corresponding array item is null
  • Complexity :O(log(N)) for each member requested, where N is the number of elements in the sorted set
redis> GEOADD Sicily 13.361389 38.115556 "Palermo" 15.087269 37.502669 "Catania"
(integer) 2
redis> GEOPOS Sicily Palermo Catania NonExisting
1) 1) "13.361389338970184"
   2) "38.115556395496299"
2) 1) "15.087267458438873"
   2) "37.50266842333162"
3) (nil)
redis> 

5.georadius: Centered on a given latitude and longitude , Return key contains position elements , All position elements whose distance from the center does not exceed the given maximum distance

  • Format :GEORADIUS key longitude latitude radius m|km|ft|mi [WITHCOORD] [WITHDIST] [WITHHASH] [COUNT count]
  • Range can use one of the following units :
    • m Expressed in meters .
    • km Expressed in kilometers .
    • mi In miles .
    • ft In feet .
  • When the following options are given , The command will return additional information :
    • WITHDIST: While returning location elements , Return the distance between the location element and the center as well . The unit of distance is consistent with the unit of range given by the user .
    • WITHCOORD: Return the longitude and dimension of the location element as well .
    • WITHHASH: With 52 Bit signed integer form , Return the location element through the original geohash Encoded ordered set score . This option is mainly used for underlying applications or debugging , It doesn't really work .
  • The command returns unordered location elements by default . Through the following two parameters , The user can specify the sorting method of the returned location elements :
    • ASC: According to the position of the center , Return the location element in a near to far way .
    • DESC: According to the position of the center , Return the location elements from far to near .
  • By default , GEORADIUS The command returns all matching location elements . Although users can use COUNT Options to get before N Match elements , But because the command may need to handle all the matched elements internally , So when you search for a very large area , Even if you only use COUNT Option to get a few elements , Command execution can also be very slow . But on the other hand , Use COUNT Option to reduce the number of elements to be returned , It's still very useful for reducing bandwidth
  • Return value
    • In the absence of any given WITH In the case of options , The command will only return an image like [“New York”,”Milan”,”Paris”] Such linearity (linear) list .
    • In the specified WITHCOORD 、 WITHDIST 、 WITHHASH Wait for options , The command returns a two-level nested array , Each subarray of the inner layer represents an element .
    • When returning nested arrays , The first element of a subarray is always the name of the positional element . As for additional information , It will be used as a subsequent element of the subarray , Is returned in the following order :
      • The distance between the center and the location element returned in floating-point format , The unit is the same as that specified by the user .
      • geohash Integers .
      • A coordinate consisting of two elements , They are longitude and latitude
    • for instance , GEORADIUS Sicily 15 37 200 km WITHCOORD WITHDIST Each subarray returned by such a command is similar to the following format :
["Palermo","190.4424",["13.361389338970184","38.115556395496299"]]
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.361389338970184"
      2) "38.115556395496299"
2) 1) "Catania"
   2) 1) "15.087267458438873"
      2) "37.50266842333162"
redis> GEORADIUS Sicily 15 37 200 km WITHDIST WITHCOORD
1) 1) "Palermo"
   2) "190.4424"
   3) 1) "13.361389338970184"
      2) "38.115556395496299"
2) 1) "Catania"
   2) "56.4413"
   3) 1) "15.087267458438873"
      2) "37.50266842333162"
redis> 

6.georadiusbymember: Find the elements in the specified range

  • Format :GEORADIUSBYMEMBER key member radius m|km|ft|mi [WITHCOORD] [WITHDIST] [WITHHASH] [COUNT count]
  • This command and GEORADIUS command , Can find the elements in the specified range , however GEORADIUSBYMEMBER The center point of is determined by a given location element , Not like it GEORADIUS like that , Use the entered longitude and latitude to determine the center point
  • The location of the specified member is used as the center of the query .
  • About GEORADIUSBYMEMBER More information about the command , Please refer to GEORADIUS Documents for commands
  • Complexity :O(N+log(M)) where N is the number of elements inside the bounding box of the circular area delimited by center and radius and M is the number of items inside the index
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) "Palermo"
redis> 
原网站

版权声明
本文为[leo_ messi94]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/03/202203010611073159.html