当前位置:网站首页>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
边栏推荐
- Jvm-Sandbox-Repeater的部署
- Record: how to scroll screenshots of web pages on Microsoft edge in win10 system?
- Sobel filter
- 攻防世界 MISC 进阶区 Ditf
- Create Ca and issue certificate through go language
- sobel过滤器
- Tla+ introductory tutorial (1): introduction to formal methods
- 达梦数据凭什么被称为国产数据库“第一股”?
- 记录:关于Win10系统中Microsoft Edge上的网页如何滚动截屏?
- Logo Camp d'entraînement section 3 techniques créatives initiales
猜你喜欢

攻防世界 MISC 进阶区 hit-the-core

Business is too busy. Is there really no reason to have time for automation?

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

Wake up day, how do I step by step towards the road of software testing

Why is Dameng data called the "first share" of domestic databases?

Attack and defense world misc advanced area Hong

攻防世界 MISC 进阶区 3-11

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

Attack and defense world misc advanced area ditf

质量体系建设之路的分分合合
随机推荐
Sqlserver encrypts and decrypts data
Jvm-Sandbox-Repeater的部署
业务太忙,真的是没时间搞自动化理由吗?
攻防世界 MISC 进阶区 3-11
剑指 Offer 67. 把字符串转换成整数
Attack and defense world misc advanced area Hong
Gnawing down the big bone - sorting (II)
Hit the core in the advanced area of misc in the attack and defense world
Locust performance test - environment construction and use
MySQL Architecture - user rights and management
idea中pom.xml依赖无法导入
Attack and defense world misc advanced area ditf
High school physics: linear motion
都说软件测试很简单有手就行,但为何仍有这么多劝退的?
Attack and defense world misc master advanced zone 001 normal_ png
leetcode 72. Edit Distance 编辑距离(中等)
共创软硬件协同生态:Graphcore IPU与百度飞桨的“联合提交”亮相MLPerf
堆排序代码详解
将QA引入软件开发生命周期是工程师要遵循的最佳实践
More than 30 institutions jointly launched the digital collection industry initiative. How will it move forward in the future?