-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathruntastic-gpx-converter.py
200 lines (159 loc) · 6.76 KB
/
runtastic-gpx-converter.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
import sys
import zipfile
import os.path
import json
import xml.etree.ElementTree as ET
import datetime
usage_message = 'RUNTASTIC-GPX-CONVERTER: bad command line'
started_message = 'conversion started, please wait ...'
success_message = 'conversion completed'
class Node:
def __init__(self, data, left=None, right=None):
self.data = data
self.left = left
self.right = right
class BST:
def __init__(self, node=None, func=None):
self.root = node
self.func = func
def insert(self, data):
if self.root == None:
self.root = Node(data)
else:
node = self.root
parent = None
while node != None:
parent = node
if self.func != None:
node = node.left if self.func(data, node.data) else node.right
else:
node = node.left if data < node.data else node.right
if self.func != None:
if self.func(data, parent.data):
parent.left = Node(data)
else:
parent.right = Node(data)
else:
if data < parent.data:
parent.left = Node(data)
else:
parent.right = Node(data)
def traversal(node):
if node != None:
yield from traversal(node.left)
yield node.data
yield from traversal(node.right)
def transformdate(date):
# change date string into ISO format
date = date.split()
date = date[0] + 'T' + date[1] + date[2][0:3] + ':' + date[2][3:]
# build datetime object from ISO format
date = datetime.datetime.fromisoformat(date)
# change timezone from local to UTC
date = date.astimezone(datetime.timezone.utc)
# return updated date in ISO format
return date.isoformat(timespec='milliseconds')
class activity:
def __init__(self):
self.id = None
self.datetime = None
self.distance = None # metres
self.duration = None # milliseconds
self.gpx = None
def getactivity(zip, basename):
# session data
sesdata = json.load(zip.open('Sport-sessions/' + basename, 'r'))
# gps data
gpsdata = json.load(zip.open('Sport-sessions/GPS-data/' + basename, 'r'))
# elevation data
eledata = json.load(zip.open('Sport-sessions/Elevation-data/' + basename, 'r'))
act = activity()
act.id = sesdata['id']
# act.datetime = gpsdata[0]['timestamp']
# change timestamp string into ISO format
timestamp = gpsdata[0]['timestamp'].split()
timestamp = timestamp[0] + 'T' + timestamp[1] + timestamp[2][0:3] + ':' + timestamp[2][3:]
# build datetime object from ISO format
act.datetime = datetime.datetime.fromisoformat(timestamp)
act.distance = '%.2f' % (sesdata['distance'] * 0.001) # from metres to kilometres
SS = (sesdata['duration'] * 0.001) # from milliseconds to seconds
MM, SS = divmod(SS, 60)
HH, MM = divmod(MM, 60)
act.duration = '%02d:%02d:%02d' % (HH, MM, SS) # from seconds to HH:MM:SS
attr = {
'creator': 'Garmin Connect',
'version': '1.1',
'xsi:schemaLocation': 'http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/11.xsd',
'xmlns:ns3': 'http://www.garmin.com/xmlschemas/TrackPointExtension/v1',
'xmlns': 'http://www.topografix.com/GPX/1/1',
'xmlns:xsi': 'http://www.w3.org/2001/XMLSchema-instance',
'xmlns:ns2': 'http://www.garmin.com/xmlschemas/GpxExtensions/v3'
}
gpx = ET.Element('gpx', attr)
meta = ET.SubElement(gpx, 'metadata')
link = ET.SubElement(meta, 'link', {'href': 'connect.garmin.com'})
text = ET.Element('text')
text.text = 'Garmin Connect'
link.append(text)
time = ET.Element('time')
# change timezone from local to UTC and use ISO format
# time.text = act.datetime.astimezone(datetime.timezone.utc).isoformat(timespec='milliseconds')
time.text = transformdate(gpsdata[0]['timestamp'])
meta.append(time)
trk = ET.SubElement(gpx, 'trk')
trkname = ET.Element('name')
trkname.text = act.id
trk.append(trkname)
trktype = ET.Element('type')
trktype.text = 'running' # sesdata['sport_type_id']
trk.append(trktype)
trkseg = ET.SubElement(trk, 'trkseg')
samelen = (len(gpsdata) == len(eledata))
for i in range(len(gpsdata)):
lat = str(gpsdata[i]['latitude'])
lon = str(gpsdata[i]['longitude'])
trkpt = ET.SubElement(trkseg, 'trkpt', {'lat' : lat, 'lon': lon})
ele = ET.Element('ele')
if samelen and (gpsdata[i]['timestamp'] == eledata[i]['timestamp']):
ele.text = str(eledata[i]['elevation'])
else:
ele.text = str(gpsdata[i]['altitude'])
trkpt.append(ele)
time = ET.Element('time')
time.text = transformdate(gpsdata[i]['timestamp'])
trkpt.append(time)
exts = ET.SubElement(trkpt, 'extensions')
ET.SubElement(exts, 'ns3:TrackPointExtension')
act.gpx = ET.tostring(gpx, encoding="UTF-8")
return act
def main():
if len(sys.argv) < 2:
print(usage_message)
sys.exit()
gpxzipname = os.path.join(os.path.dirname(sys.argv[1]), os.path.basename(sys.argv[1]).rstrip('.zip') + '_GPX.zip')
with zipfile.ZipFile(sys.argv[1], 'r') as userzip:
with zipfile.ZipFile(gpxzipname, 'w', zipfile.ZIP_DEFLATED) as gpxzip:
print(started_message)
activities = BST(func=(lambda a, b : a.datetime < b.datetime))
for filename in userzip.namelist():
if os.path.dirname(filename) == 'Sport-sessions/GPS-data':
act = getactivity(userzip, os.path.basename(filename))
gpxzip.writestr(act.id + '.gpx', act.gpx)
activities.insert(act)
html = ET.Element('html')
body = ET.SubElement(html, 'body')
table = ET.SubElement(body, 'table', {'style': 'border-collapse: collapse;'})
header = ['session id', 'datetime', 'distance (km)', 'duration']
tr = ET.SubElement(table, 'tr')
for item in header:
th = ET.SubElement(tr, 'th', {'style': 'border: 1px solid black; padding: 2px 10px;'})
th.text = item
for act in traversal(activities.root):
row = [act.id, act.datetime.strftime('%d-%m-%Y %H:%M'), act.distance, act.duration]
tr = ET.SubElement(table, 'tr')
for value in row:
td = ET.SubElement(tr, 'td', {'style': 'border: 1px solid black; padding: 2px 10px;'})
td.text = value
gpxzip.writestr('activities.html', ET.tostring(html, encoding="UTF-8", method="html"))
print(success_message)
main()