geospatial类型详解
geospatial类型详解
添加地理位置
# geoadd
# 规则:南极北极无法添加,一般会直接下载城市数据,通过java程序一次性导入进去
# 一般都是
127.0.0.1:6379[3]> geoadd China:city 116.397128 39.916527 Beijing
(integer) 1
127.0.0.1:6379[3]> geoadd China:city 121.48941 31.40527 Shanghai
(integer) 1
127.0.0.1:6379[3]> geoadd China:city 108.93425 34.23053 Xi'an
Invalid argument(s)
127.0.0.1:6379[3]> geoadd China:city 108.93425 34.23053 Xian
(integer) 1
127.0.0.1:6379[3]> geoadd China:city 120.21201 30.2084 Hangzhou
(integer) 1
127.0.0.1:6379[3]> geoadd China:city 113.27324 23.15792 Guangzhou
(integer) 1
127.0.0.1:6379[3]> geoadd China:city 113.88308 22.55329 Shenzhen
获取
# geoops
127.0.0.1:6379[3]> geopos China:City Beijing
1) (nil)
127.0.0.1:6379[3]> geopos China:city Beijing
1) 1) "116.39712899923324585"
2) "39.91652647362980844"
127.0.0.1:6379[3]> geopos China:city beijing
1) (nil)
127.0.0.1:6379[3]> geopos China:city Xian
1) 1) "108.93425256013870239"
2) "34.23053097599082406"
计算距离
# geodist 单位km千米 mi英里 ft英尺
127.0.0.1:6379[3]> geodist China:city Beijing Shanghai
"1052105.5643"
127.0.0.1:6379[3]> geodist China:city Beijing Shanghai km
"1052.1056"
127.0.0.1:6379[3]> geodist China:city Beijing Xian km
"915.0863"
找出一定半径内的地方
# georadius 可用于附近的人
127.0.0.1:6379[3]> georadius China:city 110 30 1000 km # 获取 110,30这个经纬度为中心,方圆1000km内的城市
1) "Xian"
2) "Shenzhen"
3) "Guangzhou"
4) "Hangzhou"
127.0.0.1:6379[3]> georadius China:city 110 30 500 km # 获取 110,30这个经纬度为中心,方圆500km内的城市
1) "Xian"
127.0.0.1:6379[3]> georadius China:city 110 30 500 km withcoord# 获取 110,30这个经纬度为中心,方圆500km内的城市 带上经纬度信息
1) 1) "Xian"
2) 1) "108.93425256013870239"
2) "34.23053097599082406"
127.0.0.1:6379[3]> georadius China:city 110 30 500 km withcoord withdist # 获取 110,30这个经纬度为中心,方圆500km内的城市 带上经纬度信息和距离信息
1) 1) "Xian"
2) "481.1278"
3) 1) "108.93425256013870239"
2) "34.23053097599082406"
127.0.0.1:6379[3]> georadius China:city 110 30 500 km withcoord withdist withhash # 获取 110,30这个经纬度为中心,方圆500km内的城市 带上经纬度信息和距离信息还有hash值
1) 1) "Xian"
2) "481.1278"
3) (integer) 4040115270369361
4) 1) "108.93425256013870239"
2) "34.23053097599082406"
127.0.0.1:6379[3]> georadius China:city 110 30 1000 km withcoord withdist count 2# 获取 110,30这个经纬度为中心,方圆1000km内的城市 带上经纬度信息和距离信息 只筛选出2个
1) 1) "Xian"
2) "481.1278"
3) 1) "108.93425256013870239"
2) "34.23053097599082406"
2) 1) "Guangzhou"
2) "827.6084"
3) 1) "113.27324062585830688"
2) "23.1579209662846921"
以某个成员为中心找到方圆一定距离的数据
# georadiusbymember
127.0.0.1:6379[3]> georadiusbymember China:city Beijing 1000 km
1) "Beijing"
2) "Xian"
返回哈希值
# geohash 返回11个数值的 geohash字符串
# 将二维的经纬度转换为一维的字符串,如果两个位置越近,两个字符串越接近
127.0.0.1:6379[3]> geohash China:city Beijing
1) "wx4g0dtf9e0"
底层实现
geo的底层实现是zset
删除
# zrange
# zrem
127.0.0.1:6379[3]> zrange China:city 0 -1
1) "Xian"
2) "Shenzhen"
3) "Guangzhou"
4) "Hangzhou"
5) "Shanghai"
6) "Beijing"
127.0.0.1:6379[3]> zrem China:city Beijing
(integer) 1
127.0.0.1:6379[3]> zrange China:city 0 -1
1) "Xian"
2) "Shenzhen"
3) "Guangzhou"
4) "Hangzhou"
5) "Shanghai"