Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What's New in Redis: GEOHASH

2025-02-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

I. brief remarks

The GEO feature of Redis will be released in Redis version 3.2, which can store and manipulate the geolocation information given by the user.

Adds the specified geospatial item (latitude, longitude, name) to the specified key. The data is stored in the key as a sort set, so that items can be retrieved later using the radius query using the GEORADIUS or GEORADIUSBYMEMBER command.

This command takes the parameters of the standard format xforme y, so longitude must be specified before latitude. The coordinates that can be indexed are limited: areas very close to the pole are not indexable. As specified in EPSG:900913 / EPSG:3785 / OSGEO:41001, the specific limits are as follows:

The effective longitude is-180 to 180 degrees.

The effective latitude is-85.05112878 to 85.05112878 degrees.

Note: there is no GEODEL command, you can use ZREM to delete elements. The geographic index structure is just a sort set.

2. Redis GEO implementation

The Redis GEO implementation mainly includes the following two technologies:

1. Use geohash to save the coordinates of the geographic location.

2. Use ordered sets (zset) to save geographically located collections.

III. GEOHASH

The idea of geohash is to convert two-dimensional longitude and latitude into one-dimensional string. Geohash has the following three characteristics:

1. The longer the string, the more accurate the range. When the coding length is 8, the accuracy is about 19 meters, and when the coding length is 9, the accuracy is about 2 meters.

2. if the string is similar, the distance is close. Using the prefix matching of the string, you can query the nearby geographical location. In this way, it is possible to quickly query the geographical location near certain coordinates.

3. The string calculated by geohash can be decoded back to the original longitude and latitude.

Fourth, GEOHASH command syntax

1. Geoadd is used to increase the coordinates of geographic locations. You can add geographic locations in batches in the following format:

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

Introduction of the field:

Key identifies a collection of geographic locations. Longitude latitude member identifies the coordinates of a geographic location. Longitude is the longitude of a geographical location, and latitude is the latitude of a geographical location.

Member is the name of the geographic location. GEOADD can add a batch of geolocations to the collection in batch

Egg:GEOADD beijing-area 39.8865577059 116.2161254883 shijingshan

2. Geopos can obtain the coordinates of geographical locations and obtain the coordinates of multiple geographical locations in batches. The command format is as follows:

GEOPOS key member [member...]

3. Geodist is used to obtain the distance between two geographical locations. The command format is as follows:

Units can be specified in the following four types:

M: meters, the default distance unit is meters, if you do not pass this parameter, the unit is meters.

Km: km.

Mi: miles.

Ft: feet.

4. Georadius can obtain the collection of geographical locations within the specified range according to the given geographic location coordinates. The format of the command is:

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

Longitude latitude identifies the coordinates of the geographical location, and radius indicates the range distance, which can be measured in m | km | ft | mi. There are also some optional parameters:

WITHCOORD: if the WITHCOORD parameter is passed in, the returned result will have the latitude and longitude of the matching position.

WITHDIST: if the WITHDIST parameter is passed in, the result will be returned with the distance between the matching location and the given geographic location.

ASC | DESC: the default result is unsorted, the ASC is sorted from near to far, and the DESC is sorted from far to near.

WITHHASH: if the WITHHASH parameter is passed in, the result will be returned with the hash value of the matching location.

COUNT count: pass in the COUNT parameter to return a specified number of results.

5. Georadiusbymember can obtain a collection of geographic locations within a specified range according to a given geographic location. The georadius command passes coordinates, and georadiusbymember passes geographic locations. Georadius is more flexible

You can get the geographic location within the range of any coordinate point. But most of the time, you just want to get other geographical locations near one location, and it's more convenient to use georadiusbymember.

The format of the georadiusbymember command is (optional parameters of the command have the same meaning as georadius):

Georadiusbymember key member radius [m | km | ft | mi] [WITHCOORD] [WITHDIST] [ASC | DESC] [WITHHASH] [COUNT count]

# Command example

1. Add a single area

GEOADD beijing-area 116.2161254883 39.8865577059 shijingshan

Batch add area

Geoadd Beijing-areas 116.2161254883 39.8865577059 ShiJingShan 116.1611938477 39.7283134103 FangShan 116.3534545898 39.7071866568 DaXing 116.4166259766 39.9097362345 DongChenQu

2. View the geographic information that has been added. You can use a single member or multiple queries.

192.168.1.130 6379 > geopos Beijing-areas ShiJingShan FangShan

1) "116.21612817049026489"

2) "39.88655846536294547"

2) 1) "116.16119652986526489"

2) "39.72831328866426048"

3. GEODIST command to calculate the distance between the two positions, through the existing KEY under the two positions to calculate the distance, the unit distance is m meters km kilometers and so on.

192.168.1.130 6379 > geodist Beijing-areas ShiJingShan FangShan m

"18216.0860"

4. Georadius views all locations near the current location and the parameters are used: [WITHCOORD] [WITHDIST] [WITHHASH] [COUNT count] [ASC | DESC] [STORE key] [STOREDIST key]

Add test data:

Geoadd beijing 116.6638183594 39.8928799003 tongzhou

Geoadd beijing 116.3534545898 39.7071866568 daxing

Geoadd beijing 116.2161254883 39.8865577059 shijingshan

Geoadd beijing 116.1611938477 39.7283134103 fangshan

Georadius beijing 116.6875076294 39.8953822745 5000 m

1. Statistics take the farthest position in the vicinity of 5000m

Georadius beijing 116.6875076294 39.8953822745 50000 m COUNT 1 DESC # # ASC sort the location closest to the current location, when using count is similar to limit

2. Calculate the distance between the positions in the vicinity of 5000m and the distance between the farthest position-- the distance of the difference between the current positions of withdist.

Georadius beijing 116.6875076294 39.8953822745 50000 m COUNT 1 DESC withdist

3. Count the position in the vicinity of 5000m and display the longitude and latitude information-withcoord shows longitude and latitude

Georadius beijing 116.6875076294 39.8953822745 50000 m DESC withcoord

4. Georadiusbymember displays information within the range of the distance from which a location has been added as the center point.

192.168.1.130 6379 > georadiusbymember beijing shijingshan 5000 m

1) "shijingshan"

192.168.1.130 6379 > georadiusbymember beijing shijingshan 50000 m

1) "shijingshan"

2) "tongzhou"

3) "fangshan"

4) "daxing"

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report