This notebook illustrates the use of GPS traces shared publicly by OSM community members in GPX format.
import pandas as pd
import geopandas as gpd
import movingpandas as mpd
from os.path import exists
from urllib.request import urlretrieve
from shapely.geometry import Point, LineString, Polygon
from datetime import datetime, timedelta
import warnings
warnings.simplefilter("ignore")
mpd.__version__
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
gdf = get_osm_traces()
osm_traces = mpd.TrajectoryCollection(gdf, 'track_fid', t='time')
print(f'The OSM traces download contains {len(osm_traces)} tracks')
for track in osm_traces: print(f'Track {track.id}: length={track.get_length():.0f}m')
track.plot()
Generalization is optional but speeds up rendering
osm_traces = mpd.MinTimeDeltaGeneralizer(osm_traces).generalize(tolerance=timedelta(minutes=1))
osm_traces.hvplot(title='OSM Traces', line_width=7, width=700, height=400)
osm_traces.get_trajectory(2).hvplot(title='Speed (m/s) along track', c='speed', cmap='RdYlBu',
line_width=7, width=700, height=400, tiles='CartoLight', colorbar=True)