{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# OSM Traces (GPX files)\n", "\n", "[![Binder](https://mybinder.org/badge_logo.svg)](https://mybinder.org/v2/gh/anitagraser/movingpandas-examples/wim-winter-school-2021?filepath=2-analysis-examples/5-osm-traces.ipynb)\n", "\n", "\n", "\n", "This notebook illustrates the use of GPS traces shared publicly by OSM community members in GPX format. \n", "\n", "Source: https://www.openstreetmap.org/traces" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import pandas as pd\n", "import geopandas as gpd\n", "import movingpandas as mpd\n", "\n", "from os.path import exists\n", "from urllib.request import urlretrieve\n", "from shapely.geometry import Point, LineString, Polygon\n", "from datetime import datetime, timedelta\n", "\n", "import warnings\n", "warnings.simplefilter(\"ignore\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "mpd.__version__" ] }, { "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():.0f}m')" ] }, { "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(2).hvplot(title='Speed (m/s) 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](1-bird-migration.ipynb)\n", "1. [Ship data analysis](2-ship-data.ipynb)\n", "1. [Horse collar data exploration](3-horse-collar.ipynb)\n", "1. [Stop hotspot detection](4-stop-hotspots.ipynb)\n", "1. [OSM traces](5-osm-traces.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.7.12" } }, "nbformat": 4, "nbformat_minor": 4 }