Trajectory#

class movingpandas.Trajectory(df, traj_id, traj_id_col=None, obj_id=None, t=None, x=None, y=None, crs='epsg:4326', parent=None)#
__init__(df, traj_id, traj_id_col=None, obj_id=None, t=None, x=None, y=None, crs='epsg:4326', parent=None)#

Create Trajectory from GeoDataFrame or DataFrame.

Parameters:
  • df (GeoDataFrame or DataFrame) – GeoDataFrame with point geometry column and timestamp index

  • traj_id (any) – Trajectory ID

  • obj_id (any) – Moving object ID

  • 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

  • parent (Trajectory) – Parent trajectory

Examples

Creating a trajectory from scratch:

>>> import pandas as pd
>>> import geopandas as gpd
>>> import movingpandas as mpd
>>>
>>> df = pd.DataFrame([
...     {'geometry':Point(0,0), 't':datetime(2018,1,1,12,0,0)},
...     {'geometry':Point(6,0), 't':datetime(2018,1,1,12,6,0)},
...     {'geometry':Point(6,6), 't':datetime(2018,1,1,12,10,0)},
...     {'geometry':Point(9,9), 't':datetime(2018,1,1,12,15,0)}
... ]).set_index('t')
>>> gdf = gpd.GeoDataFrame(df, crs=31256)
>>> traj = mpd.Trajectory(gdf, 1)

For more examples, see the tutorial notebooks.

Conversion#

Trajectory.to_line_gdf([columns])

Return the trajectory's line segments as GeoDataFrame.

Trajectory.to_linestring()

Return trajectory geometry as LineString.

Trajectory.to_linestringm_wkt()

Return the WKT string of the trajectory LineStringM representation.

Trajectory.to_mf_json([datetime_to_str, ...])

Converts a Trajectory to a dictionary compatible with the Moving Features JSON (MF-JSON) specification.

Trajectory.to_point_gdf([return_orig_tz])

Return the trajectory's points as GeoDataFrame.

Trajectory.to_traj_gdf([wkt, agg])

Return a GeoDataFrame with one row containing the trajectory as a single LineString.

Enrichment#

Trajectory.add_acceleration([overwrite, ...])

Add acceleration column and values to the trajectory's DataFrame.

Trajectory.add_angular_difference([...])

Add angular difference to the trajectory's DataFrame.

Trajectory.add_direction([overwrite, name])

Add direction column and values to the trajectory's DataFrame.

Trajectory.add_distance([overwrite, name, units])

Add distance column and values to the trajectory's DataFrame.

Trajectory.add_speed([overwrite, name, units])

Add speed column and values to the trajectory's DataFrame.

Trajectory.add_timedelta([overwrite, name])

Add timedelta column and values to the trajectory's DataFrame.

Trajectory.add_traj_id([overwrite])

Add trajectory id column and values to the trajectory's DataFrame.

General methods and attributes#

Trajectory.copy()

Return a copy of the trajectory.

Trajectory.drop(**kwargs)

Drop columns or rows from the trajectory DataFrame

Trajectory.get_max(column)

Return maximum value in the provided DataFrame column

Trajectory.get_min(column)

Return minimum value in the provided DataFrame column

Trajectory.is_valid()

Return whether the trajectory meets minimum requirements.

Trajectory.size()

Returns number of rows in Trajectory.df

Plotting#

Trajectory.explore(*args, **kwargs)

Generate a plot using GeoPandas explore (folium/leaflet.js) https://geopandas.org/en/stable/docs/reference/api/geopandas.GeoDataFrame.explore.html

Trajectory.hvplot(*args, **kwargs)

Generate an interactive plot using HoloViews.

Trajectory.hvplot_pts(*args, **kwargs)

Generate an interactive plot of trajectory points.

Trajectory.plot(*args, **kwargs)

Generate a plot using GeoPandas default plotting (Matplotlib).

hvplot examle

Spatiotemporal analysis#

Trajectory.clip(polygon[, point_based])

Return trajectory segments clipped by the given polygon.

Trajectory.distance(other[, units])

Return the minimum distance to the other geometric object (based on shapely https://shapely.readthedocs.io/en/stable/manual.html#object.distance).

Trajectory.get_bbox()

Return the trajectory's bounding box.

Trajectory.get_crs()

Return the CRS of the trajectory

Trajectory.get_direction()

Return the direction of the trajectory.

Trajectory.get_duration()

Return the trajectory's duration from start to end.

Trajectory.get_end_location()

Return the trajectory's end location.

Trajectory.get_end_time()

Return the trajectory's end time.

Trajectory.get_length([units])

Return the length of the trajectory.

Trajectory.get_linestring_between(t1, t2[, ...])

Return LineString of segment between times t1 and t2.

Trajectory.get_mcp()

Return the Minimum Convex Polygon of the trajectory data

Trajectory.get_position_at(t[, method])

Compute and return position at time t.

Trajectory.get_row_at(t[, method])

Return row of the trajectory's DataFrame at time t.

Trajectory.get_sampling_interval()

Return the sampling interval of the trajectory.

Trajectory.get_segment_between(t1, t2)

Return Trajectory segment between times t1 and t2.

Trajectory.get_start_location()

Return the trajectory's start location.

Trajectory.get_start_time()

Return the trajectory's start time.

Trajectory.hausdorff_distance(other[, units])

Return the Hausdorff distance to the other geometric object (based on shapely https://shapely.readthedocs.io/en/stable/manual.html#object.hausdorff_distance).

Trajectory.interpolate_position_at(t)

Compute and return interpolated position at time t.

Trajectory.intersection(feature[, point_based])

Return the trajectory segments that intersects the given polygon feature.

Trajectory.intersects(polygon)

Return whether the trajectory intersects the given polygon.

Trajectory.is_latlon()

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

Trajectory.to_crs(crs)

Return the trajectory reprojected to the target CRS

Spatiotemporal columns#

Trajectory.get_angular_difference_col()

Return name of the angular difference column

Trajectory.get_column_names()

Return the list of column names

Trajectory.get_direction_col()

Return name of the direction column

Trajectory.get_distance_col()

Return name of the distance column

Trajectory.get_geom_col()

Return name of the geometry column

Trajectory.get_speed_col()

Return name of the speed column

Trajectory.get_timedelta_col()

Return name of the timedelta column

Trajectory.get_traj_id_col()

Return name of the trajectory ID column