
MovingPandas: Data Structures and Methods for Movement Data Analysis
Apr 29, 2020 • Software
MovingPandas is an easy to use toolkit for exploring movement data that has recently passed the PyOpenSci review.
Movement data is everywhere: from tracking apps that record human or vehicle movement to ecologists monitoring wildlife behavior. Movement data analysis is challenging since movement data combines time series and spatial data analyses questions. Existing spatial analysis libraries, such as GeoPandas, are great at handling spatial data but they don’t support moving objects.
MovingPandas aims to fill the gap of missing tools for exploring movement data. It provides data structures and methods for dealing with data of moving objects. MovingPandas has been accepted by pyOpenSci as part of its ecosystem in March 2020.
A Toolkit for Movement Data Analysis
The MovingPandas repository contains multiple tutorials in the form of Jupyter notebooks that illustrate diverse analysis capabilities using different datasets, including: tracking data of ships, migration of birds, and tracks from a horse’s GPS collar.
MovingPandas Trajectory objects are created from GeoPandas GeoDataFrames. A minimal example would be:
import pandas as pd
import geopandas as gpd
import movingpandas as mpd
from shapely.geometry import Point
from datetime import datetime
from pyproj import CRS
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=CRS(31256))
traj = mpd.Trajectory(gdf, 1)
MovingPandas provides static plots using Matplotlib and interactive plots using hvplot:
traj.plot()

Matplotlib and hvplot parameters are passed along to the underlying libraries to enable extensive customization of plots:
traj.hvplot(geo=True, tiles='OSM', line_width=5, frame_width=300, frame_height=300)

Exploring Movement Characteristics
MovingPandas makes it straightforward to compute movement characteristics, such as trajectory length and duration, as well as movement speed and direction.
For example, we can explore the daily travelled distance as recorded by a GPS tracker:
df = read_file('tracker.gpkg')
df = df.set_index('t')
tc = mpd.TrajectoryCollection(df, 'CollarID')
daily = tc.split_by_date(mode='day')
daily_lengths = [traj.get_length() for traj in daily.trajectories]
daily_t = [traj.get_start_time() for traj in daily.trajectories]
daily_lengths = pd.DataFrame(daily_lengths, index=daily_t, columns=['length'])
daily_lengths.hvplot(title='Daily trajectory length')

In this case, the movement data, which comes from a GPS collar of a horse, reveals that the animal tends to travel farther during summer days than during shorter winter days.
Other functions deal with trajectory generalization, splitting trajectories into subtrajectories, clipping trajectories to an area of interest, and extracting trajectory start and end times and locations.
Standing on the Shoulders of Giants
By leveraging existing functionality within the Python data analysis ecosystem, such as, for example: time series handling by Pandas, spatial data analysis by GeoPandas, and interactive plotting by HoloViews, MovingPandas can focus on its core functionality dealing with challenges that are specific to movement data.
For example, the close integration with HoloViews makes it possible to create interactive dashboards to explore the effect of different trajectory generalization methods:

Test Your Movement Data Anylsis Skills Today!
You can try MovingPandas in a MyBinder notebook - no installation required
What’s next?
MovingPandas is under active development and there are some exciting features coming up. If you’d like to contribute to this project, you’re welcome to head on over to the Github repo!
Keep reading
Maintain Python code quality in Quarto Markdown files using lintquarto
lintquarto is a package for running linters, formatters, static type checkers and code analysis tools on Python code in Quarto markdown (.qmd) files.
Read moreCyNetDiff: A Python Library for Accelerated Implementation of Network Diffusion Models
CyNetDiff is a Python package for accelerating network diffusion simulations, recently accepted into the pyOpenSci ecosystem.
Read moreNavigating LLMs in Open Source: pyOpenSci's New Peer Review Policy
Generative AI products are reducing the effort and skill necessary to generate large amounts of code. In some cases, this strains volunteer peer review programs like ours. Learn about pyOpenSci’s …
Read more