-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathconv2gpx.py
More file actions
54 lines (50 loc) · 2.25 KB
/
conv2gpx.py
File metadata and controls
54 lines (50 loc) · 2.25 KB
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
import os
from xml.dom import getDOMImplementation
import json
import time
import re
impl = getDOMImplementation()
doc = impl.createDocument(None,'gpx',None)
top_element = doc.documentElement
top_element.setAttribute('version','1.0')
top_element.setAttribute('creator','Android GPS Tracker')
top_element.setAttribute('xmlns:xsi','http://www.w3.org/2001/XMLSchema-instance')
top_element.setAttribute('xmlns','http://www.topografix.com/GPX/1/0')
top_element.setAttribute('xsi:schemaLocation','http://www.topografix.com/GPX/1/0 http://www.topografix.com/GPX/1/0/gpx.xsd')
OUTPUT_FILENAME_TEMPLATE = 'gps_output_{0}.json'
for filename in os.listdir(os.getcwd()):
match = re.match(OUTPUT_FILENAME_TEMPLATE.format('(\d*)'), filename)
try:
id = int(match.group(1))
except AttributeError:
continue
track = doc.createElement('trk')
track.appendChild(doc.createElement('name')).appendChild(doc.createTextNode('Track {0}'.format(id)))
trackseg = doc.createElement('trkseg')
lasttime = 0
f = open(filename,'r')
for line in f:
try:
point = json.loads(line)
if ((int(point['time'])-lasttime) > 1000) and trackseg.hasChildNodes():
track.appendChild(trackseg)
trackseg = doc.createElement('trkseg')
lasttime = int(point['time'])
trackpoint = doc.createElement('trkpt')
trackpoint.setAttribute('latitude',str(point['latitude']))
trackpoint.setAttribute('longitude',str(point['longitude']))
trackpoint.appendChild(doc.createElement('time')).appendChild(doc.createTextNode(str(time.strftime('%Y-%m-%dT%H-%M-%S',time.gmtime(float(point['time'])/1000)))))
trackpoint.appendChild(doc.createElement('ele')).appendChild(doc.createTextNode(str(point['altitude'])))
trackpoint.appendChild(doc.createElement('speed')).appendChild(doc.createTextNode(str(point['speed'])))
trackpoint.appendChild(doc.createElement('pdop')).appendChild(doc.createTextNode(str(point['accuracy'])))
trackpoint.appendChild(doc.createElement('fix')).appendChild(doc.createTextNode('3d'))
trackseg.appendChild(trackpoint)
except ValueError:
print 'Invalid JSON: ' + line
continue
f.close()
track.appendChild(trackseg)
top_element.appendChild(track)
f = open('gpstrack.gpx','w+')
f.write(doc.toxml())
f.close