/
trajectory_generalizer.py
202 lines (157 loc) · 5.46 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
# -*- coding: utf-8 -*-
from copy import copy
from shapely.geometry import LineString
from .trajectory import Trajectory
from .trajectory_collection import TrajectoryCollection
from .geometry_utils import measure_distance_spherical, measure_distance_euclidean
class TrajectoryGeneralizer:
"""
Generalizer base class
"""
def __init__(self, traj):
"""
Create TrajectoryGeneralizer
Parameters
----------
traj : Trajectory/TrajectoryCollection
"""
self.traj = traj
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.
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_spherical(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
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.
tolerance : float
Distance tolerance
Examples
--------
>>> mpd.DouglasPeuckerGeneralizer(traj).generalize(tolerance=1.0)
"""
def _generalize_traj(self, traj, tolerance):
keep_rows = []
i = 0
simplified = traj.to_linestring().simplify(tolerance).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