当前位置:网站首页>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
边栏推荐
- PHP short video source code, thumb animation will float when you like it
- Gnawing down the big bone - sorting (II)
- POM in idea XML dependency cannot be imported
- Three stage operations in the attack and defense drill of the blue team
- Shell script implements application service log warehousing MySQL
- LOGO特训营 第一节 鉴别Logo与Logo设计思路
- SPSS安装激活教程(包含网盘链接)
- 剑指 Offer 68 - I. 二叉搜索树的最近公共祖先
- Mysql root 账号如何重置密码
- PostgreSQL server programming aggregation and grouping
猜你喜欢

达梦数据凭什么被称为国产数据库“第一股”?

攻防世界 MISC 进阶区 hong

Embedded development: skills and tricks -- seven skills to improve the quality of embedded software code

Li Kou 98: verify binary search tree

攻防世界 MISC 进阶 glance-50

将QA引入软件开发生命周期是工程师要遵循的最佳实践

Detailed explanation of heap sort code

Attack and defense world misc master advanced zone 001 normal_ png

Unity Xiuxian mobile game | Lua dynamic sliding function (specific implementation of three source codes)

Challenges faced by virtual human industry
随机推荐
In Linux, I call odspcmd to query the database information. How to output silently is to only output values. Don't do this
LOGO特训营 第一节 鉴别Logo与Logo设计思路
The table is backed up in ODPs. Why check m in the metabase_ Table, the logical sizes of the two tables are inconsistent, but the number of
华泰证券是国家认可的券商吗?开户安不安全?
达梦数据凭什么被称为国产数据库“第一股”?
Advanced area of attack and defense world misc 3-11
剑指 Offer 65. 不用加减乘除做加法
How to manage 15million employees easily?
Attack and defense world misc advanced grace-50
SPSS安装激活教程(包含网盘链接)
The proofreading activity of data science on the command line second edition was restarted
How to reset the password of MySQL root account
idea中pom.xml依赖无法导入
About stack area, heap area, global area, text constant area and program code area
Google Earth Engine(GEE)——以MODIS/006/MCD19A2为例批量下载逐天AOD数据逐天的均值、最大值、最小值、标准差、方差统计分析和CSV下载(北京市各区为例)
蓝队攻防演练中的三段作战
Persistence mechanism of redis
Attack and Defense World MISC Advanced Area Erik baleog and Olaf
Postgresqlql advanced skills pivot table
攻防世界 misc 高手进阶区 a_good_idea