Home > Database > Redis > body text

Redis special data type Geospatial

WBOY
Release: 2022-10-12 16:21:35
forward
1588 people have browsed it

In the previous article "Redis Special Data Type Stream", I introduced you to the relevant content about the data type stream. This article brings you Redis Another data type of Geospatial, let’s take a look at it, I hope it will be helpful to everyone.

Redis special data type Geospatial

Recommended learning: Redis video tutorial

Preface

We all know Redis Provides a wealth of data types, including four special types: BitMap, HyperLogLog, Geospatial, and Stream.

Today we will talk in detail about Geospatial, one of the four special data types of Redis;

Application scenarios: Scenarios for storing geographical location information, such as Didi taxi calling;

Overview Introduction

Copyright belongs to the author. For commercial reprinting, please contact the author for authorization. For non-commercial reprinting, please indicate the source.

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

In our daily lives, we increasingly rely on searching for “nearby restaurants” and hailing taxis on taxi-hailing software, all of which are inseparable from location-based service (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 if adjacent longitude and latitude ranges need to be queried, GEO is very suitable for application in LBS service scenarios.

Internal implementation

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

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

In this way, we can save the longitude and latitude into the Sorted Set, and use the "ordered range search by weight" feature provided by the Sorted Set to implement the "search nearby" function frequently used in LBS services. need.

Common commands

# 存储指定的地理空间位置,可以将一个或多个经度(longitude)、纬度(latitude)、位置名称(member)添加到指定的 key 中。
GEOADD key longitude latitude member [longitude latitude member ...]
# 从给定的 key 里返回所有指定名称(member)的位置(经度和纬度),不存在的返回 nil。
GEOPOS key member [member ...]
# 返回两个给定位置之间的距离。
GEODIST key member1 member2 [m|km|ft|mi]
# 根据用户给定的经纬度坐标来获取指定范围内的地理位置集合。
GEORADIUS key longitude latitude radius m|km|ft|mi [WITHCOORD] [WITHDIST] [WITHHASH] [COUNT count] [ASC|DESC] [STORE key] [STOREDIST key]
Copy after login

Application scenarios

Didi Taxi

Here we take the Didi taxi-hailing scenario as an example to introduce how to use the GEO commands: GEOADD and GEORADIUS.

Assume 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 longitude and latitude of all vehicles. The collection key is cars:locations.

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

GEOADD cars:locations 116.034579 39.030452 33
Copy after login

When the user wants to find an online ride-hailing service near him , LBS applications can use the GEORADIUS command.

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

GEORADIUS cars:locations 116.054579 39.030452 5 km ASC COUNT 10
Copy after login

Recommended learning: Redis video tutorial

The above is the detailed content of Redis special data type Geospatial. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:juejin.im
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!