/
trajectory_collection.py
393 lines (332 loc) · 11.5 KB
/
trajectory_collection.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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
# -*- coding: utf-8 -*-
import os
import sys
import pandas as pd
from copy import copy
from geopandas import GeoDataFrame
sys.path.append(os.path.dirname(__file__))
from .trajectory import Trajectory
from .trajectory_plotter import _TrajectoryCollectionPlotter
class TrajectoryCollection:
def __init__(self, data, traj_id_col=None, obj_id_col=None, t=None, x=None, y=None, crs='epsg:4326', min_length=0):
"""
Create TrajectoryCollection from list of trajectories or GeoDataFrame
Parameters
----------
data : list[Trajectory] or GeoDataFrame or DataFrame
List of Trajectory objects or a GeoDataFrame with trajectory IDs, point geometry column and timestamp index
traj_id_col : string
Name of the GeoDataFrame column containing trajectory IDs
obj_id_col : string
Name of the GeoDataFrame column containing moving object IDs
t : string
Name of the DataFrame column containing the timestamp
x : string
Name of the DataFrame column containing the x coordinate
y : string
Name of the DataFrame column containing the y coordinate
crs : string
CRS of the x/y coordinates
min_length : numeric
Desired minimum length of trajectories. (Shorter trajectories are discarded.)
Examples
--------
>>> import geopandas as read_file
>>> import movingpandas as mpd
>>>
>>> gdf = read_file('data.gpkg')
>>> gdf['t'] = pd.to_datetime(gdf['t'])
>>> gdf = gdf.set_index('t')
>>> trajectory_collection = mpd.TrajectoryCollection(gdf, 'trajectory_id')
"""
self.min_length = min_length
if type(data) == list:
self.trajectories = [traj for traj in data if traj.get_length() >= min_length]
else:
self.trajectories = self._df_to_trajectories(data, traj_id_col, obj_id_col, t, x, y, crs)
def __len__(self):
return len(self.trajectories)
def __str__(self):
return 'TrajectoryCollection with {} trajectories'.format(self.__len__())
def __repr__(self):
return self.__str__()
def __iter__(self):
"""
Iterator for trajectories in this collection
Examples
--------
>>> for traj in trajectory_collection:
>>> print(traj)
"""
for traj in self.trajectories:
if len(traj.df) >= 2:
yield traj
else:
raise ValueError(f"Trajectory with length >= 2 expected: "
f"got length {len(traj.df)}")
def copy(self):
"""
Return a copy of the trajectory collection.
Returns
-------
TrajectoryCollection
"""
trajectories = [traj.copy() for traj in self.trajectories]
# NOTE: traj_id_col and obj_id_col not needed since trajectories are already preprocessed
# on __init__().
return TrajectoryCollection(trajectories, min_length=self.min_length)
def to_point_gdf(self):
"""
Return the trajectories' points as GeoDataFrame.
Returns
-------
GeoDataFrame
"""
gdfs = [traj.df for traj in self.trajectories]
return pd.concat(gdfs)
def to_line_gdf(self):
"""
Return the trajectories' line segments as GeoDataFrame.
Returns
-------
GeoDataFrame
"""
gdfs = [traj.to_line_gdf() for traj in self.trajectories]
gdf = pd.concat(gdfs)
gdf.reset_index(drop=True, inplace=True)
return gdf
def to_traj_gdf(self, wkt=False):
"""
Return a GeoDataFrame with one row per Trajectory within the TrajectoryCollection
Returns
-------
GeoDataFrame
"""
gdfs = [traj.to_traj_gdf(wkt) for traj in self.trajectories]
gdf = pd.concat(gdfs)
gdf.reset_index(drop=True, inplace=True)
return gdf
def _df_to_trajectories(self, df, traj_id_col, obj_id_col, t, x, y, crs):
trajectories = []
for traj_id, values in df.groupby([traj_id_col]):
if len(values) < 2:
continue
if obj_id_col in values.columns:
obj_id = values.iloc[0][obj_id_col]
else:
obj_id = None
trajectory = Trajectory(values, traj_id, obj_id=obj_id, t=t, x=x, y=y, crs=crs)
if trajectory.get_length() < self.min_length or trajectory.df.geometry.count() < 2:
continue
if isinstance(df, GeoDataFrame):
trajectory.crs = df.crs
else:
trajectory.crs = crs
trajectories.append(trajectory)
return trajectories
def get_trajectory(self, traj_id):
"""
Return the Trajectory with the requested ID
Parameters
----------
traj_id : any
Trajectory ID
Returns
-------
Trajectory
"""
for traj in self:
if traj.id == traj_id:
return traj
def get_locations_at(self, t):
"""
Returns GeoDataFrame with trajectory locations at the specified timestamp
Parameters
----------
t : datetime.datetime
Returns
-------
GeoDataFrame
Trajectory locations at timestamp t
"""
gdf = GeoDataFrame()
for traj in self:
if t == 'start':
x = traj.get_row_at(traj.get_start_time())
elif t == 'end':
x = traj.get_row_at(traj.get_end_time())
else:
if t < traj.get_start_time() or t > traj.get_end_time():
continue
x = traj.get_row_at(t)
gdf = gdf.append(x)
return gdf
def get_start_locations(self):
"""
Returns GeoDataFrame with trajectory start locations
Returns
-------
GeoDataFrame
Trajectory start locations
"""
return self.get_locations_at('start')
def get_end_locations(self):
"""
Returns GeoDataFrame with trajectory end locations
Returns
-------
GeoDataFrame
Trajectory end locations
"""
return self.get_locations_at('end')
def get_intersecting(self, polygon):
"""
Return trajectories that intersect the given polygon.
Parameters
----------
polygon : shapely.geometry.Polygon
Polygon to intersect with
Returns
-------
TrajectoryCollection
Resulting intersecting trajectories
"""
intersecting = []
for traj in self:
try:
if traj.intersects(polygon):
intersecting.append(traj)
except:
pass
result = copy(self)
result.trajectories = intersecting
return result
def clip(self, polygon, point_based=False):
"""
Clip trajectories by the given polygon.
Parameters
----------
polygon : shapely.geometry.Polygon
Polygon to clip with
point_based : bool
Clipping method
Returns
-------
TrajectoryCollection
Resulting clipped trajectory segments
"""
clipped = []
for traj in self:
try:
for intersect in traj.clip(polygon, point_based):
clipped.append(intersect)
except:
pass
result = copy(self)
result.trajectories = clipped
return result
def filter(self, property_name, property_values):
"""
Filter trajectories by property
A property is a value in the df that is constant for the whole trajectory. The filter only checks if the value
on the first row equals the requested property value.
Parameters
----------
property_name : string
Name of the DataFrame column containing the property
property_values : list(any)
Desired property values
Returns
-------
TrajectoryCollection
Trajectories that fulfill the filter criteria
Examples
--------
>>> filtered = trajectory_collection.filter('object_type', ['TypeA', 'TypeB'])
"""
filtered = []
for traj in self:
if type(property_values) == list:
if traj.df.iloc[0][property_name] in property_values:
filtered.append(traj)
else:
if traj.df.iloc[0][property_name] == property_values:
filtered.append(traj)
result = copy(self)
result.trajectories = filtered
return result
def add_speed(self, overwrite=False):
"""
Add speed column and values to the trajectories.
Speed is calculated as CRS units per second, except if the CRS is geographic (e.g. EPSG:4326 WGS84)
then speed is calculated in meters per second.
Parameters
----------
overwrite : bool
Whether to overwrite existing speed values (default: False)
"""
for traj in self:
traj.add_speed(overwrite)
def get_min(self, column):
"""
Return minimum value in the provided DataFrame column over all trajectories
Parameters
----------
column : string
Name of the DataFrame column
Returns
-------
Sortable
Minimum value
"""
return min([traj.df[column].min() for traj in self])
def get_max(self, column):
"""
Return maximum value in the provided DataFrame column over all trajectories
Parameters
----------
column : string
Name of the DataFrame column
Returns
-------
Sortable
Maximum value
"""
return max([traj.df[column].max() for traj in self])
def plot(self, *args, **kwargs):
"""
Generate a plot.
Parameters
----------
args :
These parameters will be passed to the TrajectoryPlotter
kwargs :
These parameters will be passed to the TrajectoryPlotter
Examples
--------
Plot speed along trajectories (with legend and specified figure size):
>>> trajectory_collection.plot(column='speed', legend=True, figsize=(9,5))
"""
return _TrajectoryCollectionPlotter(self, *args, **kwargs).plot()
def hvplot(self, *args, **kwargs):
"""
Generate an interactive plot.
Parameters
----------
args :
These parameters will be passed to the TrajectoryPlotter
kwargs :
These parameters will be passed to the TrajectoryPlotter
Examples
--------
Plot speed along trajectories (with legend and specified figure size):
>>> trajectory_collection.hvplot(c='speed', line_width=7.0, width=700, height=400, colorbar=True)
"""
return _TrajectoryCollectionPlotter(self, *args, **kwargs).hvplot()
def _get_location_at(traj, t, columns=None):
loc = {'t': t, 'geometry': traj.get_position_at(t),
'traj_id': traj.id, 'obj_id': traj.obj_id}
if columns and columns != [None]:
for column in columns:
loc[column] = traj.df.iloc[traj.df.index.get_loc(t, method='nearest')][column]
return loc