{{format('0')}} {{format('310')}} {{format('3156')}}

【redis】三大特殊类型 geospatial - 地理空间 [ 编程杂谈 ]

大数据男孩 文章 正文

想做一个技术博客,奈何实力不够
分享

明妃

{{nature("2022-08-14 17:23:19")}}更新

说明

常应用于 附近的人两地距离计算朋友圈定位 ...

中文文档:http://redis.cn/commands/geoadd.html

将有效的地理空间位置(经度,纬度,名称)添加到指定的 Key 中,这些 Key 会存到 Hset(有序集合) 中。

  • 有效的经度-180度到180度。
  • 有效的纬度-85.05112878度到85.05112878度。
  • 该类型的底层实现Hset (有序聚合),所以也能使用有序集合的命令操作

常用 六大命令

地理位置的坐标

可以在网络上 下载完整的信息进行添加

geoadd key longitude latitude member [longitude latitude member ...]

127.0.0.1:6379> geoadd china:city 104.065735 30.659462 chengdu
(integer) 1
127.0.0.1:6379> geoadd china:city 116.405285 39.904989 beijing 114.085947 22.547 shenzhen
(integer) 2

[mark]()

获取地理位置的坐标

geopos key member [member]

127.0.0.1:6379> geopos china:city beijing
1) 1) "116.40528291463851929"
   2) "39.9049884229125027"
127.0.0.1:6379> geopos china:city beijing shenzhen
1) 1) "116.40528291463851929"
   2) "39.9049884229125027"
2) 1) "114.08594459295272827"
   2) "22.54699993773966327"

获取两地距离

key member1 member2 [unit]

  • [unit]:m(米)、km(千米)、mi(英里)、ft(尺)
127.0.0.1:6379> geodist china:city beijing shenzhen km
"1943.0240"

获取附近的位置集合

需要给 ==经纬度== 通过半径来返回位置集合

georadius key longitude latitude raidus m|km|ft|mi [withcoord][withdist][withhash][COUNT count][asc|desc][store key][storedist key]

下列可选参数是在返回结果嵌套一层显示

  • [withcoord]:返回结果显示位置经纬度
  • [withdist]:返回结果显示距离
  • [COUNT count]:指定返回结果数量
  • [withhash]:返回结果哈希
  • [asc|desc]:根据中心,由近到远|由远到近 返回结果
127.0.0.1:6379> georadius china:city 110 30 1000 km
1) "chengdu"
2) "shenzhen"
127.0.0.1:6379> georadius china:city 110 30 1000 km withcoord withdist withhash asc count 2
1) 1) "chengdu"
   2) "574.3396"
   3) (integer) 4026137794127754
   4) 1) "104.06573742628097534"
      2) "30.65946118872339099"
2) 1) "shenzhen"
   2) "923.4929"
   3) (integer) 4046433733682118
   4) 1) "114.08594459295272827"
      2) "22.54699993773966327"

获取附近的位置集合

需要给 ==位置== 通过半径来返回位置集合

georadiusbymember key member raidus m|km|ft|mi [withcoord][withdist][withhash][COUNT count][asc|desc][store key][storedist key]

下列可选参数是在返回结果嵌套一层显示

  • [withcoord]:返回结果显示位置经纬度
  • [withdist]:返回结果显示距离
  • [COUNT count]:指定返回结果数量
  • [withhash]:返回结果哈希
  • [asc|desc]:根据中心,由近到远|由远到近 返回结果
127.0.0.1:6379> georadiusbymember china:city beijing 2000 km
1) "chengdu"
2) "shenzhen"
3) "beijing"
127.0.0.1:6379> georadiusbymember china:city beijing 2000 km withcoord withdist withhash asc count 2
1) 1) "beijing"
   2) "0.0000"
   3) (integer) 4069885370671010
   4) 1) "116.40528291463851929"
      2) "39.9049884229125027"
2) 1) "chengdu"
   2) "1517.9907"
   3) (integer) 4026137794127754
   4) 1) "104.06573742628097534"
      2) "30.65946118872339099"

获取位置HASH

二维经纬度信息,转换成一维字符串,返回的11位哈希值字符串越相似,说明距离越近

geohash key member [member]

127.0.0.1:6379> GEOHASH china:city beijing chengdu shenzhen
1) "wx4g0b7xrt0"
2) "wm6n2j6k730"
3) "ws10k0dcg10"
评论 0
0
{{userInfo.data?.nickname}}
{{userInfo.data?.email}}
TOP 2
Spark 2.0 单机模式与集群模式 安装

{{nature('2020-01-02 16:47:07')}} {{format('12518')}}人已阅读

TOP 3
Office 2016 Pro Plus 激活

{{nature('2019-12-11 20:43:10')}} {{format('9405')}}人已阅读

TOP 4
Linux上 MySQL 开启远程登陆的两种方法

{{nature('2019-12-26 17:20:52')}} {{format('7398')}}人已阅读

TOP 5
Linux 安装 MySQL 5.7

{{nature('2019-12-26 16:03:55')}} {{format('4876')}}人已阅读

目录

标签云

Redis geospatial

一言

# {{hitokoto.data.from || '来自'}} #
{{hitokoto.data.hitokoto || '内容'}}
作者:{{hitokoto.data.from_who || '作者'}}
自定义UI
配色方案

侧边栏