1 轨迹计算
1.1 使用geopy
geopy模块常用于定位全球地址、以及经纬度相关的转换与计算,详细请参考:
https://pypi.org/project/geopy/
1.2 安装
pip install geopy
1.3 根据经纬度计算距离
Geopy可以使用测地线距离或大圆距离计算两点之间的测地线距离, 默认的测地线距离可用作函数geopy.distance.distance。
下面是测地线距离的示例用法,取对 元组数量:(lat, lon)
from geopy.distance import geodesic
pre = (39.053535, 113.661705)
after = (39.054092,113.660706)
print(geodesic(pre, after).miles)
输出结果:0.06605704759005734米
1.4 拓展:根据经纬度反向获取地址
from geopy.geocoders import Nominatim
geolocator = Nominatim(user_agent="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36")
location = geolocator.reverse("39.053795, 113.66118")
print(location.address)
输出结果:S205, 繁峙县, 忻州市, 山西省, 中国
2 应用
经过查看论文研究,很多用户会经常徒步旅行,同时呢会使用两步走app来记录自己的行程轨迹,这样我们可以分析用户的徒步过程的一些状态
可以根据用户轨迹相关信息,进行GIS空间分析
具体可参考
https://m.fx361.com/news/2022/1124/13935932.html
数据示例:
距离计算代码:
def calculate_gj():
print("开始计算")
tp = [i for i in readData().values]
print("总条数:%s"%len(tp))
all_tp_dict = {value: [tp[i] for i in range(len(tp)) if tp[i][-1] == value] for value in set([i[-1] for i in tp])}
print("开始循环")
for all_tp in all_tp_dict.values():
# print(all_tp)
tp = [i for i in all_tp]
userName = tp[0][0]
dt = tp[0][1]
# tp = [i for i in readData().values]
for t in range(len(tp) - 1):
t1 = t
t2 = t + 1
stopTime = (tp[t2][3] - tp[t1][3]) / 1000
stopDistance = geodesic((tp[t1][5], tp[t1][2]), (tp[t2][5], tp[t2][2])).m
speed = round((tp[t2][6] + tp[t1][6])/2,2)
if ((stopDistance < 50 and stopTime <= 60*60) or (stopDistance < 50 and stopTime>0)) and (speed<=1.5 and speed>=0):
result = {}
result["userName"] = userName
result["dt"] = dt
result["停留时间(分钟)"] = round(stopTime / 60, 2)
result["纬度"] = tp[t1][5]
result["经度"] = tp[t1][2]
result["距离"] = round(stopDistance, 2)
result["速度"] = round((tp[t2][6] + tp[t1][6])/2,2)
# result.update(rr)
print(result)
resultList.append(result)
图片无法展示,具体可参考:https://mp.weixin.qq.com/s?__biz=Mzk0NzI4MDc5Mw==&mid=2247485599&idx=1&sn=10386d8825722aa73cc56d3f762a52ea&chksm=c3780669f40f8f7f56c3cb86f6e5d0453af9bf11446f8dc06107947db34cbd7a174b4273862b#rd