{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Computing movement speed\n", "\n", "\n", "\n", "[![Binder](https://mybinder.org/badge_logo.svg)](https://mybinder.org/v2/gh/movingpandas/movingpandas-examples/main?filepath=1-tutorials/2-computing-speed.ipynb)\n", "[![IPYNB](https://img.shields.io/badge/view-ipynb-hotpink)](https://github.com/movingpandas/movingpandas-examples/blob/main/1-tutorials/2-computing-speed.ipynb)\n", "[![HTML](https://img.shields.io/badge/view-html-green)](https://movingpandas.github.io/movingpandas-website/1-tutorials/2-computing-speed.html)\n", "\n", "MovingPandas offers functions to compute and/or visualize the speed of movement along the trajectory between consecutive points." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import pandas as pd\n", "import geopandas as gpd\n", "import movingpandas as mpd\n", "import shapely as shp\n", "import hvplot.pandas\n", "\n", "from geopandas import GeoDataFrame, read_file\n", "from shapely.geometry import Point, LineString, Polygon\n", "from datetime import datetime, timedelta\n", "from holoviews import opts\n", "\n", "import warnings\n", "\n", "warnings.filterwarnings(\"ignore\")\n", "\n", "opts.defaults(\n", " opts.Overlay(active_tools=[\"wheel_zoom\"], frame_width=500, frame_height=400)\n", ")\n", "\n", "mpd.show_versions()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Basic example" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "df = pd.DataFrame(\n", " [\n", " {\"geometry\": Point(0, 0), \"t\": datetime(2018, 1, 1, 12, 0, 0)},\n", " {\"geometry\": Point(6, 0), \"t\": datetime(2018, 1, 1, 12, 0, 6)},\n", " {\"geometry\": Point(6, 6), \"t\": datetime(2018, 1, 1, 12, 0, 11)},\n", " {\"geometry\": Point(9, 9), \"t\": datetime(2018, 1, 1, 12, 0, 14)},\n", " ]\n", ").set_index(\"t\")\n", "gdf = GeoDataFrame(df, crs=31256)\n", "toy_traj = mpd.Trajectory(gdf, 1)\n", "toy_traj" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "toy_traj.add_speed(overwrite=True)\n", "toy_traj.df" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can also visualize the speed values:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "toy_traj.plot(column=\"speed\", linewidth=5, capstyle=\"round\", legend=True)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Similar to speed, we can also add other columns:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "toy_traj.add_direction(overwrite=True)\n", "toy_traj.df" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "toy_traj.add_timedelta(overwrite=True)\n", "toy_traj.df" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "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." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "toy_traj.add_distance(overwrite=True, name=\"distance (km)\", units=\"km\")\n", "toy_traj.add_distance(overwrite=True, name=\"distance (yards)\", units=\"yd\")\n", "toy_traj.add_speed(overwrite=True, name=\"speed (ft/min)\", units=(\"ft\", \"min\"))\n", "toy_traj.add_speed(overwrite=True, name=\"speed (knots)\", units=(\"nm\", \"h\"))\n", "toy_traj.add_acceleration(\n", " overwrite=True, name=\"acceleration (mph/s)\", units=(\"mi\", \"h\", \"s\")\n", ")\n", "toy_traj.df" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Real-world trajectories" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "gdf = read_file(\"../data/geolife_small.gpkg\")\n", "tc = mpd.TrajectoryCollection(gdf, \"trajectory_id\", t=\"t\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "my_traj = tc.trajectories[1]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "my_traj.df" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "Even if the ``Trajectory``/``TrajectoryCollection`` GeoDataFrame does not contain a speed column, we can still plot movement speed:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "my_traj.plot(\n", " column=\"speed\", linewidth=5, capstyle=\"round\", figsize=(9, 3), legend=True, vmax=20\n", ")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "my_traj.hvplot(\n", " c=\"speed\",\n", " clim=(0, 20),\n", " line_width=7.0,\n", " tiles=\"CartoLight\",\n", " cmap=\"Viridis\",\n", " colorbar=True,\n", ")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "tc.plot(column=\"speed\", linewidth=5, capstyle=\"round\", legend=True, vmax=20)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "mpd-ex", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.10.15" } }, "nbformat": 4, "nbformat_minor": 4 }