preface

Yes, I’m not a clicker, and I guarantee that no good brother has ever used it, and no good brother has ever used it. To be honest, IF I didn’t do this Redis feature blog, I wouldn’t know what it was likeHyperLogLog,Bitmaps,LuaAnd other related functions. I believe that many good brothers also like me, the whole Redis is only able to use some of the basic things, you see this is not learned. Today the words of the same I still did not use, but I learn to learn ah, do it, next year’s interview (not a strong man I ah, I learn this is not to install *) of the time and can install a wave, install the kind of run.

An overview of the

Redis3.2 provides GEO (location of geographic information) function, GEO is mainly used to store location information, and to operate on the stored information, used to achieve such as nearby location, shake and other functions that depend on location information, for developers who need to implement these features is a great blessing.

The command

Add location information

Redis providegeoaddCommand to add or update geographic information locations wherelongitude,latitude,memberAre the longitude, latitude and members of the geographical location respectively. Add the city latitude and longitude as shown below

# # format
geoadd key longitude latitude member [longitude latitude member .]
## Add Beijing, the return result represents the number of successful additions, if there is already a return 0
127.0.0.1:6379> geoadd cities:locations 116.28 39.55 beijing
(integer) 1
Copy the code

Get geographic information location

## format, member member
geopos key member [member .]
## Get the location of Tianjin
127.0.0.1:6379> geopos cities:locations tianjin
1) 1) "117.12000042200088501"
2) "39.0800000535766543"
Copy the code

Gets the distance between two geographical locations

Redis provides geodist to capture the distance between two geographical locations. Unit represents the unit of the result, including the following four types:

  1. M (meters) stands for meters.
  2. Km (kilometers) stands for kilometers.
  3. Mi (miles) stands for miles.
  4. Ft stands for ruler.
# # format
geodist key member1 member2 [unit]
## Calculate the distance from Beijing to Tianjin
127.0.0.1:6379> geodist cities:locations tianjin beijing km
"89.2061"
Copy the code

Gets a collection of geographic information locations within the specified location range

Georadius and Georadiusbymember have the same functions. They both take a geological location as the center to calculate other geographic information locations within the specified radius. The difference is that the central location of Georadius gives specific latitude and longitude. Georadiusbymember simply gives the member. In its radiusm | km | ft | mi is necessary parameter, specify the radius (unit), the two commands have a lot of optional parameters, as follows:

  1. Withcoord: Returns a result that contains latitude and longitude.
  2. Withdist: The return result contains the distance from the central node location.
  3. Withhash: Returns a result that contains a geohash, which is described later.
  4. COUNT COUNT: Specifies the number of results to be returned.
  5. Asc | desc: return the result according to the distance from the center node for ascending or descending order.
  6. Store Key: Saves the geographical location information of the returned result to the specified key.
  7. Storedist Key: Stores the distance of the returned result from the central node to the specified key.
# # format
georadius key longitude latitude radiusm|km|ft|mi [withcoord] [withdist] [withhash] [COUNT count] [asc|desc] [store key] [storedist key]
# # format
georadiusbymember key member radiusm|km|ft|mi [withcoord] [withdist]
[withhash] [COUNT count] [asc|desc] [store key] [storedist key]
## Calculate the cities within 150 kilometers of Beijing among the five cities
127.0.0.1:6379> georadiusbymember cities:locations beijing 150 km
1) "beijing"
2) "tianjin"
3) "tangshan"
4) "baoding"
Copy the code

Get GeoHash

GeoHash

GeoHash is essentially a way of spatially indexing, based on understanding the earth as a two-dimensional plane and recursively breaking the plane into smaller sub-blocks, each with the same encoding over a certain latitude and longitude range. Using GeoHash to establish spatial index can improve the efficiency of latitude and longitude retrieval for spatial POI data. The Geohash principle uses the Geohash to convert a two-dimensional latitude and longitude into a one-dimensional string. Geohash has the following features:

  1. GEOIs of the data typezsetRedis will have all geo-location informationgeohashStored in azsetIn the.
  2. The longer the string, the more precise the position, for examplegeohashWhen the length is 9, the accuracy is about 2 meters. The following figure
  3. The more similar two strings are, the closer they are to each other, and Redis implements the related commands using the string prefix matching algorithm.
  4. geohashCoding and latitude and longitude are interchangeable.

# # format
geohash key member [member .]
## Calculate Beijing geoHash
127.0.0.1:6379> geohash cities:locations beijing
1) "wx4ww02w070"
Copy the code

Delete geographical location information

GEO does not provide commands to delete members, but because the underlying implementation of GEO is Zset, zREM command can be used to delete geographical location information.

# # format
zrem key member
127.0.0.1:6379> zrem cities:locations beijing
1
Copy the code

The principle of

Added location principle

The underlying data structure of GEO is zset(sorted set). If you do not know the basic data structure, you can see the application scenario and API parsing of Redis sorted set. Zadd key score member [score member… Do you guys see it? Is it similar? GEO has latitude and longitude, while zset has only one score. Yes, it actually uses an algorithm to calculate the corresponding 52-bit GEOHASH (which is also an algorithm mentioned above) as the Score value of the element by latitude and longitude. Does that make it the same as Zset? So its storage principle and Zset is the same, but there is a calculation of Score value process.

Geohash analysis

In fact, this has already been mentioned above. If you haven’t read that article, I have found another one that is easier to understand. If you don’t want to read it, I will summarize it as follows. Understanding GeoHash requires solving two problems:

  1. How do you uniquely represent a piece of space on earth?
  2. How do you slice the earth into roughly sized chunks that support different granularity representations?

Solution:

  1. Change the three-dimensional earth into two dimensions: the latitude interval of the earth is [-90,90], and the longitude interval is [-180,180]. Let’s expand it out and think of it as a big rectangle.
  2. Turn two dimensions into one: by using the method of the first step, we can transform the surface of the earth into a plane of two dimensions. So what we’re going to do is convert two dimensions into one. If you cut a two-dimensional space, you can cut a lot of squares. How do I represent this square? The easiest way to do this is to walk through the plane. Each time a point is traversed, a value is assigned to it, such as 00, 01, 10, 11, which corresponds to traversing different positions on the surface as the binary number increases. When the space is divided into four blocks, the coding sequence is 00 in the lower left corner, 01 in the upper left corner, 10 in the lower right corner, and 11 in the upper right corner, which is a z-like curve.
  3. To express one dimension as binary code storage:GeohashThere are several forms of encoding, two of which are common,base 32andbase 36. Encodes a string of binary data that falls on the grid.

Usage scenarios

This has already been mentioned above. Location-dependent functions such as nearby location, nearby people, and shake can all be implemented using GEO.

conclusion

This article is about the location of geographic information GEO, not too deep, the length is already very long. One of the main difficulties is the calculation algorithm of GeoHash. To understand this, you should read the above two articles or search for relevant information. There is a chance to tread the pit behind, there are familiar with the good brother can leave the pit in the comment area.

That’s the end of this issue. Welcome to leave your comments in the comments sectionAsk for attention, ask for likes

The Redis publish/subscribe protocol is a Redis publish/subscribe protocol