#!/usr/bin/env python # coding: utf-8 # # OSM Traces (GPX files) # # # # [![Binder](https://mybinder.org/badge_logo.svg)](https://mybinder.org/v2/gh/movingpandas/movingpandas-examples/main?filepath=2-analysis-examples/osm-traces.ipynb) # [![IPYNB](https://img.shields.io/badge/view-ipynb-hotpink)](https://github.com/movingpandas/movingpandas-examples/blob/main/2-analysis-examples/osm-traces.ipynb) # [![HTML](https://img.shields.io/badge/view-html-green)](https://movingpandas.github.io/movingpandas-website/2-analysis-examples/osm-traces.html) # # This notebook illustrates the use of [GPS traces shared publicly by OSM community members](https://www.openstreetmap.org/traces) in GPX format. # # In[ ]: import numpy as np import pandas as pd import geopandas as gpd import movingpandas as mpd import shapely as shp import hvplot.pandas import matplotlib.pyplot as plt from geopandas import GeoDataFrame, read_file from shapely.geometry import Point, LineString, Polygon from datetime import datetime, timedelta from holoviews import opts, dim from os.path import exists from urllib.request import urlretrieve import warnings warnings.filterwarnings('ignore') plot_defaults = {'linewidth':5, 'capstyle':'round', 'figsize':(9,3), 'legend':True} opts.defaults(opts.Overlay(active_tools=['wheel_zoom'], frame_width=500, frame_height=400)) hvplot_defaults = {'tiles':None, 'cmap':'Viridis', 'colorbar':True} mpd.show_versions() # ## Download OSM traces and generate a GeoDataFrame # In[ ]: def get_osm_traces(page=0, bbox='16.18,48.09,16.61,48.32'): file = 'osm_traces.gpx' url = f'https://api.openstreetmap.org/api/0.6/trackpoints?bbox={bbox}&page={page}' if not exists(file): urlretrieve(url, file) gdf = gpd.read_file(file, layer='track_points') # OPTIONAL: dropping empty columns gdf.drop(columns=['ele', 'course', 'speed', 'magvar', 'geoidheight', 'name', 'cmt', 'desc', 'src', 'url', 'urlname', 'sym', 'type', 'fix', 'sat', 'hdop', 'vdop', 'pdop', 'ageofdgpsdata', 'dgpsid'], inplace=True) return gdf # ## TrajectoryCollection from OSM traces GeoDataFrame # In[ ]: gdf = get_osm_traces() osm_traces = mpd.TrajectoryCollection(gdf, 'track_fid', t='time') print(f'The OSM traces download contains {len(osm_traces)} tracks') # In[ ]: for track in osm_traces: print(f'Track {track.id}: length={track.get_length(units="km"):.2f} km') # In[ ]: track.plot() # ## Generalizing and visualizing # # Generalization is optional but speeds up rendering # In[ ]: osm_traces = mpd.MinTimeDeltaGeneralizer(osm_traces).generalize(tolerance=timedelta(minutes=1)) osm_traces.hvplot(title='OSM Traces', line_width=7, width=700, height=400) # In[ ]: osm_traces.get_trajectory(0).add_speed(overwrite=True, units=("km","h")) osm_traces.get_trajectory(0).hvplot( title='Speed (km/h) along track', c='speed', cmap='RdYlBu', line_width=7, width=700, height=400, tiles='CartoLight', colorbar=True) # ## Continue exploring MovingPandas # # 1. [Bird migration analysis](bird-migration.ipynb) # 1. [Ship data analysis](ship-data.ipynb) # 1. [Horse collar data exploration](horse-collar.ipynb) # 1. [OSM traces](osm-traces.ipynb) # 1. [Soccer game](soccer-game.ipynb) # 1. [Mars rover & heli](mars-rover.ipynb)