-
Notifications
You must be signed in to change notification settings - Fork 19
/
ops.py
280 lines (213 loc) · 7.17 KB
/
ops.py
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
270
271
272
273
274
275
276
277
278
279
280
import logging
import time
from copy import deepcopy
from typing import List, NamedTuple, Any
import numpy as np
from yamm.constructs.coordinate import Coordinate
from yamm.constructs.road import Road
from yamm.constructs.trace import Trace
from yamm.maps.map_interface import MapInterface, PathWeight
from yamm.matchers.lcss.constructs import TrajectorySegment, TrajectoryScheme
from yamm.matchers.lcss.utils import merge
from yamm.matchers.matcher_interface import MatchResult
log = logging.getLogger(__name__)
def score(trace: Trace, path: List[Road], distance_epsilon: float) -> float:
"""
computes the similarity score between a trace and a path
:param trace:
:param path:
:param distance_epsilon:
:return:
"""
s = time.time()
m = len(trace.coords)
n = len(path)
if m < 2:
return 0
elif n < 1:
return 0
C = [[0 for i in range(n + 1)] for j in range(m + 1)]
f = trace._frame
distances = np.array([f.distance(r.geom).values for r in path])
for i in range(1, m + 1):
for j in range(1, n + 1):
# dt = road_to_coord_dist(road, coord)
dt = distances[j - 1][i - 1]
if dt < distance_epsilon:
point_similarity = 1 - dt / distance_epsilon
else:
point_similarity = 0
C[i][j] = max(
(C[i - 1][j - 1] + point_similarity), C[i][j - 1], C[i - 1][j]
)
sim_score = C[m][n] / float(min(m, n))
e = time.time()
print(f"SCORE: size: {m * n} \t\t time: {round(e - s, 2)} seconds")
return sim_score
def new_path(
road_map: MapInterface,
trace: Trace,
distance_epsilon: float,
) -> List[Road]:
"""
Computes a shortest time and shortest distance path and returns the path that
most closely matches the trace.
:param road_map:
:param trace:
:param distance_epsilon:
:return:
"""
if len(trace.coords) < 1:
return []
origin = trace.coords[0]
destination = trace.coords[-1]
time_path = road_map.shortest_path(origin, destination, weight=PathWeight.TIME)
dist_path = road_map.shortest_path(origin, destination, weight=PathWeight.DISTANCE)
if time_path == dist_path:
return time_path
if not time_path and not dist_path:
return []
time_score = score(trace, time_path, distance_epsilon)
dist_score = score(trace, dist_path, distance_epsilon)
if dist_score > time_score:
return dist_path
else:
return time_path
def split_trajectory_segment(
road_map: MapInterface,
trajectory_segment: TrajectorySegment,
distance_epsilon: float,
) -> List[TrajectorySegment]:
"""
Splits a trajectory segment based on the provided cutting points.
Merge back any segments that are too short
:param road_map: the road map to match to
:param trajectory_segment: the trajectory segment to split
:param distance_epsilon: the distance epsilon
:return: a list of split segments or the original segment if it can't be split
"""
trace = trajectory_segment.trace
cutting_points = trajectory_segment.cutting_points
def _short_segment(ts: TrajectorySegment):
if len(ts.trace) < 2 or len(ts.path) < 1:
return True
return False
if len(trace.coords) < 2:
# segment is too short to split
return [trajectory_segment]
elif len(cutting_points) < 1:
# no points to cut
return [trajectory_segment]
o = trace.coords[0]
d = trace.coords[-1]
new_paths = []
new_traces = []
# start
scp = cutting_points[0]
new_trace = trace[: scp.trace_index]
new_paths.append(new_path(road_map, new_trace, distance_epsilon))
new_traces.append(new_trace)
# mids
for i in range(len(cutting_points) - 1):
cp = cutting_points[i]
ncp = cutting_points[i + 1]
new_trace = trace[cp.trace_index : ncp.trace_index]
new_paths.append(new_path(road_map, new_trace, distance_epsilon))
new_traces.append(new_trace)
# end
ecp = cutting_points[-1]
new_trace = trace[ecp.trace_index :]
new_paths.append(new_path(road_map, new_trace, distance_epsilon))
new_traces.append(new_trace)
if not any(new_paths):
# can't split
return [trajectory_segment]
elif not any(new_traces):
# can't split
return [trajectory_segment]
else:
segments = [TrajectorySegment(t, p) for t, p in zip(new_traces, new_paths)]
merged_segments = merge(segments, _short_segment)
return merged_segments
def same_trajectory_scheme(
scheme1: TrajectoryScheme, scheme2: TrajectoryScheme
) -> bool:
"""
compares two trajectory schemes for equality
:param scheme1:
:param scheme2:
:return: are the schemes the same?
"""
same_paths = all(map(lambda a, b: a.path == b.path, scheme1, scheme2))
same_traces = all(
map(lambda a, b: a.trace.coords == b.trace.coords, scheme1, scheme2)
)
return same_paths and same_traces
class StationaryIndex(NamedTuple):
i_index: List[int] # i based index on the trace
c_index: List[Any] # coordinate ids
def find_stationary_points(trace: Trace) -> List[StationaryIndex]:
"""
find the positional index of all stationary points in a trace
:param trace:
:return:
"""
f = trace._frame
coords = trace.coords
dist = f.distance(f.shift())
index_collections = []
index = set()
for i in range(1, len(dist)):
d = dist.iloc[i] # distance to previous point
if d < 0.001:
index.add(i - 1)
index.add(i)
else:
# there is distance between this point and the previous
if index:
l_index = sorted(list(index))
cids = [coords[li].coordinate_id for li in l_index]
si = StationaryIndex(l_index, cids)
index_collections.append(si)
index = set()
# catch any group of points at the end
if index:
l_index = sorted(list(index))
cids = [coords[li].coordinate_id for li in l_index]
si = StationaryIndex(l_index, cids)
index_collections.append(si)
return index_collections
def drop_stationary_points(
trace: Trace, stationary_index: List[StationaryIndex]
) -> Trace:
"""
drops stationary points from the trace, keeping the first point
:param trace:
:param stationary_index:
:return:
"""
for si in stationary_index:
trace = trace.drop(si.c_index[1:])
return trace
def add_matches_for_stationary_points(
matches: MatchResult,
stationary_index: List[StationaryIndex],
) -> MatchResult:
"""
takes a set of matches and adds duplicate match entries for stationary points
:param matches:
:param stationary_index:
:return:
"""
matches = deepcopy(matches)
for si in stationary_index:
mi = si.i_index[0]
m = matches[mi]
new_matches = [
m.set_coordinate(
Coordinate(ci, geom=m.coordinate.geom, crs=m.coordinate.crs)
)
for ci in si.c_index[1:]
]
matches[si.i_index[1] : si.i_index[1]] = new_matches
return matches