Last active
June 27, 2024 14:29
-
-
Save ArthurDelannoyazerty/eef54b621a702924e8f2b66dffb41075 to your computer and use it in GitHub Desktop.
Plot the altitude over the distance from GPX files
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import gpxpy | |
import matplotlib.pyplot as plt | |
import os | |
import re | |
from itertools import accumulate | |
size = 1.2 | |
gpx_folderpath = "gpx/" | |
def parse_gpx_file(file_path): | |
gpx_file = open(file_path, 'r') | |
gpx = gpxpy.parse(gpx_file) | |
latitudes = [] | |
longitudes = [] | |
altitudes = [] | |
distances = [] | |
previous_point = None | |
for track in gpx.tracks: | |
for segment in track.segments: | |
for point in segment.points: | |
latitudes.append(point.latitude) | |
longitudes.append(point.longitude) | |
altitudes.append(point.elevation) | |
if previous_point: | |
distance = previous_point.distance_3d(point) | |
distances.append(distance) | |
else: | |
distances.append(0) | |
previous_point = point | |
return latitudes, longitudes, altitudes, distances | |
def sort_key(file): | |
num = re.search(r'(\d+)', file).group(1) | |
num = num.replace('-', ' ') | |
num = int(num) if num.isdigit() else 0 | |
return (num, file) | |
files = [f for f in os.listdir(gpx_folderpath) if os.path.isfile(os.path.join(gpx_folderpath, f))] | |
files.sort(key=sort_key) | |
altitudes, distances, distance_per_day = [], [], [0] | |
for filename in files: | |
gpx_file_path = gpx_folderpath + filename | |
_, _, altitudes_piece, distances_piece = parse_gpx_file(gpx_file_path) | |
altitudes += altitudes_piece | |
distances += [e/1000 for e in distances_piece] | |
distance_per_day.append(sum(distances)) | |
cumulative_distances = list(accumulate(distances)) | |
plt.plot(cumulative_distances, altitudes) | |
for e in distance_per_day: | |
plt.axvline(x = e, color = (245/255,99/255,88/255)) | |
plt.xlabel('Distance (km)') | |
plt.ylabel('Altitude (m)') | |
plt.title('Altitude vs Distance') | |
plt.gcf().set_size_inches(60*size, 13*size) | |
plt.grid(True) | |
plt.savefig('out.png') | |
plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment