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, t=None, x=None, y=None, crs='epsg:4326', min_length=0, min_duration=None)
__init__(data, traj_id_col=None, obj_id_col=None, t=None, x=None, y=None, crs='epsg:4326', min_length=0, min_duration=None)

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.)

  • min_duration (timedelta) – Desired minimum duration of trajectories. (Shorter trajectories are discarded.)

Examples

>>> import geopandas as read_file
>>> import movingpandas as mpd
>>>
>>> gdf = read_file('data.gpkg')
>>> collection = mpd.TrajectoryCollection(gdf, 'trajectory_id', t='t')
add_acceleration(overwrite=False)

Add acceleration column and values to the trajectories.

Acceleration is calculated as CRS units per second squared, except if the CRS is geographic (e.g. EPSG:4326 WGS84) then acceleration is calculated in meters per second squared.

Parameters:

overwrite (bool) – Whether to overwrite existing acceleration values (default: False)

add_angular_difference(overwrite=False)

Add angular difference to the trajectories.

Angular difference is calculated as the absolute smaller angle between direction for points along the trajectory. Values are [0, 180.0]

Parameters:

overwrite (bool) – Whether to overwrite existing angular difference values (default: False)

add_direction(overwrite=False)

Add direction column and values to the trajectories.

The direction is calculated between consecutive locations. Direction values are in degrees, starting North turning clockwise.

Parameters:

overwrite (bool) – Whether to overwrite existing direction values (default: False)

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)

add_traj_id(overwrite=False)

Add trajectory id column and values to the trajectories.

Parameters:

overwrite (bool) – Whether to overwrite existing trajectory id values (default: False)

clip(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:

Resulting clipped trajectory segments

Return type:

TrajectoryCollection

copy()

Return a copy of the trajectory collection.

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 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:

Trajectories that fulfill the filter criteria

Return type:

TrajectoryCollection

Examples

>>> filtered = trajectory_collection.filter('object_type', ['TypeA', 'TypeB'])
get_end_locations()

Returns GeoDataFrame with trajectory end locations

Returns:

Trajectory end locations

Return type:

GeoDataFrame

get_geom_column_name()

Return name of the geometry column

Return type:

string

get_intersecting(polygon)

Return trajectories that intersect the given polygon.

Parameters:

polygon (shapely.geometry.Polygon) – Polygon to intersect with

Returns:

Resulting intersecting trajectories

Return type:

TrajectoryCollection

get_locations_at(t)

Returns GeoDataFrame with trajectory locations at the specified timestamp

Parameters:

t (datetime.datetime) – Timestamp to extract trajectory locations for

Returns:

Trajectory locations at timestamp t

Return type:

GeoDataFrame

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_segments_between(t1, t2)

Return Trajectory segments between times t1 and t2.

Parameters:
  • t1 (datetime.datetime) – Start time for the segments

  • t2 (datetime.datetime) – End time for the segments

Returns:

Extracted trajectory segments

Return type:

TrajectoryCollection

get_start_locations()

Returns GeoDataFrame with trajectory start locations

Returns:

Trajectory start locations

Return type:

GeoDataFrame

get_trajectory(traj_id)

Return the Trajectory with the requested ID

Parameters:

traj_id (any) – Trajectory ID

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):

>>> 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))
to_line_gdf()

Return the trajectories’ line segments as GeoDataFrame.

Return type:

GeoDataFrame

to_point_gdf()

Return the trajectories’ points as GeoDataFrame.

Return type:

GeoDataFrame

to_traj_gdf(wkt=False, agg=False)

Return a GeoDataFrame with one row per Trajectory within the TrajectoryCollection

Return type:

GeoDataFrame