{ "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", "\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\n", "import folium" ] }, { "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\"])" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "gdf.explore()" ] }, { "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))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "tc.explore(column=\"trajectory_id\", cmap=\"plasma\", style_kwds={\"weight\": 4})" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "## Exploring movement speed" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "tc.plot(\n", " column=\"speed\", linewidth=5, capstyle=\"round\", legend=True, vmax=20, figsize=(9, 5)\n", ")" ] }, { "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(\n", " min_duration=timedelta(seconds=120), max_diameter=100\n", ")" ] }, { "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": [ "(\n", " tc.hvplot(line_width=7, tiles=None, frame_width=400, frame_height=400)\n", " * stop_points.hvplot(geo=True, color=\"black\", size=100)\n", ")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "m = tc.explore(\n", " column=\"trajectory_id\",\n", " cmap=\"autumn\",\n", " style_kwds={\"weight\": 4},\n", " name=\"Trajectories\",\n", ")\n", "\n", "stop_points.explore(\n", " m=m,\n", " marker_kwds={\"radius\": 4},\n", " name=\"Stop points\",\n", ")\n", "\n", "folium.TileLayer(\"CartoDB dark_matter\").add_to(m)\n", "folium.LayerControl().add_to(m)\n", "\n", "m" ] }, { "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 }