当前位置:网站首页>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
边栏推荐
- Concurrent optimization summary
- NFT Insider #64:电商巨头eBay提交NFT相关商标申请,毕马威将在Web3和元宇宙中投入3000万美元
- md5工具类
- Mysql root 账号如何重置密码
- Unity Xiuxian mobile game | Lua dynamic sliding function (specific implementation of three source codes)
- SPSS installation and activation tutorial (including network disk link)
- Advanced area of attack and defense world misc 3-11
- Attack and defense world misc advanced area Hong
- LOGO特训营 第一节 鉴别Logo与Logo设计思路
- PostgreSQL server programming aggregation and grouping
猜你喜欢
Tla+ introductory tutorial (1): introduction to formal methods
蓝队攻防演练中的三段作战
Concurrent optimization summary
Redis sentinel simply looks at the trade-offs between distributed high availability and consistency
The Sandbox 和数字好莱坞达成合作,通过人力资源开发加速创作者经济的发展
将QA引入软件开发生命周期是工程师要遵循的最佳实践
Advanced area a of attack and defense world misc Masters_ good_ idea
Logo special training camp Section V font structure and common design techniques
攻防世界 MISC 进阶区 hong
LOGO特训营 第四节 字体设计的重要性
随机推荐
[Yugong series] go teaching course 003-ide installation and basic use in July 2022
Unity修仙手游 | lua动态滑动功能(3种源码具体实现)
Flask 上下文详解
Embedded development: skills and tricks -- seven skills to improve the quality of embedded software code
微服务--开篇
蓝队攻防演练中的三段作战
剑指 Offer 65. 不用加减乘除做加法
剑指Offer 68 - II. 二叉树的最近公共祖先
攻防世界 MISC 进阶 glance-50
攻防世界 misc 进阶区 2017_Dating_in_Singapore
Shell 脚本实现应用服务日志入库 Mysql
Attack and defense world misc advanced area Hong
La prospérité est épuisée, les choses sont bonnes et mauvaises: Où puis - je aller pour un chef de station personnel?
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特训营 第二节 文字与图形的搭配关系
攻防世界 MISC 进阶区 Ditf
Logo special training camp Section V font structure and common design techniques
Deployment of JVM sandbox repeater
Test will: bug classification and promotion solution
Logo special training camp section 1 Identification logo and logo design ideas