{ "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", "\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", "warnings.filterwarnings('ignore')\n", "\n", "opts.defaults(opts.Overlay(active_tools=['wheel_zoom'], frame_width=500, frame_height=400))\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([(xmin,ymin), (xmin,ymax), (xmax,ymax), (xmax,ymin), (xmin,ymin)])\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)" ] }, { "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()" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "## Computing intersections" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "polygon_feature = {\n", " \"geometry\": polygon,\n", " \"properties\": {'field1': 'abc'}\n", "}" ] }, { "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.to_point_gdf()" ] }, { "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 }