-
Notifications
You must be signed in to change notification settings - Fork 0
/
parsetcx.py
342 lines (275 loc) · 9.57 KB
/
parsetcx.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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
# Copyright (C) 2017 Braden Mitchell
#
# 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, either version 3 of the License, or
# (at your option) any later version.
#
# 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/>.
# ----------------------------------------------------------------------------
from abc import ABCMeta, abstractmethod
from datetime import datetime
from lxml import etree
NS = {'ns': 'http://www.garmin.com/xmlschemas/TrainingCenterDatabase/v2',
'ns3': 'http://www.garmin.com/xmlschemas/ActivityExtension/v2'}
class TCXParser(object):
def __init__(self, tcx_file):
root = self.get_root(tcx_file)
if not root is None:
self.time = Time(root)
self.heartrate = HeartRate(root)
self.distance = Distance(root)
self.speed = Speed(root)
self.cadence = Cadence(root)
self.location = Location(root)
self.altitude = Altitude(root)
self.pace = Pace(self.time, self.distance)
self.device = self.get_device_info(root)
def get_root(self, tcx_file):
if not tcx_file is None:
if tcx_file.lower().endswith(('.tcx', '.xml')):
with open(tcx_file, 'r') as f:
tree = etree.parse(f)
root = tree.xpath('/ns:TrainingCenterDatabase',
namespaces=NS)[0]
if not root is None:
return root.xpath('./ns:Activities/ns:Activity',
namespaces=NS)[0]
def get_device_info(self, root):
device = root.xpath('./ns:Creator/ns:Name', namespaces=NS)[0].text
t = root.xpath('./ns:Creator/ns:Version', namespaces=NS)[0]
version = ('v' + t.xpath('./ns:VersionMajor', namespaces=NS)[0].text
+ '.' + t.xpath('./ns:VersionMinor', namespaces=NS)[0].text)
return device, version
class Measure(metaclass=ABCMeta):
def get_data(self, root, var):
if len(root.findall('./ns:Lap/ns:Track/ns:Trackpoint/ns:' + var,
namespaces=NS)):
data = []
for lap in root.xpath('./ns:Lap', namespaces=NS):
for trkpt in lap.xpath('./ns:Track/ns:Trackpoint',
namespaces=NS):
try:
data.append(float(trkpt.xpath('./ns:' + var,
namespaces=NS)[0].text))
except IndexError:
data.append(data[-1])
return data
def __repr__(self):
return repr(self.data)
def __iter__(self):
yield from self.data
def __getitem__(self, index):
return self.data[index]
def __len__(self):
return len(self.data)
class Time(Measure):
def __init__(self, root):
self.data, abs_time = self.get_data(root)
self.absolute = AbsTime(abs_time)
def get_data(self, root):
if len(root.findall('./ns:Lap/ns:Track/ns:Trackpoint/ns:Time',
namespaces=NS)):
raw = []
for lap in root.xpath('./ns:Lap', namespaces=NS):
for trkpt in lap.xpath('./ns:Track/ns:Trackpoint',
namespaces=NS):
try:
raw.append(datetime.strptime(str(trkpt.xpath(
'./ns:Time', namespaces=NS)[0].text),
'%Y-%m-%dT%H:%M:%S.%fZ'))
except IndexError:
raw.append(data[-1])
data = [x - raw[0] for x in raw]
return data, raw
def __repr__(self):
return repr([repr(x) for x in self.data])
def __str__(self):
return str([str(x) for x in self.data])
@property
def duration(self):
if self.data:
return self.data[-1]
@property
def start(self):
if self.data:
return self.absolute[0]
@property
def finish(self):
if self.data:
return self.absolute[-1]
class AbsTime(Measure):
def __init__(self, abs_time):
self.data = abs_time
def __repr__(self):
return repr([repr(x) for x in self.data])
def __str__(self):
return str([str(x) for x in self.data])
@property
def start(self):
if self.data:
return self.data[0]
@property
def finish(self):
if self.data:
return self.data[-1]
class HeartRate(Measure):
def __init__(self, root):
var = 'HeartRateBpm/ns:Value'
self.data = self.get_data(root, var)
@property
def min(self):
if self.data:
return min(self.data)
@property
def max(self):
if self.data:
return max(self.data)
@property
def average(self):
if self.data:
return sum(self.data) / len(self.data)
class Distance(Measure):
def __init__(self, root):
var = 'DistanceMeters'
self.data = self.get_data(root, var)
@property
def raw(self):
if self.data:
cumdist = []
for i, val in enumerate(self.data):
if i == 0:
cumdist.append(val)
else:
cumdist.append(val - sum(li))
return cumdist
@property
def total(self):
if self.data:
return self.data[-1]
class Speed(Measure):
def __init__(self, root):
var = 'Extensions/ns3:TPX/ns3:Speed'
self.data = self.get_data(root, var)
@property
def min(self):
if self.data:
return min(self.data)
@property
def max(self):
if self.data:
return max(self.data)
@property
def average(self):
if self.data:
return sum(self.data) / len(self.data)
class Cadence(Measure):
def __init__(self, root):
var = 'Extensions/ns3:TPX/ns3:RunCadence'
self.data = self.get_data(root, var)
@property
def min(self):
if self.data:
return min(self.data)
@property
def max(self):
if self.data:
return max(self.data)
@property
def average(self):
if self.data:
return sum(self.data) / len(self.data)
class Location(Measure):
def __init__(self, root):
self.data = self.get_data(root)
def get_data(self, root):
if len(root.findall('./ns:Lap/ns:Track/ns:Trackpoint/ns:Position',
namespaces=NS)):
data = []
for lap in root.xpath('./ns:Lap', namespaces=NS):
for trkpt in lap.xpath('./ns:Track/ns:Trackpoint',
namespaces=NS):
try:
data.append((float(trkpt.xpath('./ns:Position/ns:LatitudeDegrees',
namespaces=NS)[0].text),
float(trkpt.xpath('./ns:Position/ns:LongitudeDegrees',
namespaces=NS)[0].text)))
except IndexError:
data.append(data[-1])
return data
@property
def start(self):
if self.data:
return self.data[0]
@property
def finish(self):
if self.data:
return self.data[-1]
@property
def latitude(self):
if self.data:
return [lat for lat, lon in self.data]
@property
def longitude(self):
if self.data:
return [lon for lat, lon in self.data]
class Altitude(Measure):
def __init__(self, root):
var = 'AltitudeMeters'
self.data = self.get_data(root, var)
@property
def min(self):
if self.data:
return min(self.data)
@property
def max(self):
if self.data:
return max(self.data)
@property
def average(self):
if self.data:
return sum(self.data) / len(self.data)
@property
def change(self):
if self.data:
alt_delta = []
for i, val in enumerate(self.data):
if i == 0:
alt_delta.append(val - val)
else:
alt_delta.append(val - self.data[i - 1])
return alt_delta
class Pace(Measure):
def __init__(self, _time, dist):
self.data = self.get_data(_time, dist)
def get_data(self, _time, dist):
if _time and dist:
pace = []
for t, d in zip(_time, dist):
pace.append((((t - _time[0]).seconds / 60) / (d / 1000)))
return pace
# def __repr__(self):
# return repr([time.strftime('%M:%S', time.gmtime(x * 60)) for x in self.data])
@property
def seconds(self):
if self.data:
return [x * 60 for x in self.data]
@property
def min(self):
if self.data:
return max(self.data)
@property
def max(self):
if self.data:
li = [x for x in self.data if x > 0]
return min(li)
@property
def average(self):
if self.data:
return self.data[-1]