-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlane_visualization.py
More file actions
270 lines (248 loc) · 9.88 KB
/
lane_visualization.py
File metadata and controls
270 lines (248 loc) · 9.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
#!/usr/bin/env python3
# encoding: utf-8
"""
tests.test_path
~~~~~~~~~~~~~~~
:author: Wannes Meert
:copyright: Copyright 2017-2018 DTAI, KU Leuven and Sirris.
:license: Apache License, Version 2.0, see LICENSE for details.
"""
# import sys
import os
# import logging
from pathlib import Path
import numpy as np
# import leuvenmapmatching as mm
from leuvenmapmatching.map.inmem import InMemMap
from leuvenmapmatching.matcher.simple import SimpleMatcher
from leuvenmapmatching import visualization as mmviz
from leuvenmapmatching.matcher.distance import DistanceMatcher
# logger = mm.logger
# directory = None
# def test_path1():
# path = [(0.8, 0.7), (0.9, 0.7), (1.1, 1.0), (1.2, 1.5), (1.2, 1.6), (1.1, 2.0),
# (1.1, 2.3), (1.3, 2.9), (1.2, 3.1), (1.5, 3.2), (1.8, 3.5), (2.0, 3.7),
# (2.1, 3.3), (2.4, 3.2), (2.6, 3.1), (2.9, 3.1), (3.0, 3.2), (3.1, 3.8),
# (3.0, 4.0), (3.1, 4.3), (3.1, 4.6), (3.0, 4.9)]
# # path_sol = ['A', ('A', 'B'), 'B', ('B', 'D'), 'D', ('D', 'E'), 'E', ('E', 'F')]
# path_sol_nodes = ['A', 'B', 'D', 'E', 'F']
# mapdb = InMemMap("map", graph={
# "A": ((1, 1), ["B", "C"]),
# "B": ((1, 3), ["A", "C", "D"]),
# "C": ((2, 2), ["A", "B", "D", "E"]),
# "D": ((2, 4), ["B", "C", "D", "E"]),
# "E": ((3, 3), ["C", "D", "F"]),
# "F": ((3, 5), ["D", "E"])
# }, use_latlon=False)
# matcher = SimpleMatcher(mapdb, max_dist=None, min_prob_norm=None,
# non_emitting_states=False, only_edges=False)
# path_pred, _ = matcher.match(path, unique=True)
# matcher.print_lattice_stats()
# matcher.print_lattice()
# mmviz.plot_map(mapdb, matcher=matcher, show_labels=True, show_matching=True,
# show_graph=True, show_lattice=True,
# filename=str(directory / "test_path1.png"))
# # assert path_pred == path_sol, f"Paths not equal:\n{path_pred}\n{path_sol}"
# nodes_pred = matcher.path_pred_onlynodes
# assert nodes_pred == path_sol_nodes, f"Nodes not equal:\n{nodes_pred}\n{path_sol_nodes}"
# def test_path1_dist():
# path = [(0.8, 0.7), (0.9, 0.7), (1.1, 1.0), (1.2, 1.5), (1.2, 1.6), (1.1, 2.0),
# (1.1, 2.3), (1.3, 2.9), (1.2, 3.1), (1.5, 3.2), (1.8, 3.5), (2.0, 3.7),
# (2.1, 3.3), (2.4, 3.2), (2.6, 3.1), (2.9, 3.1), (3.0, 3.2), (3.1, 3.8),
# (3.0, 4.0), (3.1, 4.3), (3.1, 4.6), (3.0, 4.9)]
# # path_sol = ['A', ('A', 'B'), 'B', ('B', 'D'), 'D', ('D', 'E'), 'E', ('E', 'F')]
# path_sol_nodes = ['A', 'B', 'D', 'E', 'F']
# mapdb = InMemMap("map", graph={
# "A": ((1, 1), ["B", "C"]),
# "B": ((1, 3), ["A", "C", "D"]),
# "C": ((2, 2), ["A", "B", "D", "E"]),
# "D": ((2, 4), ["B", "C", "D", "E"]),
# "E": ((3, 3), ["C", "D", "F"]),
# "F": ((3, 5), ["D", "E"])
# }, use_latlon=False)
# matcher = DistanceMatcher(mapdb, max_dist=None, min_prob_norm=None,
# obs_noise=0.5,
# non_emitting_states=False)
# matcher.match(path)
# mmviz.plot_map(mapdb, matcher=matcher, show_labels=True, show_matching=True, show_graph=True,
# filename=str(directory / "test_path1_dist.png"))
# nodes_pred = matcher.path_pred_onlynodes
# assert nodes_pred == path_sol_nodes, f"Nodes not equal:\n{nodes_pred}\n{path_sol_nodes}"
def my_test():
path = [(0.8, 0.7), (0.9, 0.7), (1.1, 1.0), (1.2, 1.5), (1.2, 1.6), (1.1, 2.0),
(1.1, 2.3), (1.3, 2.9), (1.2, 3.1), (1.5, 3.2), (1.8, 3.5), (2.0, 3.7),
(2.1, 3.3), (2.4, 3.2), (2.6, 3.1), (2.9, 3.1), (3.0, 3.2), (3.1, 3.8),
(3.0, 4.0), (3.1, 4.3), (3.1, 4.6), (3.0, 4.9)]
# path_sol = ['A', ('A', 'B'), 'B', ('B', 'D'), 'D', ('D', 'E'), 'E', ('E', 'F')]
path_sol_nodes = ['A', 'B', 'D', 'E', 'F']
mapdb = InMemMap("map", graph={
"A": ((1, 1), ["B", "C"]),
"B": ((1, 3), ["A", "C", "D"]),
"C": ((2, 2), ["A", "B", "D", "E"]),
"D": ((2, 4), ["B", "C", "D", "E"]),
"E": ((3, 3), ["C", "D", "F"]),
"F": ((3, 5), ["D", "E"]),
"G": ((2,1),["A","ad"])
}, use_latlon=False)
matcher = SimpleMatcher(mapdb, max_dist=None, min_prob_norm=None,
non_emitting_states=False, only_edges=False)
path_pred, _ = matcher.match(path, unique=True)
# matcher.print_lattice_stats()
# matcher.print_lattice()
nodes_pred = matcher.path_pred_onlynodes
mmviz.plot_map(mapdb, path=path,nodes=nodes_pred, show_labels=True, show_matching=True,
show_graph=True, show_lattice=False,
filename=str(directory / "my_test.png"))
# assert path_pred == path_sol, f"Paths not equal:\n{path_pred}\n{path_sol}"
# nodes_pred = matcher.path_pred_onlynodes
# assert nodes_pred == path_sol_nodes, f"Nodes not equal:\n{nodes_pred}\n{path_sol_nodes}"
def read_mmpath(path,index):
file=open(path, "r")
mmpath=[]
while(True):
line=file.readline()
# print(line)
if len(line)>0:
mmpath.append(int(line))
else:
break
file.close()
return mmpath
def read_trace(path,index):
file=open(path, "r")
# num=file.readline()
# print(num)
trace=[]
start_record=False
tmp=None
while(True):
a=file.readline().replace('\n', '').split(sep=" ")
if len(a)==3:
trace.append((float(a[1]),float(a[2])))
else:
break
file.close()
return trace
def read_road(path):
file=open(path, "r")
# num=int(file.readline())
# print(num)
roads=[]
# for i in range(num):
while True:
road=file.readline().split(sep=" ")[0:-1]
if road:
# print(a)
roads.append(road)
else:
break
file.close()
return roads
def build_neighbor_map(roads,mmpath):
graph={}
for road in roads:
graph=road2graph(road,graph)
# nodes.append(road[0]+'_0')
# graph[road[0]]= ((float(road[7]), float(road[6])), [road[1], road[2]])
# graph[predecessor[0]]= ((float(predecessor[7]), float(predecessor[6])), [predecessor[1], predecessor[2]])
# graph[successor[0]]= ((float(successor[7]), float(successor[6])), [successor[1], successor[2]])
mapdb = InMemMap("map", graph=graph, use_latlon=True)
return mapdb,graph
def build_map(roads,mmpath):
graph={}
roads_id=[int(i[0]) for i in roads]
for index in mmpath:
# print(roads_id)
i=roads_id.index(index)
road=roads[i]
# predecessor=roads[int(roads[index][1])]
# successor=roads[int(roads[index][1])]
graph=road2graph(road,graph)
# nodes.append(road[0]+'_0')
# graph[road[0]]= ((float(road[7]), float(road[6])), [road[1], road[2]])
# graph[predecessor[0]]= ((float(predecessor[7]), float(predecessor[6])), [predecessor[1], predecessor[2]])
# graph[successor[0]]= ((float(successor[7]), float(successor[6])), [successor[1], successor[2]])
mapdb = InMemMap("map", graph=graph, use_latlon=True)
return mapdb,graph
# def build_nodes(graph,trace):
# nodes=[]
# for pos in trace:
# dis=10000
# node=None
# for point in graph:
# x_diff=graph[point][0][0]-pos[0]
# y_diff=graph[point][0][1]-pos[1]
# d_diff=np.sqrt(x_diff**2+y_diff**2)
# # print(x_diff,y_diff)
# if d_diff<dis:
# dis=d_diff
# node=point
# nodes.append(node)
# nodes_unique=[]
# [nodes_unique.append(i) for i in nodes if not i in nodes_unique]
# return nodes_unique
def build_nodes(roads,mm_id,trace):
nodes=[]
roads_id=[int(i[0]) for i in roads]
for i in range(len(trace)):
dis=10000
node=None
# print(roads_id)
road_index=roads_id.index(mm_id[i])
road=roads[road_index]
num=int(road[5])
for j in range(num):
lon=float(road[7+2*j])
lat=float(road[6+2*j])
x_diff=lon-trace[i][1]
y_diff=lat-trace[i][0]
d_diff=np.sqrt(x_diff**2+y_diff**2)
if d_diff<dis:
dis=d_diff
node=road[0]+'_'+str(j)
nodes.append(node)
# print("len nodes:",len(nodes))
# print(nodes)
nodes_unique=[]
[nodes_unique.append(i) for i in nodes if not i in nodes_unique]
# print("len nodes:",len(nodes_unique))
return nodes_unique
def road2graph(road,graph):
num=int(road[5])
for i in range(num):
point_name=road[0]+'_'+str(i)
lon=float(road[7+2*i])
lat=float(road[6+2*i])
link1=road[0]+'_'+str(i-1)
link2=road[0]+'_'+str(i+1)
if i==0:
link1=road[1]
if i==num-1:
link2=road[2]
graph[point_name]= ((lat, lon), [link1, link2])
return graph
#TODO
#dynamic visualization
if __name__ == "__main__":
directory = Path(os.environ.get('TESTDIR', Path(__file__).parent))
print(f"Saving files to {directory}")
# my_test()
roads=read_road("./madmap_candidate_road.txt")
index=[0,1000000]
mm_id=read_mmpath("./mm_out_standard.txt",index)
# print(mmpath)
print("mm_id",len(mm_id))
# trace=read_trace("./trace.txt",index)
trace=read_trace("./lane_out.txt",index)
print("trace",len(trace))
mapdb,graph=build_map(roads,mm_id)
# nodes=build_nodes(roads,mm_id,trace)
mapdb_full,graph_full=build_neighbor_map(roads,mm_id)
mmviz.plot_map(mapdb_full,path=trace,
show_labels=False, show_matching=False,
show_graph=True, show_lattice=False,filename=str(directory / "full_lane.png"),
dpi=300)
mmviz.plot_map(mapdb,path=trace,
show_labels=False, show_matching=False,
show_graph=True, show_lattice=False,filename=str(directory / "mmroads_lane.png"),
dpi=300)