I'm following this tutorial, and the code is supposed to iterate over multiple .gpx files to plot a chart that shows the distance grouped per month and year. It works, but the function seems to have an issue as it reads only the first .gpx file and I can't find where it fails:
import os
import pandas as pd
import matplotlib.pyplot as plt
from IPython.display import HTML
from glob import glob
import gpxpy
#function definition
def load_run_data(gpx_path, filter=""):
gpx_files = glob(os.path.join(gpx_path, filter + "*.gpx"))
run_data = []
for file_idx, gpx_file in enumerate(gpx_files):
gpx = gpxpy.parse(open(gpx_file, 'r'))
# Loop through tracks
for track_idx, track in enumerate(gpx.tracks):
track_name = track.name
track_time = track.get_time_bounds().start_time
track_length = track.length_3d()
track_duration = track.get_duration()
track_speed = track.get_moving_data().max_speed
for seg_idx, segment in enumerate(track.segments):
segment_length = segment.length_3d()
for point_idx, point in enumerate(segment.points):
run_data.append([file_idx, os.path.basename(gpx_file), track_idx, track_name,
track_time, track_length, track_duration, track_speed,
seg_idx, segment_length, point.time, point.latitude,
point.longitude, point.elevation, segment.get_speed(point_idx)])
return run_data
data = load_run_data(gpx_path='./gpx', filter="")
df = pd.DataFrame(data, columns=['File_Index', 'File_Name', 'Index', 'Name',
'Time', 'Length', 'Duration', 'Max_Speed',
'Segment_Index', 'Segment_Length', 'Point_Time', 'Point_Latitude',
'Point_Longitude', 'Point_Elevation', 'Point_Speed'])
HTML(df.head().to_html(max_cols=4))
#conversion of distance to km
cols = ['File_Index', 'Time', 'Length', 'Duration', 'Max_Speed']
tracks = df[cols].copy()
tracks['Length'] /= 1e3
tracks.drop_duplicates(inplace=True)
tracks.head()
#add year and month columns, group the values
tracks['Year'] = tracks['Time'].apply(lambda x: x.year)
tracks['Month'] = tracks['Time'].apply(lambda x: x.month)
tracks_grouped = tracks.groupby(['Year','Month'])
tracks_grouped.describe().head()
#plot the chart
figsize=(7, 3.5)
tracks_grouped = tracks.groupby(['Year', 'Month'])
ax = tracks_grouped['Length'].sum().plot(kind='bar', figsize=figsize)
xlabels = [text.get_text() for text in ax.get_xticklabels()]
ax.set_xticklabels(xlabels, rotation=70)
_ = ax.set_ylabel('Distance (km)')
plt.show()
gpx_files = glob(os.path.join(gpx_path, filter + "*.gpx"))
?['./gpx\\file_1.gpx', './gpx\\file_2.gpx', ... ]