-
Notifications
You must be signed in to change notification settings - Fork 0
/
gps Ex 003(@resources used@).py
47 lines (36 loc) · 1.29 KB
/
gps Ex 003(@resources used@).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
import time
from time import strftime
def dms_to_dec(value, dir):
mPos = value.find(".")-2
degree = float(value[:mPos])
minute = float(value[mPos:])
converted_degree = float(degree) + float(minute)/float(60)
if dir == "W":
converted_degree = -converted_degree
elif dir == "S":
converted_degree = -converted_degree
return "%.9f" % float(round(converted_degree, 8))
def format_time(value):
pre = strftime("%Y-%m-%dT")
hour = value[:2]
minute = value[2:4]
second = value[4:6]
timeval = pre + hour + ":" + minute + ":" + second + "Z"
return timeval
def convert(inputName, outputName):
data = open(inputName, "r")
file = open(outputName, 'w')
file.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<gpx version=\"1.0\">\n")
file.write("<trk>\n<trkseg>\n")
for line in data:
gpgga = line.split(',')
if gpgga[0] == '$GPGGA':
lat_val = dms_to_dec(gpgga[2], gpgga[3])
long_val = to_dec(gpgga[4], gpgga[5])
strtrkpt = "<trkpt lat=\"" + str(lat_val) + "\" lon=\"" + str(long_val) + "\"> <time>" + format_time(gpgga[1]) + "</time> </trkpt>\n"
file.write(strtrkpt)
file.write("</trkseg>\n</trk>\n</gpx>\n")
file.close()
# main
convert('nmea.txt','nmea.gpx')
print ("Done!")