当前位置:网站首页>Redis getting started complete tutorial: Geo
Redis getting started complete tutorial: Geo
2022-07-04 22:53:00 【Gu Ge academic】
Redis3.2 Version provides GEO( Location of geographic information ) function , Support the storage of geographic location information
Information is used to realize such as nearby location 、 Shake this kind of function that depends on Geographic Information , For need
It's good news for developers who want to realize these functions .GEO The function is Redis Another author of
Matt Stancliff [1] reference NoSQL database Ardb [2] Realized ,Ardb The author is from China , it
Provides excellent GEO function .
1. Add location information
geoadd key longitude latitude member [longitude latitude member ...]
longitude、latitude、member They are the longitude of the geographical location 、 latitude 、 member , surface
3-7 Exhibition 5 The longitude and latitude of a city .

cities:locations It's the top 5 A collection of urban geographic information , Now add Beijing
Geographic location information :
127.0.0.1:6379> geoadd cities:locations 116.28 39.55 beijing
(integer) 1
The returned result represents the number of successful additions , If cities:locations Not included beijing,
So the return result is 1, If it already exists, return 0:
127.0.0.1:6379> geoadd cities:locations 116.28 39.55 beijing
(integer) 0
If you need to update the geographic location information , Still usable geoadd command , Although the result is returned
by 0.geoadd Command can add multiple geographic location information at the same time :
127.0.0.1:6379> geoadd cities:locations 117.12 39.08 tianjin 114.29 38.02
shijiazhuang 118.01 39.38 tangshan 115.29 38.51 baoding
(integer) 4
2. Get geographic location information
geopos key member [member ...]
The following operation will obtain the economic dimension of Tianjin :
127.0.0.1:6379> geopos cities:locations tianjin
1) 1) "117.12000042200088501"
2) "39.0800000535766543"
3. Gets the distance between two geographical locations .
geodist key member1 member2 [unit]
among unit Represents the unit of the returned result , It includes the following four :
·m(meters) It's for rice .
·km(kilometers) Representative kilometer .
·mi(miles) For miles .
·ft(feet) For ruler .
The following operations are used to calculate the distance between Tianjin and Beijing , And in kilometers :
127.0.0.1:6379> geodist cities:locations tianjin beijing km
"89.2061"
4. Gets the collection of geographic information locations within the specified location range
georadius key longitude latitude radiusm|km|ft|mi [withcoord] [withdist]
[withhash] [COUNT count] [asc|desc] [store key] [storedist key]
georadiusbymember key member radiusm|km|ft|mi [withcoord] [withdist]
[withhash] [COUNT count] [asc|desc] [store key] [storedist key]
georadius and georadiusbymember The effect of the two commands is the same , All in one place
Calculate other geographic information locations within the specified radius by taking the location as the center , The difference is georadius command
The center position of gives the specific longitude and latitude ,georadiusbymember Just give the members . Its
in radiusm|km|ft|mi Is the required parameter , The radius is specified ( With units ), These two commands have many
Optional parameters , As shown below :
·withcoord: The return result contains latitude and longitude .
·withdist: The return result contains the distance from the center node position .
·withhash: The return result contains geohash, of geohash Later on .
·COUNT count: Specify the number of returned results .
·asc|desc: The returned results are in ascending or descending order according to the distance from the central node .
·store key: Save the geographic location information of the returned result to the specified key .
·storedist key: Save the distance between the returned result and the central node to the specified key .
The following operation calculates... In five cities , Distance from Beijing 150 Cities within kilometers :
127.0.0.1:6379> georadiusbymember cities:locations beijing 150 km
1) "beijing"
2) "tianjin"
3) "tangshan"
4) "baoding"
5. obtain geohash
geohash key member [member ...]
Redis Use geohash [3] Convert two-dimensional latitude and longitude to one-dimensional string , The following operation will return
beijing Of geohash value .
127.0.0.1:6379> geohash cities:locations beijing
1) "wx4ww02w070"
geohash It has the following characteristics :
·GEO The data type of is zset,Redis All geographic information will be geohash Store in zset
in .
127.0.0.1:6379> type cities:locations
zset
· The longer the string , Indicates a more precise position , surface 3-8 The corresponding precision of string length is given
degree , for example geohash The length is 9 when , Precision in 2 Rice or so .
surface 3-8 geohash The correspondence between length and accuracy 
· The more similar the two strings are , The closer they are ,Redis Match with string prefix
Algorithm implementation related commands .
·geohash Coding and latitude and longitude can be converted to each other .
Redis It is using ordered sets and combining geohash The features of GEO A number of orders .
6. Delete geographic information
zrem key member
GEO There is no command to delete members , But because GEO The underlying implementation of zset, therefore
You can borrow zrem Command to delete geographic location information .
[1] https://matt.sh/
[2] https://github.com/yinqiwen/ardb
[3] https://en.wikipedia.org/wiki/Geohash
边栏推荐
- Prosperity is exhausted, things are right and people are wrong: where should personal webmasters go
- [the 2023 autumn recruitment of MIHA tour] open [the only exclusive internal push code of school recruitment eytuc]
- Redis入门完整教程:Pipeline
- Redis入门完整教程:Bitmaps
- Redis入门完整教程:发布订阅
- 攻防世界 misc 高手进阶区 a_good_idea
- Analysis of environmental encryption technology
- The overview and definition of clusters can be seen at a glance
- 剑指Offer 68 - II. 二叉树的最近公共祖先
- Google Earth Engine(GEE)——以MODIS/006/MCD19A2为例批量下载逐天AOD数据逐天的均值、最大值、最小值、标准差、方差统计分析和CSV下载(北京市各区为例)
猜你喜欢

