论文笔记:PyTrack: A Map-Matching-Based Python Toolbox for Vehicle Trajectory Reconstruction_UQI-LIUWJ的博客-CSDN博客4
0 包的安装
官网的两种方式我都试过,装是能装成功,但是python import PyTrack包的时候还是显示找不到Pytrack
# conda
conda install pytrack
# or PyPI
pip install PyTrack-lib
于是使用这种安装方式,成功了:
pip 另一种安装方法:python setup.py install_python setup.py bdist_wheel did not run successful_UQI-LIUWJ的博客-CSDN博客
1 GPS数据导入
import numpy as np
import pandas as pd
df=pd.DataFrame([{'datetime':'23-04-21 16:46:19:583000','lat':43.759650,'lon':11.291561},
{'datetime':'23-04-21 16:46:36:570000','lat':43.759645,'lon':11.291544},
{'datetime':'23-04-21 16:46:52:647000','lat':43.759671,'lon':11.291162},
{'datetime':'23-04-21 16:47:37:568000','lat':43.759677,'lon':11.291148},
{'datetime':'23-04-21 16:47:49:639000','lat':43.759691,'lon':11.290932},
{'datetime':'23-04-21 17:12:37:573000','lat':43.779596,'lon':11.254733},
{'datetime':'23-04-21 17:12:51:592000','lat':43.779583,'lon':11.254295},
{'datetime':'23-04-21 17:13:05:572000','lat':43.779206,'lon':11.253978},
{'datetime':'23-04-21 17:13:20:592000','lat':43.779205,'lon':11.253974},
{'datetime':'23-04-21 17:13:36:590000','lat':43.778464,'lon':11.253364}])
df
2 GPS数据处理
latitude = df["lat"].to_list()
longitude = df["lon"].to_list()
points = [(lat, lon) for lat, lon in zip(latitude, longitude)]
points
'''
[(43.75965, 11.291561),
(43.759645, 11.291544),
(43.759671, 11.291162),
(43.759677, 11.291148),
(43.759691, 11.290932),
(43.779596, 11.254733),
(43.779583, 11.254295),
(43.779206, 11.253978),
(43.779205, 11.253974),
(43.778464, 11.253364)]
'''
north, east = np.max(np.array(points),axis=0)
south, west = np.min(np.array(points),axis=0)
north,south,east,west
#(43.779596, 43.759645, 11.291561, 11.253364)
3 获得路网Graph
from pytrack.graph import graph, distance
from pytrack.analytics import visualization
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')
#Downloaded 1,767.31kB
3.1 显示提取的图
map=visualization.Map(location=(np.mean(latitude),np.mean(longitude)))
map.add_graph(G,plot_nodes=True)
map
4 提取候选点
- get_candidates方法返回两个输出
- graph的插值版本G_interp
- 候选点的字典candidates
- 下面0,1 ,7,8都是一个candidate;2,3,4,5,6是4个;9是3个
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)
candidates
'''
{0: {'observation': (43.75965, 11.291561),
'osmid': [135115743],
'edge_osmid': [590848305],
'candidates': [(43.75963763247117, 11.291543159878621)],
'candidate_type': array([False]),
'dists': [1.9859410359888336]},
1: {'observation': (43.759645, 11.291544),
'osmid': [135115743],
'edge_osmid': [590848305],
'candidates': [(43.75963763247117, 11.291543159878621)],
'candidate_type': array([False]),
'dists': [0.8220066596419578]},
2: {'observation': (43.759671, 11.291162),
'osmid': [1366820392, 831342769, 831342769, 245816382],
'edge_osmid': [158183875, 160047415, 202760017, 590848305],
'candidates': [(43.759700520685946, 11.290803894486569),
(43.7596557, 11.290801),
(43.7596557, 11.290801),
(43.75964691271334, 11.29117208534078)],
'candidate_type': array([False, False, False, False]),
'dists': [28.94629461607051,
29.041910651129065,
29.041910651129065,
2.798176615616366]},
3: {'observation': (43.759677, 11.291148),
'osmid': [1366820392, 831342769, 831342769, 245816382],
'edge_osmid': [158183875, 160047415, 202760017, 590848305],
'candidates': [(43.759700520685946, 11.290803894486569),
(43.7596557, 11.290801),
(43.7596557, 11.290801),
(43.75964691271334, 11.29117208534078)],
'candidate_type': array([False, False, False, False]),
'dists': [27.7587065301145,
27.968156168366924,
27.968156168366924,
3.864490374081274]},
4: {'observation': (43.759691, 11.290932),
'osmid': [1366820392, 831342769, 831342769, 1751266336],
'edge_osmid': [158183875, 160047415, 202760017, 590848305],
'candidates': [(43.759700520685946, 11.290803894486569),
(43.7596557, 11.290801),
(43.7596557, 11.290801),
(43.75965309954146, 11.290924702315552)],
'candidate_type': array([False, False, False, False]),
'dists': [10.342517462643924,
11.229036268613909,
11.229036268613909,
4.254901893095896]},
5: {'observation': (43.779596, 11.254733),
'osmid': [1082477038, 1196043488, 699408714, 1082477038],
'edge_osmid': [23338921, 24695287, 24933636, 203924197],
'candidates': [(43.7796997, 11.2544238),
(43.77956558718794, 11.254738857110752),
(43.779738600417716, 11.254457384444233),
(43.7796997, 11.2544238)],
'candidate_type': array([False, False, False, False]),
'dists': [27.371092347460355,
3.4142911971419823,
27.222205203514605,
27.371092347460355]},
6: {'observation': (43.779583, 11.254295),
'osmid': [1082477038, 1082477038, 1082477038, 432026454],
'edge_osmid': [23338921, 24695287, 24933636, 203924197],
'candidates': [(43.7796997, 11.2544238),
(43.7796997, 11.2544238),
(43.7796997, 11.2544238),
(43.77958438083008, 11.254328000262998)],
'candidate_type': array([False, False, False, False]),
'dists': [16.59261899959034,
16.59261899959034,
16.59261899959034,
2.6538256098028095]},
7: {'observation': (43.779206, 11.253978),
'osmid': [1531471656],
'edge_osmid': [203924197],
'candidates': [(43.77919997631304, 11.254008676575015)],
'candidate_type': array([False]),
'dists': [2.552298511854619]},
8: {'observation': (43.779205, 11.253974),
'osmid': [1531471656],
'edge_osmid': [203924197],
'candidates': [(43.77919997631304, 11.254008676575015)],
'candidate_type': array([False]),
'dists': [2.8394681876277263]},
9: {'observation': (43.778464, 11.253364),
'osmid': [568144517, 132324140, 132324140],
'edge_osmid': [27317795, 491331618, 915510037],
'candidates': [(43.77843868125, 11.25338133125),
(43.7782499, 11.2532268),
(43.7782499, 11.2532268)],
'candidate_type': array([False, False, False]),
'dists': [3.14040435143449, 26.231663705314208, 26.231663705314208]}}
'''
4.1 可视化candidate
maps.add_graph(G, plot_nodes=True)
maps.draw_candidates(candidates, 30)
maps
5 创建Trellis图
根据上面的candidates创建相应的trellis
trellis = mpmatching_utils.create_trellis(candidates)
5.1 绘制Trellis图
trellis_draw = visualization.draw_trellis(trellis, figsize=(3, 7))
trellis_draw
每个点的candidate个数和前面candidates中每个点的candidate数目一致
6 进行地图匹配
# Perform the map-matching process
path_prob, predecessor = mpmatching.viterbi_search(G_interp, trellis, "start", "target")
6.1 绘制地图匹配图
# Plot map-matching results
maps.draw_path(G_interp, trellis, predecessor)
maps