How to apply Geospatial of Redis special data type

01-19-2023

Today, the editor will share with you the relevant knowledge points on how to apply Geospatial, a special data type of Redis. I hope you will gain something after reading this article. Let's take a look at it together.

Overview

Redis Geospatial is a new data type in Redis version 3.2, which is mainly used to store geographical location information and operate on the stored information .

In daily life, we rely more and more on searching nearby restaurants and calling taxis on taxi-hailing software, all of which are inseparable from Location-Based Service (LBS) applications. The data accessed by the LBS application is a set of longitude and latitude information associated with people or things, and to be able to query the adjacent longitude and latitude range, GEO is very suitable for application in the LBS service scenario.

AgAAC6u-TVPA1V2Fxf5EiZqljSW7nSwp.png

Internal implementation

GEO itself does not design a new underlying data structure, but directly uses the Sorted Set collection type.

The GEO type uses the GeoHash encoding method to realize the conversion of latitude and longitude to the element weight score in the Sorted Set. The two key mechanisms are "interval division of the two-dimensional map" and "encoding of the interval" . After a set of latitude and longitude falls in a certain interval, it is represented by the coded value of the interval, and the coded value is used as the weight score of the Sorted Set element.

In this way, we can save the latitude and longitude in the Sorted Set, and use the feature of sorted range search by weight provided by the Sorted Set to realize the frequently used search requirements in the LBS service.

Common commands

# Store the specified geospatial location, one or more longitude (longitude), latitude ( latitude), location name (member) to the specified key. 

GEOADD key longitude latitude member [longitude latitude member ...] 

# Return the location (longitude and latitude) of all the specified name (member) from the given key, and return nil if it does not exist. 

GEOPOS key member [member ...] 

# Returns the distance between two given positions. 

GEODIST key member1 member2 [m|km|ft|mi] 

# According to the longitude and latitude coordinates given by the user, get the geographic location collection within the specified range. 

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


Application Scenario

DiDi Calling a Car

Here, taking Didi Calling a Car as an example, introduces how to use the GEO command: GEOADD and GEORADIUS these two commands.

Assuming that the vehicle ID is 33, and the latitude and longitude location is (116.034579, 39.030452), we can use a GEO collection to save the latitude and longitude of all vehicles, and the collection key is cars:locations.

Execute the following command to store the current latitude and longitude location of the vehicle with ID number 33 in the GEO collection:

GEOADD cars:locations 116.034579 39.030452 33


When the user wants to find an online car-hailing car near him, the LBS application can use the GEORADIUS command.

For example, when the LBS application executes the following command, Redis will search for the vehicle information within 5 kilometers centered on this latitude and longitude according to the input user's latitude and longitude information (116.054579, 39.030452), and return it to LBS application.

GEORADIUS cars:locations 116.054579 39.030452 5 km ASC COUNT 10



Copyright Description:No reproduction without permission。

Knowledge sharing community for developers。

Let more developers benefit from it。

Help developers share knowledge through the Internet。

Follow us

Recommended reading

high perspicacity