import gpxpy
import mplleaflet
import pykalman
import pandas as pd
import matplotlib.pyplot as plt
plt.rcParams['axes.xmargin'] = 0.1
plt.rcParams['axes.ymargin'] = 0.1
%matplotlib inline
import seaborn as sns
sns.set_style("whitegrid")
sns.set_context("talk")
%load_ext autoreload
%autoreload 2
with open('../gpx/3-laender-giro.gpx') as fh:
gpx_file = gpxpy.parse(fh)
print("File has {} track(s).".format(len(gpx_file.tracks)))
File has 1 track(s).
print("Track has {} segment(s).".format(len(gpx_file.tracks[0].segments)))
Track has 1 segment(s).
segment = gpx_file.tracks[0].segments[0]
coords = pd.DataFrame([{'lat': p.latitude,
'lon': p.longitude,
'ele': p.elevation,
'time': p.time} for p in segment.points])
coords.set_index('time', drop=True, inplace=True)
coords.head()
ele | lat | lon | |
---|---|---|---|
time | |||
2016-06-26 04:31:27 | 1343.0 | 46.889062 | 10.502119 |
2016-06-26 04:31:28 | 1343.0 | 46.889062 | 10.502119 |
2016-06-26 04:31:29 | 1343.0 | 46.889062 | 10.502119 |
2016-06-26 04:31:30 | 1344.0 | 46.889065 | 10.502119 |
2016-06-26 04:31:31 | 1345.0 | 46.889068 | 10.502121 |
plt.plot(coords['lon'].values, coords['lat'].values)
plt.title("Track")
<matplotlib.text.Text at 0x1156d7c18>
coords.ele.plot(title='Elevation')
<matplotlib.axes._subplots.AxesSubplot at 0x1156ddb70>
for point in segment.points:
point.elevation = None
import srtm
elevation_data = srtm.get_data()
elevation_data.add_elevations(gpx_file, smooth=True)
coords['new_ele'] = [p.elevation for p in gpx_file.tracks[0].segments[0].points]
coords[['ele','new_ele']].plot(title='Elevation')
<matplotlib.axes._subplots.AxesSubplot at 0x1187b8cc0>
start_time, end_time = segment.get_time_bounds()
duration = end_time - start_time
average_speed = segment.length_3d() / duration.seconds * 3.6
print("Average speed is {:.2f} km/h".format(average_speed))
Average speed is 19.93 km/h
segment.points[0].speed = 0.0
segment.points[-1].speed = 0.0
gpx_file.add_missing_speeds()
coords['speed'] = [p.speed for p in segment.points]
coords['speed'] *= 3.6
coords['speed'].plot()
<matplotlib.axes._subplots.AxesSubplot at 0x111ae2be0>
coords['speed'].max()
235.81869835597354
segment.get_uphill_downhill()
UphillDownhill(uphill=5232.6028913648215, downhill=5232.602891364802)