{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"# MovingPandas @ SDSC21\n",
"\n",
"\n",
"\n",
"[Anita Graser](https://anitagraser.com/about/)\n",
"\n",
"* Easy trajectory data handling\n",
"* Straightforward data exploration\n",
"\n",
"\n",
"Based on GeoPandas and Holoviews.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"outputs": [],
"source": [
"import warnings\n",
"warnings.filterwarnings('ignore')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"outputs": [],
"source": [
"from datetime import datetime, timedelta\n",
"import sys\n",
"sys.path.append(\"..\")"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"## Loading trajectory data from a GeoPackage"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [],
"source": [
"from geopandas import gpd\n",
"gdf = gpd.read_file('data/demodata_geolife.gpkg')\n",
"gdf"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [],
"source": [
"gdf.plot(figsize=(9,5))"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"source": [
"After reading the trajectory point data from file, we want to construct the trajectories."
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"## Creating a TrajectoryCollection"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [],
"source": [
"import movingpandas as mpd\n",
"traj_collection = mpd.TrajectoryCollection(gdf, 'trajectory_id', t='t')\n",
"print(traj_collection)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [],
"source": [
"traj_collection.plot(column='trajectory_id', linewidth=5, legend=True, figsize=(9,5))"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"## Exploring movement speed"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [],
"source": [
"traj_collection.plot(column='speed', linewidth=5, capstyle='round', legend=True, vmax=20, figsize=(9,5))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"outputs": [],
"source": [
"my_traj = traj_collection.get_trajectory(2)\n",
"print(my_traj)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"outputs": [],
"source": [
"my_traj.hvplot(c='speed', line_width=7.0, tiles='StamenTonerBackground', cmap='Viridis', colorbar=True, clim=(0,20), width=700, height=400)"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"## Detecting stops"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [],
"source": [
"detector = mpd.TrajectoryStopDetector(traj_collection)\n",
"stop_points = detector.get_stop_points(min_duration=timedelta(seconds=120), max_diameter=100)\n",
"stop_points.head()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [],
"source": [
"ax = traj_collection.plot(figsize=(9,5), linewidth=5)\n",
"stop_points.plot(ax=ax, color='deeppink', markersize=100)"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"## Splitting at stops"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [],
"source": [
"my_traj = traj_collection.get_trajectory(1)\n",
"split = mpd.StopSplitter(my_traj).split(min_duration=timedelta(seconds=60), max_diameter=100)\n",
"split"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [],
"source": [
"ax = split.trajectories[0].plot(figsize=(9,6), linewidth=5)\n",
"for i, traj in enumerate(split):\n",
" traj.plot(ax=ax, color=['deeppink', 'slategray'][i%2], linewidth=5)"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"## Something more interactive\n",
"\n",
"[Holoviews demo](http://localhost:8888/notebooks/Documents/GitHub/anitagraser/movingpandas/tutorials/4-stop-detection.ipynb)"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"\n",
"\n",
"# More ...\n",
"\n",
"Examples: https://github.com/anitagraser/movingpandas-examples\n",
"\n",
"Repo: https://github.com/anitagraser/movingpandas\n",
"\n",
"Documentation: https://movingpandas.readthedocs.io/en/master/\n"
]
}
],
"metadata": {
"celltoolbar": "Slideshow",
"kernelspec": {
"display_name": "Python 3",
"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.7.10"
}
},
"nbformat": 4,
"nbformat_minor": 4
}