Python library for trajectory and movement data analysis built on pandas and GeoPandas.
npx @tessl/cli install tessl/pypi-movingpandas@0.22.0MovingPandas is a comprehensive Python library for trajectory and movement data analysis that builds upon pandas, GeoPandas, and the broader geospatial ecosystem. It provides specialized data structures for working with spatio-temporal trajectory data, including Trajectory and TrajectoryCollection classes with rich functionality for data exploration, visualization, and analysis.
pip install movingpandasimport movingpandas as mpdCommon imports for specific functionality:
from movingpandas import Trajectory, TrajectoryCollection
from movingpandas import read_mf_json, gdf_to_mf_jsonimport movingpandas as mpd
import pandas as pd
import geopandas as gpd
from shapely.geometry import Point
from datetime import datetime, timedelta
# Create sample trajectory data
data = []
base_time = datetime(2023, 1, 1, 12, 0, 0)
for i in range(10):
data.append({
'geometry': Point(i * 0.1, i * 0.1),
't': base_time + timedelta(minutes=i * 5),
'obj_id': 'A'
})
# Create GeoDataFrame
gdf = gpd.GeoDataFrame(data, crs='EPSG:4326')
# Create a Trajectory
traj = mpd.Trajectory(gdf, traj_id='traj_1')
# Basic trajectory operations
print(f"Start time: {traj.get_start_time()}")
print(f"End time: {traj.get_end_time()}")
print(f"Duration: {traj.get_duration()}")
print(f"Length: {traj.get_length()}")
# Add computed columns
traj = traj.add_speed()
traj = traj.add_direction()
# Create TrajectoryCollection from multiple trajectories
collection = mpd.TrajectoryCollection(gdf, traj_id_col='obj_id')MovingPandas builds on the foundation of pandas and GeoPandas, extending them with specialized trajectory data structures:
The library provides extensive trajectory processing capabilities through modular components that can be combined for complex analysis workflows.
The fundamental classes for representing and managing trajectory data, providing rich functionality for spatio-temporal analysis and visualization.
class Trajectory:
def __init__(self, df, traj_id, traj_id_col=None, obj_id=None, t=None, x=None, y=None, crs="epsg:4326", parent=None): ...
def get_start_time(self): ...
def get_end_time(self): ...
def get_duration(self): ...
def get_length(self, units=UNITS()): ...
def add_speed(self, overwrite=False, name="speed", units=UNITS()): ...
def add_direction(self, overwrite=False, name="direction"): ...
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, min_duration=None): ...
def get_trajectory(self, traj_id): ...
def filter(self, property_name, property_values): ...
def get_intersecting(self, polygon): ...Advanced algorithms for trajectory generalization, splitting, cleaning, and analysis. These classes provide specialized methods for preparing and analyzing movement data.
class DouglasPeuckerGeneralizer:
def __init__(self, traj): ...
def generalize(self, tolerance=1.0): ...
class TemporalSplitter:
def __init__(self, traj): ...
def split(self, mode="day", min_length=0): ...
class OutlierCleaner:
def __init__(self, traj): ...
def clean(self, v_max=None, units=UNITS(), alpha=3): ...
class TrajectoryStopDetector:
def __init__(self, traj, n_processes=1): ...
def get_stop_segments(self, max_diameter, min_duration): ...Tools for extracting insights from trajectory collections, including significant point detection, clustering, and flow analysis.
class TrajectoryCollectionAggregator:
def __init__(self, traj_collection, max_distance, min_distance, min_stop_duration, min_angle=45): ...
def get_significant_points_gdf(self): ...
def get_clusters_gdf(self): ...
def get_flows_gdf(self): ...
class PointClusterer:
def __init__(self, points, max_distance, is_latlon): ...
def get_clusters(self): ...Functions for importing and exporting trajectory data in various formats, with special support for OGC Moving Features JSON.
def read_mf_json(json_file_path, traj_id_property=None, traj_id=0): ...
def read_mf_dict(data, traj_id=0, traj_id_property=None): ...
def gdf_to_mf_json(gdf, traj_id_column, datetime_column, temporal_columns=None, **kwargs): ...
def show_versions(): ...from collections import namedtuple
UNITS = namedtuple("UNITS", "distance time time2 crs", defaults=(None, None, None, None))
class TimeZoneWarning(UserWarning, ValueError):
"""Warning for timezone-related issues in trajectory data."""# Standard column names used throughout MovingPandas
ACCELERATION_COL_NAME = "acceleration"
ANGULAR_DIFFERENCE_COL_NAME = "angular_difference"
DIRECTION_COL_NAME = "direction"
DISTANCE_COL_NAME = "distance"
SPEED_COL_NAME = "speed"
TIMEDELTA_COL_NAME = "timedelta"
TRAJ_ID_COL_NAME = "traj_id"