-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplot_gpx.py
More file actions
72 lines (55 loc) · 1.71 KB
/
plot_gpx.py
File metadata and controls
72 lines (55 loc) · 1.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""One-liner describing module.
Author:
Erik Johannes Husom
Created:
2021
"""
import sys
import gpxpy
import matplotlib.pyplot as plt
import mplleaflet
import pandas as pd
def plot_gpx(filepath):
# Load your GPX file
with open(filepath, 'r') as gpx_file:
gpx = gpxpy.parse(gpx_file)
# Initialize lists to store data
latitudes = []
longitudes = []
times = []
speeds = []
# Extract data from GPX file
for track in gpx.tracks:
for segment in track.segments:
for point in segment.points:
latitudes.append(point.latitude)
longitudes.append(point.longitude)
times.append(point.time)
if len(times) > 1:
# Calculate speed
delta_time = (times[-1] - times[-2]).total_seconds()
distance = point.distance_2d(segment.points[-2])
speed = distance / delta_time * 3.6 # Convert m/s to km/h
speeds.append(speed)
# Plotting the map
plt.figure(figsize=(10, 6))
plt.plot(longitudes, latitudes, 'b-', alpha=0.7)
mplleaflet.show()
# Create a Pandas DataFrame for speed over time
df = pd.DataFrame({'Time': times[1:], 'Speed': speeds})
df.set_index('Time', inplace=True)
# df.index = df.index.dt.tz_localize(None)
print(df)
print(df.info())
# Plotting the speed over time
plt.figure(figsize=(10, 6))
plt.plot(df.index, df['Speed'], 'r-')
plt.xlabel('Time')
plt.ylabel('Speed (km/h)')
plt.title('Speed Over Time')
plt.show()
if __name__ == '__main__':
filepath = sys.argv[1]
plot_gpx(filepath)