/
trajectory_generalizer.py
289 lines (226 loc) · 8.58 KB
/
trajectory_generalizer.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
281
282
283
284
285
286
287
288
289
# -*- coding: utf-8 -*-
from copy import copy
from shapely.geometry import LineString, Point
import pandas as pd
from .trajectory import Trajectory
from .trajectory_collection import TrajectoryCollection
from .geometry_utils import measure_distance_geodesic, measure_distance_euclidean
class TrajectoryGeneralizer:
"""
Generalizer base class
"""
def __init__(self, traj):
"""
Create TrajectoryGeneralizer
Parameters
----------
traj : Trajectory or TrajectoryCollection
"""
self.traj = traj
self.traj_col_name = traj.get_geom_column_name()
def generalize(self, tolerance):
"""
Generalize the input Trajectory/TrajectoryCollection.
Parameters
----------
tolerance : any type
Tolerance threshold, differs by generalizer
Returns
-------
Trajectory/TrajectoryCollection
Generalized Trajectory or TrajectoryCollection
"""
if isinstance(self.traj, Trajectory):
return self._generalize_traj(self.traj, tolerance)
elif isinstance(self.traj, TrajectoryCollection):
return self._generalize_traj_collection(tolerance)
else:
raise TypeError
def _generalize_traj_collection(self, tolerance):
generalized = []
for traj in self.traj:
generalized.append(self._generalize_traj(traj, tolerance))
result = copy(self.traj)
result.trajectories = generalized
return result
def _generalize_traj(self, traj, tolerance):
return traj
class MinDistanceGeneralizer(TrajectoryGeneralizer):
"""
Generalizes based on distance.
This generalization ensures that consecutive locations are at least a
certain distance apart.
Distance is calculated using CRS units, except if the CRS is geographic
(e.g. EPSG:4326 WGS84) then distance is calculated in metres.
tolerance : float
Desired minimum distance between consecutive points
Examples
--------
>>> mpd.MinDistanceGeneralizer(traj).generalize(tolerance=1.0)
"""
def _generalize_traj(self, traj, tolerance):
temp_df = traj.df.copy()
prev_pt = temp_df.iloc[0][traj.get_geom_column_name()]
keep_rows = [0]
i = 0
for index, row in temp_df.iterrows():
pt = row[traj.get_geom_column_name()]
if traj.is_latlon:
dist = measure_distance_geodesic(pt, prev_pt)
else:
dist = measure_distance_euclidean(pt, prev_pt)
if dist >= tolerance:
keep_rows.append(i)
prev_pt = pt
i += 1
keep_rows.append(len(traj.df) - 1)
new_df = traj.df.iloc[keep_rows]
new_traj = Trajectory(new_df, traj.id)
return new_traj
class MinTimeDeltaGeneralizer(TrajectoryGeneralizer):
"""
Generalizes based on time.
This generalization ensures that consecutive rows are at least a certain
timedelta apart.
tolerance : datetime.timedelta
Desired minimum time difference between consecutive rows
Examples
--------
>>> mpd.MinTimeDeltaGeneralizer(traj).generalize(tolerance=timedelta(minutes=10))
"""
def _generalize_traj(self, traj, tolerance):
temp_df = traj.df.copy()
temp_df["t"] = temp_df.index
prev_t = temp_df.head(1)["t"][0]
keep_rows = [0]
i = 0
for index, row in temp_df.iterrows():
t = row["t"]
tdiff = t - prev_t
if tdiff >= tolerance:
keep_rows.append(i)
prev_t = t
i += 1
keep_rows.append(len(traj.df) - 1)
new_df = traj.df.iloc[keep_rows]
new_traj = Trajectory(new_df, traj.id)
return new_traj
class MaxDistanceGeneralizer(TrajectoryGeneralizer):
"""
Generalizes based on distance.
Similar to Douglas-Peuker. Single-pass implementation that checks whether
the provided distance threshold is exceed.
tolerance : float
Distance tolerance in trajectory CRS units
Examples
--------
>>> mpd.MaxDistanceGeneralizer(traj).generalize(tolerance=1.0)
"""
def _generalize_traj(self, traj, tolerance):
prev_pt = None
pts = []
keep_rows = []
i = 0
for index, row in traj.df.iterrows():
current_pt = row[traj.get_geom_column_name()]
if prev_pt is None:
prev_pt = current_pt
keep_rows.append(i)
continue
line = LineString([prev_pt, current_pt])
for pt in pts:
if line.distance(pt) > tolerance:
prev_pt = current_pt
pts = []
keep_rows.append(i)
continue
pts.append(current_pt)
i += 1
keep_rows.append(i)
new_df = traj.df.iloc[keep_rows]
new_traj = Trajectory(new_df, traj.id)
return new_traj
class DouglasPeuckerGeneralizer(TrajectoryGeneralizer):
"""
Generalizes using Douglas-Peucker algorithm (as implemented in shapely/Geos).
tolerance : float
Distance tolerance in trajectory CRS units
References
----------
* Douglas, D., & Peucker, T. (1973). Algorithms for the reduction of the number
of points required to represent a digitized line or its caricature.
The Canadian Cartographer 10(2), 112–122. doi:10.3138/FM57-6770-U75U-7727.
Examples
--------
>>> mpd.DouglasPeuckerGeneralizer(traj).generalize(tolerance=1.0)
"""
def _generalize_traj(self, traj, tolerance):
keep_rows = []
i = 0
simplified = (
traj.to_linestring().simplify(tolerance, preserve_topology=False).coords
)
for index, row in traj.df.iterrows():
current_pt = row[traj.get_geom_column_name()]
if current_pt.coords[0] in simplified:
keep_rows.append(i)
i += 1
new_df = traj.df.iloc[keep_rows]
new_traj = Trajectory(new_df, traj.id)
return new_traj
class TopDownTimeRatioGeneralizer(TrajectoryGeneralizer):
"""
Generalizes using Top-Down Time Ratio algorithm proposed by Meratnia & de By (2004).
This is a spatiotemporal trajectory generalization algorithm. Where Douglas-Peucker
simply measures the spatial distance between points and original line geometry,
Top-Down Time Ratio (TDTR) measures the distance between points and their
spatiotemporal projection on the trajectory. These projections are calculated based
on the ratio of travel times between the segment start and end times and the point
time.
tolerance : float
Distance tolerance (distance returned by shapely Point.distance function)
References
----------
* Meratnia, N., & de By, R.A. (2004). Spatiotemporal compression techniques for
moving point objects. In International Conference on Extending Database Technology
(pp. 765-782). Springer, Berlin, Heidelberg.
Examples
--------
>>> mpd.TopDownTimeRatioGeneralizer(traj).generalize(tolerance=1.0)
"""
def _generalize_traj(self, traj, tolerance):
generalized = self.td_tr(traj.df.copy(), tolerance)
return Trajectory(generalized, traj.id)
def td_tr(self, df, tolerance):
if len(df) <= 2:
return df
else:
de = (
df.index.max().to_pydatetime() - df.index.min().to_pydatetime()
).total_seconds()
t0 = df.index.min().to_pydatetime()
pt0 = df[self.traj_col_name].iloc[0]
ptn = df[self.traj_col_name].iloc[-1]
dx = ptn.x - pt0.x
dy = ptn.y - pt0.y
dists = df.apply(
lambda rec: self._dist_from_calced(rec, t0, pt0, de, dx, dy),
axis=1,
)
if dists.max() > tolerance:
return pd.concat(
[
self.td_tr(
df.iloc[: df.index.get_loc(dists.idxmax()) + 1], tolerance
),
self.td_tr(
df.iloc[df.index.get_loc(dists.idxmax()) :], tolerance
),
]
)
else:
return df.iloc[[0, -1]]
def _dist_from_calced(self, rec, start_t, start_geom, de, dx, dy):
di = (rec.name - start_t).total_seconds()
calced = Point(start_geom.x + dx * di / de, start_geom.y + dy * di / de)
return rec[self.traj_col_name].distance(calced)