{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# OSM Traces (GPX files)\n", "\n", "\n", "\n", "[![Binder](https://mybinder.org/badge_logo.svg)](https://mybinder.org/v2/gh/movingpandas/movingpandas-examples/main?filepath=2-analysis-examples/osm-traces.ipynb)\n", "[![IPYNB](https://img.shields.io/badge/view-ipynb-hotpink)](https://github.com/movingpandas/movingpandas-examples/blob/main/2-analysis-examples/osm-traces.ipynb)\n", "[![HTML](https://img.shields.io/badge/view-html-green)](https://movingpandas.github.io/movingpandas-website/2-analysis-examples/osm-traces.html)\n", "\n", "This notebook illustrates the use of [GPS traces shared publicly by OSM community members](https://www.openstreetmap.org/traces) in GPX format. \n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "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", "from os.path import exists\n", "from urllib.request import urlretrieve\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'], frame_width=500, frame_height=400))\n", "hvplot_defaults = {'tiles':None, 'cmap':'Viridis', 'colorbar':True}\n", "\n", "mpd.show_versions()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Download OSM traces and generate a GeoDataFrame" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def get_osm_traces(page=0, bbox='16.18,48.09,16.61,48.32'):\n", " file = 'osm_traces.gpx'\n", " url = f'https://api.openstreetmap.org/api/0.6/trackpoints?bbox={bbox}&page={page}'\n", " if not exists(file):\n", " urlretrieve(url, file)\n", " gdf = gpd.read_file(file, layer='track_points')\n", " # OPTIONAL: dropping empty columns\n", " gdf.drop(columns=['ele', 'course', 'speed', 'magvar', 'geoidheight', 'name', 'cmt', 'desc',\n", " 'src', 'url', 'urlname', 'sym', 'type', 'fix', 'sat', 'hdop', 'vdop',\n", " 'pdop', 'ageofdgpsdata', 'dgpsid'], inplace=True) \n", " return gdf" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## TrajectoryCollection from OSM traces GeoDataFrame" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "gdf = get_osm_traces()\n", "osm_traces = mpd.TrajectoryCollection(gdf, 'track_fid', t='time')\n", "print(f'The OSM traces download contains {len(osm_traces)} tracks')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "for track in osm_traces: print(f'Track {track.id}: length={track.get_length(units=\"km\"):.2f} km')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "track.plot()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Generalizing and visualizing\n", "\n", "Generalization is optional but speeds up rendering" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "osm_traces = mpd.MinTimeDeltaGeneralizer(osm_traces).generalize(tolerance=timedelta(minutes=1))\n", "osm_traces.hvplot(title='OSM Traces', line_width=7, width=700, height=400)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "osm_traces.get_trajectory(0).add_speed(overwrite=True, units=(\"km\",\"h\"))\n", "osm_traces.get_trajectory(0).hvplot(\n", " title='Speed (km/h) along track', c='speed', cmap='RdYlBu',\n", " line_width=7, width=700, height=400, tiles='CartoLight', colorbar=True)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Continue exploring MovingPandas\n", "\n", "1. [Bird migration analysis](bird-migration.ipynb)\n", "1. [Ship data analysis](ship-data.ipynb)\n", "1. [Horse collar data exploration](horse-collar.ipynb)\n", "1. [OSM traces](osm-traces.ipynb)\n", "1. [Soccer game](soccer-game.ipynb)\n", "1. [Mars rover & heli](mars-rover.ipynb)" ] } ], "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.8" } }, "nbformat": 4, "nbformat_minor": 4 }