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. Length is calculated using CRS units, except if the CRS is geographic (e.g. EPSG:4326 WGS84) then length is calculated in meters. (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, name='acceleration', units=(None, None, None, None))

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)

  • name (str) – Name of the acceleration column (default: “acceleration”)

  • units (tuple(str)) –

    Units in which to calculate acceleration

    distancestr

    Abbreviation for the distance unit (default: CRS units, or metres if geographic)

    timestr

    Abbreviation for the time unit (default: seconds)

    time2str

    Abbreviation for the second time unit (default: seconds)

    For more info, check the list of supported units at https://movingpandas.org/units

add_angular_difference(overwrite=False, name='angular_difference')

Add angular difference to the trajectory’s DataFrame.

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, name='direction')

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_distance(overwrite=False, name='distance', units=None)

Add distance column and values to the trajectories.

Parameters:
  • overwrite (bool) – Whether to overwrite existing distance values (default: False)

  • name (str) – Name of the distance column (default: “distance”)

  • units (str) – Units in which to calculate distance values (default: CRS units) For more info, check the list of supported units at https://movingpandas.org/units

add_speed(overwrite=False, name='speed', units=(None, None, None, None))

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)

  • units (tuple(str)) –

    Units in which to calculate speed

    distancestr

    Abbreviation for the distance unit (default: CRS units, or metres if geographic)

    timestr

    Abbreviation for the time unit (default: seconds)

    For more info, check the list of supported units at https://movingpandas.org/units

add_timedelta(overwrite=False, name='timedelta')

Add timedelta column and values to the trajectories.

Timedelta is calculated as the time difference between the current and the previous row. Values are instances of datetime.timedelta.

Parameters:
  • overwrite (bool) – Whether to overwrite existing timedelta values (default: False)

  • name (str) – Name of the timedelta column (default: “timedelta”)

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

drop(**kwargs)

Drop columns or rows from the trajectories’ DataFrames

Examples

>>> tc.drop(columns=['abc','def'])
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_column_names()

Return the list of column names

Return type:

list

get_crs()

Return the CRS of the trajectories

get_direction_col()

Return name of the direction column

Return type:

string

get_end_locations(with_direction=False)

Returns GeoDataFrame with trajectory end locations

Returns:

Trajectory end locations

Return type:

GeoDataFrame

get_geom_col()

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, with_direction=False)

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

Return name of the speed column

Return type:

string

get_start_locations(with_direction=False)

Returns GeoDataFrame with trajectory start locations

Returns:

Trajectory start locations

Return type:

GeoDataFrame

get_traj_id_col()

Return name of the trajectory ID column

Return type:

string

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:

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)
intersection(feature, point_based=False)

Intersect trajectories with the given polygon feature.

Feature attributes are appended to the trajectory’s DataFrame.

By default, the trajectory’s line representation is clipped by the polygon. If pointbased=True, the trajectory’s point representation is used instead, leading to shorter segments.

Parameters:
  • feature (shapely Feature) – Feature to intersect with

  • point_based (bool) – Clipping method

Returns:

Intersecting trajectory segments

Return type:

TrajectoryCollection

is_latlon()

Return True if the trajectory CRS is geographic (e.g. EPSG:4326 WGS84)

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(columns=None)

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