MYSQL架构——用户权限与管理

Logo special training camp section II collocation relationship between words and graphics
![[OpenGL] note 29 anti aliasing (MSAA)](/img/66/61f29e1c41d3099d55e2ead0a3b01e.png)
[OpenGL] note 29 anti aliasing (MSAA)

Redis入门完整教程:GEO

蓝队攻防演练中的三段作战

Persistence mechanism of redis

新版判断PC和手机端代码,手机端跳转手机端,PC跳转PC端最新有效代码

Google Earth Engine(GEE)——基于 MCD64A1 的 GlobFire 日常火灾数据集

Redis: redis configuration file related configuration and redis persistence

Attack and defense world misc advanced area Hong
随机推荐
2022-07-04: what is the output of the following go language code? A:true; B:false; C: Compilation error. package main import “fmt“ func main() { fmt.Pri
About stack area, heap area, global area, text constant area and program code area
Why is Dameng data called the "first share" of domestic databases?
新版判断PC和手机端代码,手机端跳转手机端,PC跳转PC端最新有效代码
10 schemes to ensure interface data security
Serial port data frame
Logo special training camp section II collocation relationship between words and graphics
NFT insider 64: e-commerce giant eBay submitted an NFT related trademark application, and KPMG will invest $30million in Web3 and metauniverse
Is Huatai Securities a nationally recognized securities firm? Is it safe to open an account?
业务太忙,真的是没时间搞自动化理由吗?
MySQL Architecture - logical architecture
Business is too busy. Is there really no reason to have time for automation?
POM in idea XML dependency cannot be imported
【lua】int64的支持
攻防世界 misc 进阶区 2017_Dating_in_Singapore
Google Earth Engine(GEE)——以MODIS/006/MCD19A2为例批量下载逐天AOD数据逐天的均值、最大值、最小值、标准差、方差统计分析和CSV下载(北京市各区为例)
Redis入门完整教程:集合详解
蓝队攻防演练中的三段作战
攻防世界 MISC 进阶区 can_has_stdio?
Solana chain application crema was shut down due to hacker attacks