{ "cells": [ { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "# MovingPandas Minimum Viable Example\n", "\n", "\n", "\n", "[![Binder](https://mybinder.org/badge_logo.svg)](https://mybinder.org/v2/gh/movingpandas/movingpandas-examples/main?filepath=1-tutorials/99-mini-example.ipynb)\n", "\n", "MovingPandas provides a trajectory datatype based on GeoPandas.\n", "The project home is at https://github.com/movingpandas/movingpandas\n", "\n", "The documentation is available at https://movingpandas.readthedocs.io/en/main/" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import warnings\n", "warnings.filterwarnings('ignore')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import pandas as pd\n", "import geopandas as gpd\n", "import movingpandas as mpd\n", "from hvplot import pandas\n", "from datetime import datetime, timedelta" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "## Loading trajectory data from a GeoPackage\n", "\n", "The MovingPandas repository contains a demo GeoPackage file that can be loaded as follows:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "gdf = gpd.read_file('../data/geolife_small.gpkg')\n", "gdf.plot(figsize=(9,5))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "gdf.hvplot(geo=True).opts(active_tools=['pan', 'wheel_zoom'])" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "After reading the trajectory point data from file, we want to construct the trajectories." ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "## Creating a TrajectoryCollection" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "tc = mpd.TrajectoryCollection(gdf, 'trajectory_id', t='t')\n", "tc" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "tc.plot(column='trajectory_id', legend=True, figsize=(9,5))" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "## Exploring movement speed" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "tc.plot(column='speed', linewidth=5, capstyle='round', legend=True, vmax=20, figsize=(9,5))" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "## Detecting stops" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "detector = mpd.TrajectoryStopDetector(tc)\n", "stop_points = detector.get_stop_points(min_duration=timedelta(seconds=120), max_diameter=100)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "ax = tc.plot(figsize=(9,5))\n", "stop_points.plot(ax=ax, color='red', markersize=100)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "( tc.hvplot(line_width=7, tiles=None, frame_width=400, frame_height=400) * \n", " stop_points.hvplot(geo=True, color='black', size=100) )" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "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.9.15" } }, "nbformat": 4, "nbformat_minor": 4 }