-
Notifications
You must be signed in to change notification settings - Fork 1
/
track.py
226 lines (202 loc) · 5.92 KB
/
track.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
#!/usr/bin/env python
#
# bryton-sync.py - Track manipulation
#
# Copyright (C) 2014 Adam Sutton <[email protected]>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, version 3 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# ###########################################################################
# Imports
# ###########################################################################
import copy
# ###########################################################################
# Functions
# ###########################################################################
#
# Calculate haversine distnace between 2 points
#
def haversine(lon1, lat1, lon2, lat2):
from math import radians, cos, sin, asin, sqrt
# convert decimal degrees to radians
lon1, lat1, lon2, lat2 = map(radians, [lon1, lat1, lon2, lat2])
# haversine formula
dlon = lon2 - lon1
dlat = lat2 - lat1
a = sin(dlat/2)**2 + cos(lat1) * cos(lat2) * sin(dlon/2)**2
c = 2 * asin(sqrt(a))
# 6367 km is the radius of the Earth
km = 6367 * c
return km
#
# Convert native bryton format to internal format with linear points
#
# I.e. no segments etc.. and combiend track and lap points
#
def convert ( track ):
ptp = None # previous track point
start = None
ret = []
dist = 0.0
plp = None
for seg in track:
for tp, lp in seg:
if not lp: continue # faulty (very rare)
if tp and tp.timestamp != lp.timestamp: continue # faulty
if not start:
start = lp.timestamp
if plp and lp.speed is not None:
t = lp.timestamp - plp.timestamp
dist += lp.speed * t
d = {
'timestamp' : lp.timestamp,
}
if tp:
d['latitude'] = tp.latitude
d['longitude'] = tp.longitude
d['altitude'] = tp.elevation
if lp.temperature is not None:
d['temperature'] = lp.temperature
if lp.heartrate is not None:
d['heartrate'] = lp.heartrate
if lp.cadence is not None:
d['cadence'] = lp.cadence
if lp.speed is not None:
d['speed'] = lp.speed
if dist:
d['distance'] = dist
ret.append(d)
plp = lp
return { 'timestamp' : start, 'track' : ret, 'static' : False }
#
# Perform all fixups on a track
#
def fixup ( track, conf ):
track = fixup_static(track, conf)
if not track['static']:
track = fixup_crop(track, conf)
track = fixup_missing(track, conf)
track = fixup_extrapolate(track, conf)
track = fixup_speed(track, conf)
track['timestamp'] = track['track'][0]['timestamp']
return track
#
# Check for whether this is a static track
#
def fixup_static ( track, conf ):
static = True
prev = None
for p in track['track']:
if 'latitude' not in p: continue
if prev is None: prev = p
d = abs(haversine(prev['longitude'], prev['latitude'],
p['longitude'], p['latitude']))
if d > conf['move_distance']:
static = False
break
track['static'] = static
return track
#
# Calculate the speed at each pint
#
def fixup_speed ( track, conf ):
prev = None
for p in track['track']:
if 'latitude' not in p: continue
if prev is None: prev = p
dd = abs(haversine(prev['longitude'], prev['latitude'],
p['longitude'], p['latitude']))
dt = p['timestamp'] - prev['timestamp']
if dt:
p['speed'] = (dd / dt) * 3600.0
else:
p['speed'] = 0
prev = p
return track
#
# Remove points without GPS
#
def fixup_missing ( track, conf ):
ret = []
for p in track['track']:
if 'latitude' not in p: continue
ret.append(p)
track['track'] = ret
return track
#
# Extrapolate points as per configuration
#
def fixup_extrapolate ( track, conf ):
period = int(conf['update_period'])
ret = [ track['track'][0] ]
for p in track['track'][1:]:
t = ret[-1]['timestamp']
dt = float(p['timestamp'] - t)
ds = (p['speed'] - ret[-1]['speed']) / dt
llf = ((p['speed'] + ret[-1]['speed']) / 2) * dt
dlat = (p['latitude'] - ret[-1]['latitude'])
dlon = (p['longitude'] - ret[-1]['longitude'])
delta = {}
for k in p:
if k not in ret[-1]: continue
delta[k] = (p[k] - ret[-1][k]) / dt
t += period
while t < p['timestamp']:
np = copy.copy(ret[-1])
np['timestamp'] = t
for k in np:
if k in [ 'timestamp' ]: continue
if k not in delta: continue
np[k] = ret[-1][k] + delta[k]
np['speed'] = ret[-1]['speed'] + ds
if llf:
i = ((ret[-1]['speed'] + np['speed']) / 2.0) / llf
else:
i = 0.0
np['latitude'] = ret[-1]['latitude'] + (i * dlat)
np['longitude'] = ret[-1]['longitude'] + (i * dlon)
ret.append(np)
t += period
ret.append(p)
track['track'] = ret
return track
#
# Attempt to crop the track to remove dead periods at the beginning and
# the end
#
def fixup_crop ( track, conf ):
ret = []
b = 0
e = len(track['track'])
# Find start
for p in track['track']:
if 'speed' in p and p['speed'] > conf['min_speed']:
break
b += 1
# Find end
while e > 0:
p = track['track'][e-1]
if 'speed' in p and p['speed'] > conf['min_speed']:
break
e -= 1
# Crop
track['track'] = track['track'][b:e]
return track
#
# Search for bad data points
#
# ###########################################################################
# Editor Configuration
#
# vim:sts=2:ts=2:sw=2:et
# ###########################################################################