-
Notifications
You must be signed in to change notification settings - Fork 1
/
convert.py
executable file
·117 lines (90 loc) · 2.79 KB
/
convert.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
import argparse
import datetime
import json
import logging
import os
import sys
import time
import xml.etree.cElementTree as ET
import xml.dom.minidom
"""
VRTP format:
{ "header":
{
"colour":-16777216,
"name":"T12 A1",
"lastModTime":1606135298501,
"trackId":"11699434",
"gridPositionCoordType":17
},
"points":
[
{"lat":28.397722,"lon":-16.585778,"map_x":-139131590,"map_y":248615801,"alt":119.16730730796871,"time":1623343518329}
Above json is unterminated
We want to get it into GPX, we will grab:
header.name
points.*.{lat, lon, time}
- time (if available) is epoch.
- alt is optional (routes vs tracks)
VRTP lives in `Android/data/com.augmentra.viewranger.android/files/viewrangermoved/Tracks`
"""
def validate_file(f):
if not os.path.exists(f):
raise argparse.ArgumentTypeError("{0} does not exist".format(f))
return f
def load_vrtp(path):
with open(path, encoding='utf-8', mode='r') as file:
data = file.read()
try:
j = json.loads(data)
except:
data += ']}'
j = json.loads(data)
return j
def vrtp_to_gpx(j: json):
name = j['header']['name']
root = ET.Element("gpx", {'version': '1.0'})
ET.SubElement(root, 'name').text = name
had_alt = False
had_time = False
trk = ET.SubElement(root, 'trk')
ET.SubElement(trk, 'name').text = name
ET.SubElement(trk, 'number').text = '1'
seg = ET.SubElement(trk, 'trkseg')
for p in j['points']:
trkpt = ET.SubElement(seg, 'trkpt', {'lat': str(p['lat']), 'lon': str(p['lon'])})
if 'alt' in p and p['alt'] != 0.0:
had_alt = True
ET.SubElement(trkpt, 'ele').text = str(p['alt'])
t = p['time']
if t == 0:
t = int(time.time())
else:
had_time = True
t = t / 1000
ET.SubElement(trkpt, 'time').text = datetime.datetime.utcfromtimestamp(t).isoformat()
logging.debug(f'Found altitude: {had_alt}')
logging.debug(f'Found time : {had_time}')
dom = xml.dom.minidom.parseString(ET.tostring(root))
gpx = dom.toprettyxml()
return gpx
def write_gpx(output, gpx):
with open(output, 'w') as file:
file.write(gpx)
def main() -> int:
p = argparse.ArgumentParser()
p.add_argument('-i', '--input', dest='input', required=True, type=validate_file, help='input vrtp file', metavar='FILE')
p.add_argument('-d', '--debug', action='store_true')
args = p.parse_args()
if args.debug:
logging.basicConfig(level=logging.DEBUG)
else:
logging.basicConfig(level=logging.INFO)
j = load_vrtp(args.input)
gpx = vrtp_to_gpx(j)
output = args.input + '.gpx'
logging.info(f'Will write output to: {output}')
write_gpx(output, gpx)
return 0
if __name__ == '__main__':
sys.exit(main())