{ "cells": [ { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "# Measuring distances \n", "\n", "\n", "\n", "[![Binder](https://mybinder.org/badge_logo.svg)](https://mybinder.org/v2/gh/movingpandas/movingpandas-examples/main?filepath=1-tutorials/11-measuring-distances.ipynb)\n", "[![IPYNB](https://img.shields.io/badge/view-ipynb-hotpink)](https://github.com/movingpandas/movingpandas-examples/blob/main/1-tutorials/11-measuring-distances.ipynb)\n", "[![HTML](https://img.shields.io/badge/view-html-green)](https://movingpandas.github.io/movingpandas-website/1-tutorials/11-measuring-distances.html)\n", "\n", "Distances can be computed between trajectories as well as between trajectories and other geometry objects. \n", "The implemented distance measures are:\n", "\n", "* [Shortest distance](https://movingpandas.readthedocs.io/en/main/trajectory.html#movingpandas.Trajectory.distance)\n", "* [Hausdorff distance](https://movingpandas.readthedocs.io/en/main/trajectory.html#movingpandas.Trajectory.hausdorff_distance)\n" ] }, { "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", "import matplotlib.pyplot as plt\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, dim\n", "\n", "import warnings\n", "warnings.filterwarnings('ignore')\n", "\n", "plot_defaults = {'linewidth':5, 'capstyle':'round', 'figsize':(9,3), 'legend':True}\n", "opts.defaults(opts.Overlay(active_tools=['wheel_zoom']))\n", "hvplot_defaults = {'tiles':'CartoLight', 'frame_height':320, 'frame_width':320, 'cmap':'Viridis', 'colorbar':True}\n", "\n", "mpd.show_versions()" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "## Measuring distances between trajectories" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "df = pd.DataFrame([\n", " {'geometry':Point(0,0), 't':datetime(2018,1,1,12,0,0)},\n", " {'geometry':Point(6,0), 't':datetime(2018,1,1,12,6,0)},\n", " {'geometry':Point(6,6), 't':datetime(2018,1,1,12,10,0)},\n", " {'geometry':Point(9,9), 't':datetime(2018,1,1,12,15,0)}\n", "]).set_index('t')\n", "geo_df = GeoDataFrame(df, crs=31256)\n", "toy_traj = mpd.Trajectory(geo_df, 1)\n", "toy_traj.df" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "df = pd.DataFrame([\n", " {'geometry':Point(3,3), 't':datetime(2018,1,1,12,0,0)},\n", " {'geometry':Point(3,9), 't':datetime(2018,1,1,12,6,0)},\n", " {'geometry':Point(2,9), 't':datetime(2018,1,1,12,10,0)},\n", " {'geometry':Point(0,7), 't':datetime(2018,1,1,12,15,0)}\n", "]).set_index('t')\n", "geo_df = GeoDataFrame(df, crs=31256)\n", "toy_traj2 = mpd.Trajectory(geo_df, 1)\n", "toy_traj2.df\n", "\n", "ax = toy_traj.plot()\n", "toy_traj2.plot(ax=ax, color='red')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(f'Distance: {toy_traj.distance(toy_traj2)} meters')\n", "print(f'Hausdorff distance: {toy_traj.hausdorff_distance(toy_traj2):.2f} meters')" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [] }, "outputs": [], "source": [ "print(f'Distance: {toy_traj.distance(toy_traj2, units=\"cm\")} cm')\n", "print(f'Hausdorff distance: {toy_traj.hausdorff_distance(toy_traj2, units=\"km\"):.6f} km')" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "## Measuring distances between trajectories and other geometry objects" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "pt = Point(1, 5)\n", "line = LineString([(3,3), (3,9)])\n", "\n", "ax = toy_traj.plot()\n", "gpd.GeoSeries(pt).plot(ax=ax, color='red')\n", "gpd.GeoSeries(line).plot(ax=ax, color='red')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(f'Distance: {toy_traj.distance(pt)}')\n", "print(f'Hausdorff distance: {toy_traj.hausdorff_distance(pt):.2f}')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(f'Distance: {toy_traj.distance(line)}')\n", "print(f'Hausdorff distance: {toy_traj.hausdorff_distance(line)}')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(f'Distance: {toy_traj.distance(line, units=\"cm\")} cm')\n", "print(f'Hausdorff distance: {toy_traj.hausdorff_distance(line, units=\"km\"):.6f} km')" ] }, { "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.10.10" } }, "nbformat": 4, "nbformat_minor": 4 }