1 distance
1.1 add_edge_lengths
pytrack.graph.distance.add_edge_lengths(G, precision=3)
将每条边的长度加到图里面去
1.1.1 主要参数
G | 路网图 |
precision | 每一条边长度,保持几位小数 |
1.2 enlarge_bbox
pytrack.graph.distance.enlarge_bbox(north, south, west, east, dist)
以north、south、west、east为边界,各向外扩张dist,构成一个边界box
north | 边界框的最北纬度 |
south | 边界框的最南纬度 |
west | 边界框的最西经度 |
east | 边界框的最东经度 |
dist | 以米为单位的距离,表示边界框要扩展多少 |
from pytrack.graph import graph, distance
distance.enlarge_bbox(1.345391, 1.33671, 103.700013, 103.678652, 500)
'''
(1.3498876016774644,
1.3322133983225357,
103.69551516635698,
103.68314983364301)
'''
1.3 get_bearing
pytrack.graph.distance.get_bearing(lat1, lon1, lat2, lon2)
计算两个点(lat1,lon1)到(lat2,lon2)之间的运行方位(正北为0正南为180)向东多少度就是正多少;向西多少度就是负多少
from pytrack.graph import graph, distance
distance.get_bearing(1.336710,103.700013,1.336710,103.800013)
#向东
#89.9988336059857
distance.get_bearing(1.336710,103.900013,1.336710,103.800013)
#向西
#-89.9988336059857
distance.get_bearing(1.336710,103.900013,1.326710,103.900013)
#向南
#180
distance.get_bearing(1.336710,103.900013,1.346710,103.900013)
#向北
#0
1.4 haversine_dist
pytrack.graph.distance.haversine_dist(lat1, lon1, lat2, lon2, earth_radius=6371009)
计算两个点(lat1,lon1)到(lat2,lon2)之间的距离
earth_radius是地球半径
distance.haversine_dist(1.336710,103.900013,1.336710,103.800013)
#11116.482398841283
#单位是米
1.5 interpolate_graph
在图中间隔一定距离插入一些点
pytrack.graph.distance.interpolate_graph(G, dist=1)
dist表示差补后每个点和下一个点之间的距离
north, south, west, east=1.345391, 1.33671, 103.700013, 103.678652
bbox=distance.enlarge_bbox(north, south, west, east, 500)
G = graph.graph_from_bbox(*distance.enlarge_bbox(north, south, west, east, 500), simplify=True, network_type='drive')
map=visualization.Map(location=(np.mean(latitude),np.mean(longitude)))
map.add_graph(G,plot_nodes=True)
map
G1=distance.interpolate_graph(G,dist=5)
map=visualization.Map(location=(np.mean(latitude),np.mean(longitude)))
map.add_graph(G1,plot_nodes=True)
map
2 Graph
2.1 graph_from_bbox
pytrack.graph.graph.graph_from_bbox(north, south, west, east,
simplify=True,
network_type='drive',
buffer_dist=0)
在一个bounding box内创建一个graph
north, south, west, east | bounding box的北、南、西、东 四端 |
simplify | 是否简化图 |
network_type | OSM路网图的类型 |
buffer_dist | 类似于enlarge_bbox里面的dist,bounding box扩大的距离(以米为单位) |
3 Matching
3.1 candidate
表示候选元素的类
classpytrack.matching.candidate.Candidate(
node_id,
edge_osmid,
obs,
great_dist,
coord)
node_id | OSM 点的id |
edge_osmid | OSM 边的id |
obs | 实际的GPS坐标(观测点的坐标) |
great_dist | 观测点和候选点之间的距离 |
coord | 候选点坐标 |
3.2 get_candidates
获得HMM map-matching 的候选点
pytrack.matching.candidate.get_candidates(
G,
points,
interp_dist=1,
closest=True,
radius=10)
G | 路网图 |
points | 实际的GPS点(观测点) |
interp_dist | 1.5 interpolate_graph的dist,dist越小,越精准,但是需要的时间越长 |
closest | 如果为真,那么每一条边中只有距离观测点最近的点会被考虑 |
radius | 搜寻半径 |
3.2.0 返回结果
返回一个interpolate过的图(即1.5 interpolate_graph的结果图)
以及一个字典,表示各个candidate
3.2.1 radius
from pytrack.matching import candidate, mpmatching_utils, mpmatching
# Extract candidates
G_interp, candidates = candidate.get_candidates(G, points,
interp_dist=5,
closest=True,
radius=30)
maps=visualization.Map(location=(np.mean(latitude),np.mean(longitude)))
maps.add_graph(G,plot_nodes=True)
maps.draw_candidates(candidates, 30)
maps
from pytrack.matching import candidate, mpmatching_utils, mpmatching
# Extract candidates
G_interp, candidates = candidate.get_candidates(G, points,
interp_dist=5,
closest=True,
radius=100)
maps=visualization.Map(location=(np.mean(latitude),np.mean(longitude)))
maps.add_graph(G,plot_nodes=True)
maps.draw_candidates(candidates, 100)
maps
3.2.2 closest
from pytrack.matching import candidate, mpmatching_utils, mpmatching
# Extract candidates
G_interp, candidates = candidate.get_candidates(G, points,
interp_dist=5,
closest=False,
radius=100)
maps=visualization.Map(location=(np.mean(latitude),np.mean(longitude)))
maps.add_graph(G,plot_nodes=True)
maps.draw_candidates(candidates, 100)
maps
3.3 cleaning
3.3.1 park_filter
通过消除在给定时间内保留在特定区域的那些点来消除停车行为。
pytrack.matching.cleaning.park_filter(traj, th_dist=50, th_time=30)
import numpy as np
import pandas as pd
from pytrack.matching import cleaning
df=pd.DataFrame([{'datetime':'2023-04-21 16:46:19:583000','lat':1.336710,'lon':103.700013},
{'datetime':'2023-04-21 16:46:36:570000','lat':1.338919,'lon':103.699627},
{'datetime':'2023-04-21 16:46:52:647000','lat':1.340078,'lon':103.696387},
{'datetime':'2023-04-21 16:47:37:568000','lat':1.340736,'lon':103.693565},
{'datetime':'2023-04-21 16:47:49:639000','lat':1.341959,'lon':103.693457},
{'datetime':'2023-04-21 17:12:37:573000','lat':1.341959,'lon':103.693457},
{'datetime':'2023-04-21 17:12:51:592000','lat':1.341959,'lon':103.693457},
{'datetime':'2023-04-21 17:13:05:572000','lat':1.341959,'lon':103.693457},
{'datetime':'2023-04-21 17:13:20:592000','lat':1.339492,'lon':103.682728},
{'datetime':'2023-04-21 17:13:36:590000','lat':1.339985,'lon':103.678652}])
df['datetime']=df.datetime.apply(datetime.fromisoformat)
cleaning.park_filter(df)
停车行为只保留第一次到达的记录(停下来的record将不会记录)
3.4 map matching
论文笔记:Hidden Markov Map MatchingThrough Noise and Sparseness_UQI-LIUWJ的博客-CSDN博客
3.4.1 viterbi_search
pytrack.matching.mpmatching.viterbi_search(
G,
trellis,
start='start',
target='target',
beta=3,
sigma=4.07)
进行维特比算法,已完成HMM 地图匹配
G | 路网图 |
trellis | 生成的trellis图(后面会说) |
start | 开始点 |
target | 种植点 |
beita |
输出概率中的β |
sigma | 转移概率中的σ |
返回两个内容 joint_prob:各个点形成的轨迹的联合概率
predecessor 每个点的前继节点
3.5 mpmatching_utils
3.5.1 create_matched_path
最匹配的GPS点组成的路径
pytrack.matching.mpmatching_utils.create_matched_path(G, trellis, predecessor)
G是get_candidates之后的G
trellis就是创建的trellis图
predecessor是viterbi算法得到的前继节点
返回两个列表:
组成path的点id集合
组成path的点的坐标集合
3.5.2 create_path
除了只返回create_match_path的第一个列表,其他的都是一样的
3.5.3 create——trellis
创建trellis图
pytrack.matching.mpmatching_utils.create_path(G, trellis, predecessor)
trellis_draw = visualization.draw_trellis(trellis, figsize=(3, 7))
trellis_draw