-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtcx_creator.py
90 lines (71 loc) · 3.4 KB
/
tcx_creator.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
from xml.etree.ElementTree import Element, SubElement
def generate_tcx_content(file, activity_type, start_time, total_time, total_dist, avg_hr, max_hr, data_list):
tcx = begin_tcx()
populate_tcx_header(tcx, activity_type)
populate_tcx_start_time(tcx, start_time)
populate_tcx_summary(tcx, total_time, total_dist, avg_hr, max_hr)
populate_tcx_track(tcx, data_list)
indent(tcx, 1)
return tcx
def begin_tcx():
tcx = Element('TrainingCenterDatabase', attrib={'xmlns': 'http://www.garmin.com/xmlschemas/TrainingCenterDatabase/v2'})
return tcx
def populate_tcx_header(tcx, activity_type):
activities = SubElement(tcx, 'Activities')
activity = SubElement(activities, 'Activity', attrib={'Sport': activity_type})
def populate_tcx_start_time(tcx, start_time):
SubElement(tcx.find('Activities').find('Activity'), 'Lap', attrib={'StartTime': start_time})
SubElementWithText(tcx.find('Activities').find('Activity'), 'Id', start_time)
def populate_tcx_summary(tcx, total_time, total_dist, avg_hr, max_hr):
lap = tcx.find('Activities').find('Activity').find('Lap')
tot = SubElementWithText(lap, 'TotalTimeSeconds', total_time)
dist = SubElementWithText(lap, 'DistanceMeters', total_dist)
cal = SubElementWithText(lap, 'Calories', text='0')
avg = SubElementWithSubValue(lap, 'AverageHeartRateBpm', avg_hr)
max = SubElementWithSubValue(lap, 'MaximumHeartRateBpm', max_hr)
intensity = SubElementWithText(lap, 'Intensity', 'Active')
trigger = SubElementWithText(lap, 'TriggerMethod', 'Manual')
def populate_tcx_track(tcx, data_list):
lap = tcx.find('Activities').find('Activity').find('Lap')
track = SubElement(lap, 'Track')
for d in data_list:
trackpoint = SubElement(track, 'Trackpoint')
if {'lat', 'lon'} <= set(d):
position = SubElement(trackpoint, 'Position')
latitude = SubElementWithText(position, 'LatitudeDegrees', str(d['lat']))
longtitude = SubElementWithText(position, 'LongitudeDegrees', str(d['lon']))
if 'alt' in d:
altitude = SubElementWithText(trackpoint, 'AltitudeMeters', str(int(float(d['alt']))))
if 'hr' in d:
hr = SubElementWithSubValue(trackpoint, 'HeartRateBpm', str(d['hr']))
time_offset = SubElementWithText(trackpoint, 'Time', str(d['timeoffset']))
def SubElementWithText(parent, tag, text):
element = parent.makeelement(tag, {})
parent.append(element)
element.text = text
return element
def SubElementWithSubValue(parent, tag, text):
element = parent.makeelement(tag, {})
parent.append(element)
sub = element.makeelement('Value', {})
element.append(sub)
sub.text = text
return sub
def indent(elem, level=0):
"""
Shamelessly copied from StackOverflow:
https://stackoverflow.com/questions/6039949/python-elementtree-error-trying-to-implement-a-pretty-print
"""
i = "\n" + level*" "
if len(elem):
if not elem.text or not elem.text.strip():
elem.text = i + " "
for e in elem:
indent(e, level+1)
if not e.tail or not e.tail.strip():
e.tail = i + " "
if not e.tail or not e.tail.strip():
e.tail = i
else:
if level and (not elem.tail or not elem.tail.strip()):
elem.tail = i