{ "cells": [ { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "# Computing intersections with polygons\n", "\n", "\n", "\n", "[![Binder](https://mybinder.org/badge_logo.svg)](https://mybinder.org/v2/gh/movingpandas/movingpandas-examples/main?filepath=1-tutorials/5-intersecting-with-polygons.ipynb)\n", "[![IPYNB](https://img.shields.io/badge/view-ipynb-hotpink)](https://github.com/movingpandas/movingpandas-examples/blob/main/1-tutorials/5-intersecting-with-polygons.ipynb)\n", "[![HTML](https://img.shields.io/badge/view-html-green)](https://movingpandas.github.io/movingpandas-website/1-tutorials/5-intersecting-with-polygons.html)\n", "\n", "Clipping and intersection functions can be used to extract trajectory segments that are located within an area of interest polygon." ] }, { "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 folium\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": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "gdf = read_file(\"../data/geolife_small.gpkg\")\n", "tc = mpd.TrajectoryCollection(gdf, \"trajectory_id\", t=\"t\")" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "## Clipping a Trajectory" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "xmin, xmax, ymin, ymax = 116.365035, 116.3702945, 39.904675, 39.907728\n", "polygon = Polygon(\n", " [(xmin, ymin), (xmin, ymax), (xmax, ymax), (xmax, ymin), (xmin, ymin)]\n", ")\n", "\n", "my_traj = tc.trajectories[2]\n", "intersections = my_traj.clip(polygon)\n", "\n", "print(\"Found {} intersections\".format(len(intersections)))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "ax = my_traj.plot()\n", "gpd.GeoSeries(polygon).plot(ax=ax, color=\"lightgray\")\n", "intersections.plot(ax=ax, color=\"red\", linewidth=5)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "m = my_traj.explore(color=\"blue\", style_kwds={\"weight\": 4}, name=\"Trajectory\")\n", "\n", "intersections.explore(m=m, color=\"red\", style_kwds={\"weight\": 4}, name=\"Intersection\")\n", "\n", "folium.TileLayer(\"OpenStreetMap\").add_to(m)\n", "folium.LayerControl().add_to(m)\n", "\n", "m" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "## Clipping a TrajectoryCollection" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "Alternatively, using **TrajectoryCollection**:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "clipped = tc.clip(polygon)\n", "clipped" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "clipped.plot()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "clipped.explore(\n", " column=\"trajectory_id\",\n", " cmap=\"cool\",\n", " tiles=\"CartoDB positron\",\n", " style_kwds={\"weight\": 4},\n", ")" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "## Computing intersections for a Trajectory" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "polygon_feature = {\"geometry\": polygon, \"properties\": {\"field1\": \"abc\"}}" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "my_traj = tc.trajectories[2]\n", "intersections = my_traj.intersection(polygon_feature)\n", "intersections" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "intersections.plot()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "intersections.explore(color=\"blue\", style_kwds={\"weight\": 4})" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "intersections.to_point_gdf()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Computing intersections for a TrajectoryCollection" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "intersections = tc.intersection(polygon_feature)\n", "intersections" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "intersections.plot()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "intersections.explore(\n", " column=\"trajectory_id\",\n", " cmap=\"autumn\",\n", " tiles=\"CartoDB positron\",\n", " style_kwds={\"weight\": 4},\n", ")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "intersections.to_point_gdf()" ] }, { "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 }