{ "cells": [ { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "# Exporting Trajectories\n", "\n", "\n", "\n", "[![Binder](https://mybinder.org/badge_logo.svg)](https://mybinder.org/v2/gh/movingpandas/movingpandas-examples/main?filepath=1-tutorials/4-exporting-trajectories.ipynb)\n", "[![IPYNB](https://img.shields.io/badge/view-ipynb-hotpink)](https://github.com/movingpandas/movingpandas-examples/blob/main/1-tutorials/4-exporting-trajectories.ipynb)\n", "[![HTML](https://img.shields.io/badge/view-html-green)](https://movingpandas.github.io/movingpandas-website/1-tutorials/4-exporting-trajectories.html)\n", "\n", "Trajectories and TrajectoryCollections can be converted back to GeoDataFrames that can then be exported to different GIS file formats." ] }, { "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')\n", "tc" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "## Converting TrajectoryCollections back to GeoDataFrames\n", "\n", "### Convert to a point GeoDataFrame" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "tc.to_point_gdf()" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "### Convert to a line GeoDataFrame" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "tc.to_line_gdf()" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "### Convert to a trajectory GeoDataFrame" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "tc.to_traj_gdf(wkt=True)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "tc.to_traj_gdf(agg={'tracker':'mode', 'sequence':['min', 'max']})" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "## Exporting to GIS file formats" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "These GeoDataFrames can be exported to different file formats using GeoPandas, as documented in https://geopandas.org/docs/user_guide/io.html" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "export_gdf = tc.to_traj_gdf(agg={'sequence':['min', 'max']})\n", "export_gdf.to_file(\"temp.gpkg\", layer='trajectories', driver=\"GPKG\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "read_file('temp.gpkg').plot()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "read_file('temp.gpkg')" ] }, { "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 }