-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathelevation.py
264 lines (206 loc) · 8.23 KB
/
elevation.py
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
import sys
import math
import gpxpy
import gpxpy.gpx
from geopy import distance
import plotly.graph_objects as go
import plotly.express as px
def add_grade(interval_list, elevation_list, grade_list, colour_dict,
increment, km_increment, elevation):
"""
Calculates the grade and adds values to the appropriate lists
:param interval_list: list of distance intervals (for x axis)
:param elevation_list: list of elevations (for y axis)
:param grade_list: list of grades (for colour)
:param colour_dict: stores area graph values by colour
:param increment: current distance interval
:param km_increment: distance between intervals
:param elevation: elevation at current point
"""
# if first point, just add the elevation
if increment == 0:
interval_list.append(increment)
elevation_list.append(elevation)
grade_list.append(0)
else:
grade = (elevation - elevation_list[-1]) / (10 * km_increment)
print('{0} {1} {2}'.format(increment, elevation, grade))
# Choose the colour for the grade
if round(grade) < 0.5:
colour_x = 'green_x'
colour_y = 'green_y'
elif round(grade) >= 0.5 and round(grade) < 3:
colour_x = 'blue_x'
colour_y = 'blue_y'
elif round(grade) >= 3 and round(grade) < 6:
colour_x = 'yellow_x'
colour_y = 'yellow_y'
elif round(grade) >= 6 and round(grade) < 9:
colour_x = 'red_x'
colour_y = 'red_y'
else:
colour_x = 'black_x'
colour_y = 'black_y'
# add co-ordinates for the area graph in the appropriate colour
colour_dict[colour_x].append(increment-km_increment)
colour_dict[colour_y].append(0)
colour_dict[colour_x].append(increment-km_increment)
colour_dict[colour_y].append(elevation_list[-1])
colour_dict[colour_x].append(increment)
colour_dict[colour_y].append(elevation)
colour_dict[colour_x].append(increment)
colour_dict[colour_y].append(0)
# also store values in appropriate lists
interval_list.append(increment)
elevation_list.append(elevation)
grade_list.append(grade)
def plot_elevation_graph(interval_list, elevation_list) -> go.Figure:
"""
Plots a basic elevation graph
:param interval_list: list of distance intervals (for x axis)
:param elevation_list: list of elevations (for y axis)
"""
chart_data = {'distance': interval_list, 'elevation': elevation_list}
fig = px.area(chart_data, x="distance", y="elevation")
# Return the chart
return fig
def plot_grade_graph(colour_dict, distance) -> go.Figure:
"""
Plots an area graph showing the grade of the route
:param colour_dict: dictionary containing all the colour areas
:return: the chart object
"""
# Create a new figure
fig = go.Figure()
# Add the traces for each grade
fig.add_trace(go.Scatter(
x=colour_dict['green_x'], y=colour_dict['green_y'],
fill='tozeroy', line=dict(color='green'),
name='< 0.5%'))
fig.add_trace(go.Scatter(
x=colour_dict['blue_x'], y=colour_dict['blue_y'],
fill='tozeroy', line=dict(color='blue'),
name='0.5-3%'))
fig.add_trace(go.Scatter(
x=colour_dict['yellow_x'], y=colour_dict['yellow_y'],
fill='tozeroy', line=dict(color='yellow'),
name='3-6%'))
fig.add_trace(go.Scatter(
x=colour_dict['red_x'], y=colour_dict['red_y'],
fill='tozeroy', line=dict(color='red'),
name='6-9%'))
fig.add_trace(go.Scatter(
x=colour_dict['black_x'], y=colour_dict['black_y'],
fill='tozeroy', line=dict(color='black'),
name='9+%'))
# Add 10 markers along the x axis
fig.update_layout(
xaxis=dict(
tickmode='linear',
tick0=0,
dtick=math.floor(distance / 10),
tickfont=dict(size=20),
),
yaxis=dict(
tickfont=dict(size=20),
),
)
# Return the chart
return fig
def calculate_grade(gpx, km_increment):
"""
Calculates the grade of the route
:param gpx: gpx file
:param km_increment: distance between intervals
:return: dictionary of color values to plot the area graph
"""
total_distance = 0 # stores the distance as we go along
last_point = None # stores the previous point
current_increment = 0 # each step of the grade graph
detailed_distance_list = [] # list of distances for every point
detailed_elevation_list = [] # list of elevations for every point
interval_list = [] # list of distance intervals (for x axis)
elevation_list = [] # list of elevations (for y axis)
grade_list = [] # list of grades (for colour)
colour_dict = { # stores area graph values by colour
'green_x': [],
'green_y': [],
'blue_x': [],
'blue_y': [],
'yellow_x': [],
'yellow_y': [],
'red_x': [],
'red_y': [],
'black_x': [],
'black_y': []
}
for track in gpx.tracks:
for segment in track.segments:
for point in segment.points:
# print('Point at ({0},{1}) -> {2}'.format(
# point.latitude, point.longitude, point.elevation
# ))
if last_point:
# Calculate the distance between the points
flat_distance = distance.distance(
(last_point.latitude, last_point.longitude),
(point.latitude, point.longitude)
).km
# print(flat_distance)
# # Calculate Euclidian distance (include elevation)
# euclidian_distance = math.sqrt(flat_distance**2 +
# (last_point.elevation - point.elevation)**2)
# print(euclidian_distance)
total_distance += flat_distance
# print('{0} {1}'.format(total_distance, point.elevation))
# add each point to the detailed distance and elevation
# lists
detailed_distance_list.append(total_distance)
detailed_elevation_list.append(round(point.elevation))
# once we pass the next increment, add the grade
if total_distance >= current_increment:
# print('{0} {1}'.format(
# total_distance,
# point.elevation))
add_grade(
interval_list,
elevation_list,
grade_list,
colour_dict,
current_increment,
km_increment,
round(point.elevation)
)
current_increment += km_increment
# store the point for the next iteration
last_point = point
return colour_dict, total_distance
def main():
if len(sys.argv) < 2:
print("Usage: python3 elevation.py [gpx_file] [km]")
sys.exit(1)
args = sys.argv
# Open the GPX file specified in the first command line argument
gpx_file = open(args[1], 'r')
# If there is no second argument, assume the value 1, then
# try to convert the second command line argument to a float
# and exit with an error message if it is not a number
if len(sys.argv) < 3:
km_increment = 1
else:
try:
km_increment = float(sys.argv[2])
except ValueError:
print("Error: km must be a number")
sys.exit(1)
# Parse the GPX file using the gpxpy library
gpx = gpxpy.parse(gpx_file)
colour_dict, distance = calculate_grade(gpx, km_increment)
# Print a basic elevation graph
# chart = plot_elevation(detailed_distance_list, detailed_elevation_list)
# Print a grade graph
chart = plot_grade_graph(colour_dict, distance)
# Display the chart
chart.show()
if __name__ == "__main__":
main()