-
Notifications
You must be signed in to change notification settings - Fork 0
/
gpxpy_test.py
executable file
·256 lines (173 loc) · 7.12 KB
/
gpxpy_test.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
#!/usr/local/bin/python3
# https://nbviewer.org/github/FlorianWilhelm/gps_data_with_python/blob/master/talk.ipynb
import sys
import gpxpy
import math
import numpy as np
import pandas as pd
import matplotlib as mpl
import matplotlib.pyplot as plt
from matplotlib.markers import MarkerStyle
from matplotlib.transforms import Affine2D
from matplotlib.text import TextPath
from svgpathtools import svg2paths
from svgpath2mpl import parse_path
plt.rcParams['axes.xmargin'] = 0.1
plt.rcParams['axes.ymargin'] = 0.1
import seaborn as sns
sns.set_style("whitegrid")
sns.set_context("talk")
import warnings # to suppress warnings of Seaborn's deprecated usage of Matplotlib
warnings.filterwarnings("ignore")
from distance_calc import calc_distance
import datetime
TZ_DIFF=-5*3600 # time difference in seconds from UTC
epoch = datetime.datetime(2023, 5, 28, 6, 42, 33).strftime('%s')
print(epoch)
exit()
# ============================================= #
def get_bearing(lat1,lon1,lat2,lon2):
dLon = lon2 - lon1;
y = math.sin(dLon) * math.cos(lat2);
x = math.cos(lat1)*math.sin(lat2) - math.sin(lat1)*math.cos(lat2)*math.cos(dLon);
brng = np.rad2deg(math.atan2(y, x));
if brng < 0: brng+= 360
return brng
def print_gpx_details(gpx_file):
print(gpx_file.get_bounds().max_latitude)
print(gpx_file.get_bounds().max_longitude)
print(gpx_file.get_bounds().min_latitude)
print(gpx_file.get_bounds().min_longitude)
print ()
print(gpx_file.get_duration())
print(gpx_file.get_time_bounds())
print(gpx_file.has_elevations())
print(gpx_file.has_times())
print(gpx_file.length_2d())
print(gpx_file.length_3d())
moving_data = gpx_file.get_moving_data(raw=True)
print (moving_data)
def create_report(gpx_file):
indentation = ' '
info_display = ""
length_2d = gpx_file.length_2d()
length_3d = gpx_file.length_3d()
info_display += "\n%sLength 2D: %s" % (indentation, format_long_length(length_2d))
info_display += "\n%sLength 3D: %s" % (indentation, format_long_length(length_3d))
moving_time, stopped_time, moving_distance, stopped_distance, max_speed = gpx_file.get_moving_data()
info_display += "\n%sMoving time: %s" %(indentation, format_time(moving_time))
info_display += "\n%sStopped time: %s" %(indentation, format_time(stopped_time))
info_display += "\n%sMax speed: %s" % (indentation, format_speed(max_speed))
info_display += "\n%sAvg speed: %s" % (indentation, format_speed(moving_distance / moving_time) if moving_time > 0 else "?")
uphill, downhill = gpx_file.get_uphill_downhill()
info_display += "\n%sTotal uphill: %s" % (indentation, format_short_length(uphill))
info_display += "\n%sTotal downhill: %s" % (indentation, format_short_length(downhill))
info_display += "\n\n\n"
print(info_display)
# ============================================= #
def format_time(time_s):
if not time_s:
return 'n/a'
else:
minutes = math.floor(time_s / 60.)
hours = math.floor(minutes / 60.)
return '%s:%s:%s' % (str(int(hours)).zfill(2), str(int(minutes % 60)).zfill(2), str(int(time_s % 60)).zfill(2))
def format_long_length(length):
return '{:.3f} miles'.format((length / 1000.)/1.609)
def format_short_length(length):
return '{:.1f}m'.format(length)
def format_speed(speed):
if not speed:
speed = 0
else:
return '{:.0f}m/s = {:.0f} MPH'.format(speed, speed * 2.237)
# ============================================= #
with open('stews.gpx') as fh:
gpx_file = gpxpy.parse(fh)
print (dir(gpx_file))
# --- Print a report --- #
print_gpx_details(gpx_file)
create_report(gpx_file)
# ----- Make dataframe from raw data ----- #
segment = gpx_file.tracks[0].segments[0]
print (segment)
print (dir(segment))
# d1 = mod_datetime.timedelta(-1, -1)
d2 = datetime.timedelta(0, TZ_DIFF) #UTC-6 for chicago
# # move back and forward to add a total of 1 second
# segment.adjust_time(d1)
segment.adjust_time(d2)
coords = pd.DataFrame([
{'lat': p.latitude,
'lon': p.longitude,
'speed': p.speed,
'time': p.time} for p in segment.points])
coords.set_index('time', drop=True, inplace=False)
coords["speed"]=coords["speed"]*2.237
cumulative_dist=0
# ----- Calculate compass headings ----- #
total_len=len(coords['lon'])
coords["heading"]=pd.Series([-1 for i in range(0,total_len)])
coords["cum_dist"]=pd.Series([-1 for i in range(0,total_len)])
coords["cum_dist_pct"]=pd.Series([-1 for i in range(0,total_len)])
# coords["Hour"]=pd.Series([-1 for i in range(0,total_len)])
# coords["Minute"]=pd.Series([-1 for i in range(0,total_len)])
# coords["Second"]=pd.Series([-1 for i in range(0,total_len)])
# coords["AM_PM"]=pd.Series([-1 for i in range(0,total_len)])
coords["time_str"]=pd.Series([-1 for i in range(0,total_len)])
coords["date_str"]=pd.Series([-1 for i in range(0,total_len)])
L=(gpx_file.length_2d() / 1000.)/1.609
# L=float(format_long_length())
# https://www.programiz.com/python-programming/datetime/strftime
for i in range(1,total_len):
C1=(coords['lat'][i-1],coords['lon'][i-1])
C2=(coords['lat'][i],coords['lon'][i])
head=round(get_bearing(C1[0], C1[1], C2[0], C2[1]),2)
dist=calc_distance(C2,C1)/1609 # division to convert to miles-per-hour
cumulative_dist+=dist
pct=100*(cumulative_dist/L)
coords["heading"][i]=head
coords["cum_dist"][i]=round(cumulative_dist,2)
coords["cum_dist_pct"][i]=round(pct,2)
coords["time_str"][i]=coords["time"][i].strftime("%I:%M:%S %p")
coords["date_str"][i]=coords["time"][i].strftime("%a, %h %d %Y")
print (coords)
# ----- Write to CSV ----- #
coords.to_csv("ouput.csv")
exit()
# ----- Plot the track and position markers ----- #
marker_path, attributes = svg2paths('up_arrow.svg')
i=0
out_dir="/Users/shivamkundan/Developer/DashCamScripts/tracks_pngs/"
for i in range(1000,total_len):
if (i%50==0):
print (i)
fig = plt.figure(frameon=False)
ax = plt.Axes(fig, [0., 0., 1., 1.])
ax.set_axis_off()
# ax = fig.gca()
# plt.axis('off')
# plt.grid(False)
fig.add_axes(ax)
ax.set_xticks([])
ax.set_yticks([])
plt.box(False)
custom_marker = parse_path(attributes[0]['d'])
custom_marker.vertices -= custom_marker.vertices.mean(axis=0)
# Plot the 'track'
plt.plot(coords['lon'].values, coords['lat'].values,color="red")
try:
h=round(coords['heading'].values[i],0)
custom_marker = custom_marker.transformed(mpl.transforms.Affine2D().rotate_deg(180-h))
custom_marker = custom_marker.transformed(mpl.transforms.Affine2D().scale(1))
# print (f"h:{h}")
# t = Affine2D().scale(3).rotate_deg(360-h)
# m = MarkerStyle(SUCCESS_SYMBOLS[0], transform=t)
except:
# t = Affine2D().scale(3)#.rotate_deg(round(coords['heading'].values[i]),0)
# m = MarkerStyle(SUCCESS_SYMBOLS[2], transform=t)
custom_marker = custom_marker.transformed(mpl.transforms.Affine2D().scale(1))
# Plot current location marker
plt.plot(coords['lon'].values[i], coords['lat'].values[i],'o',marker=custom_marker,color="yellow",markersize=20)
# Save file
fig.canvas.print_png(f"{out_dir}{i}.png")