当前位置:网站首页>Redis入门完整教程:GEO
Redis入门完整教程:GEO
2022-07-04 22:29:00 【谷哥学术】
Redis3.2版本提供了GEO(地理信息定位)功能,支持存储地理位置信
息用来实现诸如附近位置、摇一摇这类依赖于地理位置信息的功能,对于需
要实现这些功能的开发者来说是一大福音。GEO功能是Redis的另一位作者
Matt Stancliff [1] 借鉴NoSQL数据库Ardb [2] 实现的,Ardb的作者来自中国,它
提供了优秀的GEO功能。
1.增加地理位置信息
geoadd key longitude latitude member [longitude latitude member ...]
longitude、latitude、member分别是该地理位置的经度、纬度、成员,表
3-7展示5个城市的经纬度。

cities:locations是上面5个城市地理位置信息的集合,现向其添加北京
的地理位置信息:
127.0.0.1:6379> geoadd cities:locations 116.28 39.55 beijing
(integer) 1
返回结果代表添加成功的个数,如果cities:locations没有包含beijing,
那么返回结果为1,如果已经存在则返回0:
127.0.0.1:6379> geoadd cities:locations 116.28 39.55 beijing
(integer) 0
如果需要更新地理位置信息,仍然可以使用geoadd命令,虽然返回结果
为0。geoadd命令可以同时添加多个地理位置信息:
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.获取地理位置信息
geopos key member [member ...]
下面操作会获取天津的经维度:
127.0.0.1:6379> geopos cities:locations tianjin
1) 1) "117.12000042200088501"
2) "39.0800000535766543"
3.获取两个地理位置的距离。
geodist key member1 member2 [unit]
其中unit代表返回结果的单位,包含以下四种:
·m(meters)代表米。
·km(kilometers)代表公里。
·mi(miles)代表英里。
·ft(feet)代表尺。
下面操作用于计算天津到北京的距离,并以公里为单位:
127.0.0.1:6379> geodist cities:locations tianjin beijing km
"89.2061"
4.获取指定位置范围内的地理信息位置集合
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和georadiusbymember两个命令的作用是一样的,都是以一个地
理位置为中心算出指定半径内的其他地理信息位置,不同的是georadius命令
的中心位置给出了具体的经纬度,georadiusbymember只需给出成员即可。其
中radiusm|km|ft|mi是必需参数,指定了半径(带单位),这两个命令有很多
可选参数,如下所示:
·withcoord:返回结果中包含经纬度。
·withdist:返回结果中包含离中心节点位置的距离。
·withhash:返回结果中包含geohash,有关geohash后面介绍。
·COUNT count:指定返回结果的数量。
·asc|desc:返回结果按照离中心节点的距离做升序或者降序。
·store key:将返回结果的地理位置信息保存到指定键。
·storedist key:将返回结果离中心节点的距离保存到指定键。
下面操作计算五座城市中,距离北京150公里以内的城市:
127.0.0.1:6379> georadiusbymember cities:locations beijing 150 km
1) "beijing"
2) "tianjin"
3) "tangshan"
4) "baoding"
5.获取geohash
geohash key member [member ...]
Redis使用geohash [3] 将二维经纬度转换为一维字符串,下面操作会返回
beijing的geohash值。
127.0.0.1:6379> geohash cities:locations beijing
1) "wx4ww02w070"
geohash有如下特点:
·GEO的数据类型为zset,Redis将所有地理位置信息的geohash存放在zset
中。
127.0.0.1:6379> type cities:locations
zset
·字符串越长,表示的位置更精确,表3-8给出了字符串长度对应的精
度,例如geohash长度为9时,精度在2米左右。
表3-8 geohash长度与精度对应关系
·两个字符串越相似,它们之间的距离越近,Redis利用字符串前缀匹配
算法实现相关的命令。
·geohash编码和经纬度是可以相互转换的。
Redis正是使用有序集合并结合geohash的特性实现了GEO的若干命令。
6.删除地理位置信息
zrem key member
GEO没有提供删除成员的命令,但是因为GEO的底层实现是zset,所以
可以借用zrem命令实现对地理位置信息的删除。
[1] https://matt.sh/
[2] https://github.com/yinqiwen/ardb
[3] https://en.wikipedia.org/wiki/Geohash
边栏推荐
- 【室友用一局王者荣耀的时间学会了用BI报表数据处理】
- 高中物理:直线运动
- UML图记忆技巧
- Introducing QA into the software development lifecycle is the best practice that engineers should follow
- LOGO特训营 第一节 鉴别Logo与Logo设计思路
- The new version judges the code of PC and mobile terminal, the mobile terminal jumps to the mobile terminal, and the PC jumps to the latest valid code of PC terminal
- LOGO特训营 第五节 字体结构与设计常用技法
- 面试必备 LeetCode 链表算法题汇总,全程干货!
- Logo special training camp Section V font structure and common design techniques
- Lost in the lock world of MySQL
猜你喜欢

MySQL Architecture - logical architecture

Logo special training camp section III initial creative techniques

攻防世界 MISC 進階區 Erik-Baleog-and-Olaf

Logo special training camp section II collocation relationship between words and graphics
How to send a reliable request before closing the page

Attack and Defense World MISC Advanced Area Erik baleog and Olaf

LOGO特训营 第三节 首字母创意手法

醒悟的日子,我是怎么一步一步走向软件测试的道路

Redis的持久化机制

Logo special training camp section 1 Identification logo and logo design ideas
随机推荐
POM in idea XML dependency cannot be imported
9 - 类
idea中pom.xml依赖无法导入
繁華落盡、物是人非:個人站長該何去何從
Detailed explanation of flask context
Shell script implements application service log warehousing MySQL
LOGO特训营 第四节 字体设计的重要性
Attack and defense world misc advanced area can_ has_ stdio?
攻防世界 MISC 进阶区 3-11
How to send a reliable request before closing the page
Redis的持久化机制
繁华落尽、物是人非:个人站长该何去何从
高中物理:直线运动
MD5 tool class
新版判断PC和手机端代码,手机端跳转手机端,PC跳转PC端最新有效代码
[Yugong series] go teaching course 003-ide installation and basic use in July 2022
php短视频源码,点赞时会有大拇指动画飘起
SPSS安装激活教程(包含网盘链接)
Gnawing down the big bone - sorting (II)
Recommendation of mobile app for making barcode