網(wǎng)站首頁 編程語言 正文
1. 需要的庫, redis
pip install redis
2. 連接Redis
import redis class RedisCtrl(object): @staticmethod def connect(config): pool = redis.ConnectionPool( host=config['host'], db=config['db'], port=config['port'], password=config['password'], ) return redis.StrictRedis(connection_pool=pool) rd = RedisCtrl.connect({ "db": 1, "port": "6379", "password": "password", "host": "", })
3. 計(jì)算
把已知的地址經(jīng)緯度導(dǎo)入到Redis中, 用于計(jì)算
rd.geoadd(name="集合名稱", values=["經(jīng)度", "維度", "地址名稱"])
一次添加多個(gè)位置values=[經(jīng)度1, 維度1, 地址名稱1, 經(jīng)度2, 維度2, 地址名稱2, 經(jīng)度3, 緯度3, 地址名稱3......]
。
4. 查看已添加位置的經(jīng)緯度
result = rd.geopos("集合名稱", "地址名稱1", "地址名稱2") print(result) # [(地址1的經(jīng)度, 地址1的維度), (地址2的經(jīng)度, 地址2的維度)]
如果未查到, 會(huì)返回None
。
result = rd.geopos("集合名稱", "錯(cuò)誤名稱1", "錯(cuò)誤名稱2") print(result) # [None, None]
5. 計(jì)算兩地之間的距離
rd.geodist("集合名稱", "地址名稱1", "地址名稱2", unit="km")
unit:距離的單位, 可選("m": 米, "km": 千米, "mi": 英里, "ft": 英尺)
, 默認(rèn)值為m
。
6. 搜索范圍內(nèi)的地址
result = rd.georadius(name="集合名稱", longitude="經(jīng)度", latitude="維度", radius="半徑距離", unit="半徑單位", sort='ASC', count=10, withdist=True, withcoord=True) print(result) # [[b'地址名稱', 距離, (經(jīng)度, 維度)], [b'shanghai', 0.1774, (121.4813420176506, 31.235159732038248)]]
-
sort: 排序方式,
ASC
由近到遠(yuǎn),DESC
由遠(yuǎn)到近。 - count: 指定返回前幾條數(shù)據(jù)。
- withdist: 是否返回距離。
- withcoord: 是否返回經(jīng)緯度信息。
注意: 返回的數(shù)據(jù)其中的地址名稱是
byte
類型的, 使用時(shí)需要decode('utf-8)
處理。
原文鏈接:https://blog.csdn.net/weixin_44649870/article/details/126668523
相關(guān)推薦
- 2023-10-17 淺記前端遇到的問題 input上傳文件屬性以及第三方組件庫上傳文件屬性
- 2022-03-27 3個(gè)適合新手練習(xí)的python小游戲_python
- 2022-02-21 React事件綁定詳解_React
- 2022-12-04 Android性能優(yōu)化死鎖監(jiān)控知識(shí)點(diǎn)詳解_Android
- 2022-07-30 淺談Redis緩沖區(qū)機(jī)制_Redis
- 2022-05-08 python添加列表元素append(),extend()及?insert()_python
- 2022-10-11 CFS調(diào)度算法調(diào)度時(shí)機(jī)的理解
- 2022-05-25 如何把自己寫的jar包打進(jìn)本地maven倉庫呢(也是springboot項(xiàng)目怎么打成SDK)
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支