#!/usr/bin/env python # coding: utf-8 # # Computing movement speed # # # # [![Binder](https://mybinder.org/badge_logo.svg)](https://mybinder.org/v2/gh/movingpandas/movingpandas-examples/main?filepath=1-tutorials/2-computing-speed.ipynb) # [![IPYNB](https://img.shields.io/badge/view-ipynb-hotpink)](https://github.com/movingpandas/movingpandas-examples/blob/main/1-tutorials/2-computing-speed.ipynb) # [![HTML](https://img.shields.io/badge/view-html-green)](https://movingpandas.github.io/movingpandas-website/1-tutorials/2-computing-speed.html) # # MovingPandas offers functions to compute and/or visualize the speed of movement along the trajectory between consecutive points. # In[ ]: 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() # ## Basic example # In[ ]: 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 # In[ ]: toy_traj.add_speed(overwrite=True) toy_traj.df # We can also visualize the speed values: # In[ ]: toy_traj.plot(column="speed", linewidth=5, capstyle='round', legend=True) # Similar to speed, we can also add other columns: # In[ ]: toy_traj.add_direction(overwrite=True) toy_traj.df # In[ ]: 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. # In[ ]: 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 # ## Real-world trajectories # In[ ]: gdf = read_file('../data/geolife_small.gpkg') tc = mpd.TrajectoryCollection(gdf, 'trajectory_id', t='t') # In[ ]: my_traj = tc.trajectories[1] # In[ ]: my_traj.df # Even if the ``Trajectory``/``TrajectoryCollection`` GeoDataFrame does not contain a speed column, we can still plot movement speed: # In[ ]: my_traj.plot(column='speed', linewidth=5, capstyle='round', figsize=(9,3), legend=True, vmax=20) # In[ ]: my_traj.hvplot(c='speed', clim=(0,20), line_width=7.0, tiles='CartoLight', cmap='Viridis', colorbar=True) # In[ ]: tc.plot(column='speed', linewidth=5, capstyle='round', legend=True, vmax=20) # In[ ]: