MovingPandas.TrajectoryCollection

movingpandas: Implementation of Trajectory classes and functions built on top of GeoPandas

class movingpandas.TrajectoryCollection(data, traj_id_col=None, obj_id_col=None, min_length=0)
__init__(data, traj_id_col=None, obj_id_col=None, min_length=0)

Create TrajectoryCollection from list of trajectories or GeoDataFrame

Parameters:
  • data (list[Trajectory] or GeoDataFrame) – 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
  • 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')
add_speed(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)
clip(polygon, pointbased=False)

Clip trajectories by the given polygon.

Parameters:
  • polygon (shapely Polygon) – Polygon to clip with
  • pointbased (bool) – Clipping method
Returns:

Resulting clipped trajectory segments

Return type:

TrajectoryCollection

filter(property_name, property_values)

Filter trajectories by property

A property is a value in the df that is constant for the whole traj. 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 (any) – Desired property value
Returns:

Trajectories that fulfill the filter criteria

Return type:

TrajectoryCollection

Examples

>>> filtered = trajectory_collection.filter('object_type', 'TypeA')
get_end_locations(columns=None)

Returns GeoDataFrame with trajectory end locations

Parameters:columns (list[string]) – List of column names that should be copied from the trajectory’s dataframe to the output
Returns:Trajectory end locations
Return type:GeoDataFrame
get_intersecting(polygon)

Return trajectories that intersect the given polygon.

Parameters:polygon (shapely Polygon) – Polygon to clip with
Returns:Resulting intersecting trajectories
Return type:TrajectoryCollection
get_max(column)

Return maximum value in the provided dataframe column over all trajectories

Parameters:column (string) – Name of the dataframe column
Returns:Maximum value
Return type:Sortable
get_min(column)

Return minimum value in the provided dataframe column over all trajectories

Parameters:column (string) – Name of the dataframe column
Returns:Minimum value
Return type:Sortable
get_start_locations(columns=None)

Returns GeoDataFrame with trajectory start locations

Parameters:columns (list[string]) – List of column names that should be copied from the trajectory’s dataframe to the output
Returns:Trajectory start locations
Return type:GeoDataFrame
get_trajectory(traj_id)

Return the Trajectory with the requested ID

Parameters:traj_id (any) – Trajectory ID
Returns:
Return type:Trajectory
hvplot(*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)
plot(*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))
split_by_date(mode)

Split trajectories into subtrajectories using regular time intervals.

Resulting subtrajectories that are shorter than the TrajectoryCollection’s min_length threshold are discarded.

Parameters:mode (str) – Split mode
Returns:Resulting split subtrajectories
Return type:TrajectoryCollection
split_by_observation_gap(gap_timedelta)

Split trajectories into subtrajectories whenever there is a gap in the observations.

Resulting subtrajectories that are shorter than the TrajectoryCollection’s min_length threshold are discarded.

Parameters:gap (datetime.timedelta) – Time gap threshold
Returns:Resulting split subtrajectories
Return type:TrajectoryCollection