-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgpx-spike-fix.py
151 lines (116 loc) · 3.73 KB
/
gpx-spike-fix.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
'''
Created on 2020 M12 27
@author: E
'''
# import xmltodict
import sys
from math import sqrt
# Imported File Fields
filename = 'workingDir/Night_run.gpx' # name of file
eleflag, hrflag, cadflag, pwrflag = True, False, False, False
fv = open(filename,'r',encoding='utf-8')
fv2 = open('outputDir/Run2.gpx','w',encoding='utf-8')
def norm(trkpt1, trkpt2):
lat1, lon1 = trkpt1['latitude'], trkpt1['longitude']
lat2, lon2 = trkpt2['latitude'], trkpt2['longitude']
# print(lat1,lat2,lon1,lon2)
return sqrt( (lat2 - lat1)**2 + (lon2 - lon1)**2 )
for i in range(8):
fv2.write(fv.readline())
fv2.write(" <trkseg>\n")
# line = fv.readline()
# #fv2.write(line)
# while line != " <trkseg>\n":
# print(line)
# line = fv.readline()
# #fv2.write(line)
# #fv2.write(fv.readline())
trkptlist = list()
while fv.readline():
"""
Here we add the cleaned trackpoint data into a list called trkptlist.
Note that only the latitude and longitude data is scrubbed - the rest we can simply keep in string format since we only care about chaning latitude and longitude.
"""
line = fv.readline()
# check for end of file
if line == ' </trkseg>\n':
break
latlon = line.split('"') # latlon needs format
print(latlon)
lat = float(latlon[1])
lon = float(latlon[3])
if eleflag:
ele = fv.readline() # elevation no format
time = fv.readline() # time no format
fv.readline() # <extensions>
fv.readline() # <gpxtpx:TrackPointExtension>
if hrflag:
hr=fv.readline() # hr
if cadflag:
cad = fv.readline() # cad
if pwrflag:
pwr = fv.readline() # power
fv.readline() # </gpxtpx:TrackPointExtension>
fv.readline() # </extensions>
# fv.readline() # </trkpt>
# now create a dictionary to store the data of each trackpoint
track_point = {
'latitude': lat,
'longitude': lon,
'elevation': ele if eleflag else None,
'time': time,
'hr': hr if hrflag else None,
'cad': cad if cadflag else None,
'pwr': pwr if pwrflag else None
}
trkptlist.append(track_point)
spikeindex = list()
epsilon = 2.5e-4
for i in range(len(trkptlist)-1):
currentnorm = norm( trkptlist[i],trkptlist[i+1] )
if currentnorm > epsilon:
print(currentnorm)
spikeindex.append(i)
if currentnorm < epsilon:
fv2.write(' <trkpt lat="{}" lon="{}">\n'.format(trkptlist[i]['latitude'],trkptlist[i]['longitude']))
if eleflag:
fv2.write(trkptlist[i]['elevation'])
fv2.write(trkptlist[i]['time'])
fv2.write(" <extensions>\n")
fv2.write(" <gpxtpx:TrackPointExtension>\n")
if hrflag:
fv2.write(trkpt['hr'])
if cadflag:
fv2.write(trkptlist[i]['cad'])
if pwrflag:
fv2.write(trkptlist[i]['pwr'])
fv2.write(" </gpxtpx:TrackPointExtension>\n")
fv2.write(" </extensions>\n")
fv2.write(" </trkpt>\n")
i+=1
fv2.write(" </trkseg>\n")
fv2.write(" </trk>\n")
fv2.write("</gpx>\n")
print(spikeindex)
fv2.close()
fv.close()
# calc the diff in lat/long btw 15:45:24 and 15:45:24
#up until 15:45:24, we take the lat and long and add the difference
# find all the trkpts and put em in a list
"""
for trkpt in List:
if time<'14:45:24':
lat = lat+dlat
long = lon+dlon
# print the list out to a new text file
' <trkpt lat='+lat+' lon='+lon+'>'
' <ele>'+ele+'</ele>'
' <time>2019-11-08T'+time+'Z</time>'
' <extensions>'
' <gpxtpx:TrackPointExtension>'
' <gpxtpx:hr>'+hr+'</gpxtpx:hr>'
' <gpxtpx:cad>'+cad+'</gpxtpx:cad>'
' </gpxtpx:TrackPointExtension>'
' </extensions>'
' </trkpt>'
"""