MovingPandas offers functions to compute and/or visualize the speed of movement along the trajectory between consecutive points.
import pandas as pd
import geopandas as gpd
import movingpandas as mpd
import shapely as shp
import hvplot.pandas
from geopandas import GeoDataFrame, read_file
from shapely.geometry import Point, LineString, Polygon
from datetime import datetime, timedelta
from holoviews import opts
import warnings
warnings.filterwarnings('ignore')
opts.defaults(opts.Overlay(active_tools=['wheel_zoom'], frame_width=500, frame_height=400))
mpd.show_versions()
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,0,6)},
{'geometry':Point(6,6), 't':datetime(2018,1,1,12,0,11)},
{'geometry':Point(9,9), 't':datetime(2018,1,1,12,0,14)}
]).set_index('t')
gdf = GeoDataFrame(df, crs=31256)
toy_traj = mpd.Trajectory(gdf, 1)
toy_traj
toy_traj.add_speed(overwrite=True)
toy_traj.df
We can also visualize the speed values:
toy_traj.plot(column="speed", linewidth=5, capstyle='round', legend=True)
Similar to speed, we can also add other columns:
toy_traj.add_direction(overwrite=True)
toy_traj.df
toy_traj.add_timedelta(overwrite=True)
toy_traj.df
We can also use custom units for distance, speed, and acceleration. Allowed units include metric units from mm to km, imperial units from inch to mile, nautical miles, and non-standard units which are used as CRS distance units e.g. US Survey units.
toy_traj.add_distance(overwrite=True, name="distance (km)", units="km")
toy_traj.add_distance(overwrite=True, name="distance (yards)", units="yd")
toy_traj.add_speed(overwrite=True, name="speed (ft/min)", units=("ft", "min"))
toy_traj.add_speed(overwrite=True, name="speed (knots)", units=("nm", "h"))
toy_traj.add_acceleration(overwrite=True, name="acceleration (mph/s)", units=("mi", "h", "s"))
toy_traj.df
gdf = read_file('../data/geolife_small.gpkg')
tc = mpd.TrajectoryCollection(gdf, 'trajectory_id', t='t')
my_traj = tc.trajectories[1]
my_traj.df
Even if the Trajectory
/TrajectoryCollection
GeoDataFrame does not contain a speed column, we can still plot movement speed:
my_traj.plot(column='speed', linewidth=5, capstyle='round', figsize=(9,3), legend=True, vmax=20)
my_traj.hvplot(c='speed', clim=(0,20), line_width=7.0, tiles='CartoLight', cmap='Viridis', colorbar=True)
tc.plot(column='speed', linewidth=5, capstyle='round', legend=True, vmax=20)