Redis has three special data types

Time:2021-8-23

Geospatial geographic location

Positioning of friends, people nearby, calculation of taxi distance
Redis geo is available in redis3.2! This function can calculate the geographical location information, the distance between the two places and the people within a few miles!
You can query some test datawww.jsons.cn/lngcode/
There are only six commands

Redis has three special data types

geoadd

#Add geographic location
#Rule: two levels cannot be added directly. We usually download city data and import it directly through Java program at one time
#Parameter key value (latitude, longitude, name)
#The effective longitude is from - 180 degrees to 180 degrees.
#The effective latitude ranges from -85.05112878 degrees to 85.05112878 degrees.
127.0.0.1:6379[1]> GEOADD china:city 116.405285 39.904989 beijin
(integer) 1
127.0.0.1:6379[1]> GEOADD china:city 121.472644 31.231706 shanghai
(integer) 1
127.0.0.1:6379[1]> GEOADD china:city 106.504962 29.533155 chongqing 114.085947 22.547 shenzhen
(integer) 2
127.0.0.1:6379[1]> GEOADD china:city 120.153576 30.287459 hangzhou 108.9608 34.26641 xian
(integer) 2

geopos

127.0.0.1:6379 [1] > geopos China: City Beijing # gets the longitude and latitude of the specified City
1) 1) "116.40528291463852"
   2) "39.904988422912503"
127.0.0.1:6379[1]> GEOPOS china:city shanghai shenzhen
1) 1) "121.47264629602432"
   2) "31.23170490709807"
2) 1) "114.08594459295273"
   2) "22.546999937739663"

The distance between two people

The parameter unit of the specified unit must be one of the following units:

M is expressed in meters.
Km is expressed in kilometers.
Mi is in miles.
FT is in feet.
If you do not explicitly specify the unit parameter, geodist defaults to meters.

The geodist command assumes that the earth is a perfect sphere when calculating the distance. In extreme cases, this assumption will cause a maximum error of 0.5%.
127.0.0.1:6379[1]> GEODIST china:city beijin shanghai
"1067597.9668"
127.0.0.1:6379[1]> GEODIST china:city beijin shanghai km
"1067.5980"

Georadius – find the elements within a radius centered on a given latitude and longitude

People near me (get the address and location of people nearby) query through the radius
All data should be entered into China: city to make the results clearer

127.0.0.1:6379 [1] > georadius China: City 110, 30, 1000 km # take the longitude and latitude of 110, 30 as the center, look for the city within 1000 km
1) "chongqing"
2) "xian"
3) "shenzhen"
4) "hangzhou"
127.0.0.1:6379[1]> GEORADIUS china:city 110 30 500 km
1) "chongqing"
2) "xian"
127.0.0.1:6379 [1] > geodius China: City 110 30 500 km withdist # displays the distance to the center
1) 1) "chongqing"
   2) "341.4052"
2) 1) "xian"
   2) "484.5164"
127.0.0.1:6379 [1] > georadius China: City 110 30 500 km withword # displays the location information of others within the range
1) 1) "chongqing"
   2) 1) "106.50495976209641"
      2) "29.53315530684997"
2) 1) "xian"
   2) 1) "108.96080106496811"
      2) "34.266409954001269"
127.0.0.1:6379 [1] > geodius China: City 110 30 500 km withlist withgood count 1 # filter the specified results
1) 1) "chongqing"
   2) "341.4052"
   3) 1) "106.50495976209641"
      2) "29.53315530684997"
127.0.0.1:6379[1]> GEORADIUS china:city 110 30 500 km withdist withcoord count 2
1) 1) "chongqing"
   2) "341.4052"
   3) 1) "106.50495976209641"
      2) "29.53315530684997"
2) 1) "xian"
   2) "484.5164"
   3) 1) "108.96080106496811"
      2) "34.266409954001269"

GEORADIUSBYMEMBER

#Find other elements around the specified element
127.0.0.1:6379 [1] > geodiusbymember China: City Beijing 1000km # shows the city within 1000km around Beijing
1) "beijin"
2) "xian"
127.0.0.1:6379[1]> GEORADIUSBYMEMBER china:city shanghai 400 km
1) "hangzhou"
2) "shanghai"

Geohash returns a geohash representation of one or more location elements

This command will return an 11 character geohash string!

#Convert the latitude and longitude of two bits into a one-dimensional string. The closer the two strings are, the closer the distance is
127.0.0.1:6379[1]> GEOHASH china:city beijin chongqing
1) "wx4g0b7xrt0"
2) "wm78p86e170"

The underlying implementation principle of geo is actually Zset! We can use the Zset command to manipulate Geo

127.0.0.1:6379 [1] > zrange China: City 0 - 1 # view all elements in the map
1) "chongqing"
2) "xian"
3) "shenzhen"
4) "hangzhou"
5) "shanghai"
6) "beijin"
127.0.0.1:6379 [1] > zrem China: City Beijing # removes the specified element
(integer) 1
127.0.0.1:6379[1]> ZRANGE china:city 0 -1
1) "chongqing"
2) "xian"
3) "shenzhen"
4) "hangzhou"
5) "shanghai"

This work adoptsCC agreement, reprint must indicate the author and the link to this article

Recommended Today

The selector returned by ngrx store createselector performs one-step debugging of fetching logic

Test source code: import { Component } from ‘@angular/core’; import { createSelector } from ‘@ngrx/store’; export interface State { counter1: number; counter2: number; } export const selectCounter1 = (state: State) => state.counter1; export const selectCounter2 = (state: State) => state.counter2; export const selectTotal = createSelector( selectCounter1, selectCounter2, (counter1, counter2) => counter1 + counter2 ); // […]