-
Notifications
You must be signed in to change notification settings - Fork 0
/
gpxtests.py
104 lines (77 loc) · 3.07 KB
/
gpxtests.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
import gpxpy as gp
import matplotlib.pyplot as plt
import numpy as np
class DataExtract():
#data = '/Users/ushhamilton/Documents/03 Programming/Python/Mapping/Data/Mosey_in_the_Mournes.gpx'
col_dict = {
"time": 0,
"latitude": 1,
"longitude": 2,
"elevation": 3,
"distance": 4,
"gradient": 5,
"speed": 6
}
unit_convert = {
"time2min": 1/60,
"time2hr": 1/3600,
"meter2km": 1/1000,
"gradient2percent": 100,
"speed2kmh": 3.6
}
def __init__(self, loc):
self.loc = loc
reader = open(self.loc, 'r')
self.data = gp.parse(reader).tracks[0]
def data_to_list(self, data):
points = []
for segment in data.segments:
for point in segment.points:
points.append([point.time, point.latitude, point.longitude, point.elevation])
points.sort()
return points
def calcs_to_list(self, points):
new_points = []
for idx, pt in enumerate(points):
if idx == 0:
dist, grad, speed = 0, 0, 0
else:
dist = gp.geo.distance(pt[self.col_dict["latitude"]], pt[self.col_dict["longitude"]], pt[self.col_dict["elevation"]], prev_lat, prev_long, prev_ele)
grad = (pt[self.col_dict["elevation"]] - prev_ele) / dist
speed = dist / (pt[self.col_dict["time"]] - prev_time).total_seconds()
ls = [*pt, dist, grad, speed]
new_points.append(ls)
prev_lat, prev_long, prev_ele, prev_time= pt[self.col_dict["latitude"]], pt[self.col_dict["longitude"]], pt[self.col_dict["elevation"]], pt[self.col_dict["time"]]
return new_points
def x_y_graph(self, xvar, yvar, time=False, culmulate=True):
data = self.calcs_to_list(self.data_to_list(self.data))
graph_data = []
for idx, pt in enumerate(data):
if idx == 0:
culm = 0
else:
if time:
culm += (pt[self.col_dict[xvar]] - culm_prev).total_seconds()
else:
if culmulate:
culm += pt[self.col_dict[xvar]]
else:
culm = pt[self.col_dict[xvar]]
graph_data.append([culm, pt[self.col_dict[yvar]]])
culm_prev = pt[self.col_dict[xvar]]
return np.array(graph_data)
def trend_line(self, data, rolling):
def moving_average(x, w):
return np.convolve(x, np.ones(w), 'valid') / w
return moving_average(data, rolling)
# x = DataExtract()
# res = x.x_y_graph("distance", "speed", False, True)
# res2 = x.x_y_graph("distance", "gradient", False, True)
# # fig, ax1 = plt.subplots()
# # ax2 = ax1.twinx()
# # ax1.plot(res[:, 0], res[:, 1]*3.6, c='lightgrey')
# n = 60
# # ax2.plot(res[:, 0][n-1:], x.trend_line(res2[:, 1], n)*100, c='b')
# # ax1.plot(res[:, 0][n-1:], x.trend_line(res[:, 1], n)*3.6, c='red')
# plt.scatter(x.trend_line(res2[:, 1], n)*100, x.trend_line(res[:, 1], n)*3.6)
# plt.show()