当前位置:网站首页>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
边栏推荐
- 都说软件测试很简单有手就行,但为何仍有这么多劝退的?
- 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
- 模拟摇杆控制舵机
- Apachecn translation, proofreading, note sorting activity progress announcement 2022.7
- The difference between Max and greatest in SQL
- Introducing QA into the software development lifecycle is the best practice that engineers should follow
- 醒悟的日子,我是怎么一步一步走向软件测试的道路
- Li Kou 98: verify binary search tree
- 关于栈区、堆区、全局区、文字常量区、程序代码区
- Feature scaling normalization
猜你喜欢
【室友用一局王者荣耀的时间学会了用BI报表数据处理】
攻防世界 MISC 进阶区 hong
Lost in the lock world of MySQL
Unity vscode emmylua configuration error resolution
攻防世界 MISC 進階區 Erik-Baleog-and-Olaf
LOGO special training camp section I identification logo and Logo Design Ideas
Locust performance test - environment construction and use
Attack and Defense World MISC Advanced Area Erik baleog and Olaf
达梦数据凭什么被称为国产数据库“第一股”?
【愚公系列】2022年7月 Go教学课程 003-IDE的安装和基本使用
随机推荐
安装人大金仓数据库
La prospérité est épuisée, les choses sont bonnes et mauvaises: Où puis - je aller pour un chef de station personnel?
达梦数据凭什么被称为国产数据库“第一股”?
LOGO特训营 第四节 字体设计的重要性
leetcode 72. Edit distance edit distance (medium)
醒悟的日子,我是怎么一步一步走向软件测试的道路
Interview essential leetcode linked list algorithm question summary, whole process dry goods!
攻防世界 MISC 进阶区 Ditf
About stack area, heap area, global area, text constant area and program code area
PMO: compare the sample efficiency of 25 molecular optimization methods
页面关闭前,如何发送一个可靠请求
Advanced area of attack and defense world misc 3-11
Breakpoint debugging under vs2019 c release
idea中pom.xml依赖无法导入
Recommendation of mobile app for making barcode
NFT insider 64: e-commerce giant eBay submitted an NFT related trademark application, and KPMG will invest $30million in Web3 and metauniverse
Shell 脚本实现应用服务日志入库 Mysql
SPSS安装激活教程(包含网盘链接)
Lost in the lock world of MySQL
共创软硬件协同生态:Graphcore IPU与百度飞桨的“联合提交”亮相MLPerf