使用 Redis GEO 储存地理位置 — blog.huangz.me
source link: https://blog.huangz.me/2019/redis-geo-location.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
使用 Redis GEO 储存地理位置¶
本文摘录自《Redis使用手册》, 详情请见: RedisGuide.com 。
很多社交网站都提供了与地理位置相关的功能, 比如记录用户自己的位置、获取指定用户的位置, 又或者查找指定范围内的其他用户等等。
在学习了 GEOADD
、 GEOPOS
和 GEODIST
这三个命令之后,
我们同样可以构建出一个具有基本功能的用户地理位置程序,
该程序能够记录用户所在的位置、获取指定用户所在的位置以及计算两个用户之间的直线距离,
具体的实现如代码清单 9-1 所示。
代码清单 9-1 具有基本功能的用户地理位置程序:/geo/location.py
import random USER_LOCATION_KEY = "user_locations" class Location: def __init__(self, client): self.client = client self.key = USER_LOCATION_KEY def pin(self, user, longitude, latitude): """ 记录指定用户的坐标。 """ self.client.geoadd(self.key, longitude, latitude, user) def get(self, user): """ 获取指定用户的坐标。 """ position_list = self.client.geopos(self.key, user) # geopos() 允许用户输入多个位置,然后以列表形式返回各个位置的坐标。 # 因为我们这里只传入了一个位置,所以只需要取出列表的第一个元素即可。 if len(position_list) != 0: return position_list[0] def calculate_distance(self, user_a, user_b): """ 以公里为单位,计算两个用户之间的直线距离。 """ return self.client.geodist(self.key, user_a, user_b, unit="km")
这个程序所做的就是使用 GEOADD
命令把用户的 ID 以及用户所在的经纬度关联起来,
然后使用 GEOPOS
命令去获取用户所在的经纬度,
又或者使用 GEODIST
命令去计算两个用户之间的直线距离。
通过执行以下代码, 我们可以创建出相应的用户位置对象, 并使用它记录 peter 、jack 和 tom 这三个用户的地理位置:
>>> from redis import Redis >>> from location import Location >>> client = Redis(decode_responses=True) >>> location = Location(client) >>> location.pin("peter", 113.20996731519699, 23.593675019671288) >>> location.pin("jack", 113.22784155607224, 23.125598202060807) >>> location.pin("tom", 113.40603142976761, 22.511156445825442)
然后通过执行以下代码, 获取 peter 和 jack 的坐标:
>>> location.get("peter") (113.20996731519699, 23.593675019671288) >>> location.get("jack") (113.22784155607224, 23.125598202060807)
最后通过执行以下代码, 计算 peter 和 jack 之间的直线距离, 以及 peter 和 tom 之间的直线距离:
>>> location.calculate_distance("peter", "jack") 52.0944 >>> location.calculate_distance("peter", "tom") 122.0651
Recommend
-
32
Note 本文摘录自《Redis使用手册》, 详情请见: RedisGuide.com 。 很多...
-
13
使用 Redis 流实现消息队列¶ 本文摘录自《Redis使用手册》, 更多信息请见: RedisGuide.com 。 在介绍了 Red...
-
9
使用 Redis 实现自动补全¶ 本文摘录自即将出版的《Redis使用手册》, 详情请见: RedisGuide.com 。 包含大量...
-
6
使用 Redis 储存社交关系¶ 本文摘录自即将出版的《Redis使用手册》, 详情请见: RedisGuide.com 。 微博...
-
6
使用 Redis 实现投票功能¶ 本文摘录自即将出版的《Redis使用手册》, 详情请见: RedisGuide.com 。 问答网站、文章...
-
14
使用 Redis 实现登录会话¶ 本文摘录自即将出版的《Redis使用手册》, 详情请见: RedisGuide.com 。 为了方便...
-
12
使用 Redis 实现锁¶ 本文摘录自即将出版的《Redis使用手册》, 详情请见: RedisGuide.com 。 锁是一种同步机制, 它可...
-
5
使用 redis-py 储存地理位置数据¶ Redis 3.2 版本的其中一个重要更新就是提供了对地理位置(GEO)数据的支持...
-
7
Redis GEO 源码注释¶ Redis 的 GEO 特性主要由 geo.h 和 geo.c 两个文件实现, 本文对这两个文件进行了详细的注释。
-
11
Redis GEO 特性简介¶ Redis 的 GEO 特性将在 Redis 3.2 版本释出, 这个功能可以将用户给定的地理位置信息储存起来, 并对这些信息进行操作。 本文将对 Redis 的 GE...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK