forked from jogick/GPX-from-Canon-log
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGPS_to_GPX.py
67 lines (53 loc) · 2.61 KB
/
GPS_to_GPX.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
# coding: utf8
import sys
point = {"lat":"", "lon":"", "ele":"", "date":""} # словарь для одной точки
params = sys.argv[1:] # получили параметры командной строки
for param_file in params:
track = {} # пустой словарь для точек всего трека
file_name = param_file.split('.')[0] # взяли имя очередного файла без расширения
file = open(param_file, 'r') # открыли его на чтение
def degmin_to_deg(strDM, sph): # функция переводит из строки формата ггмм.мммм в гг.ггггг в sph - полушарие (N, S, E, W)
fDM = float(strDM)
deg = int((fDM/100))
ddeg = ((fDM/100)%1)*100/60
deg = deg+ddeg
if ((sph == 'S') or (sph == 'W')):
deg = deg * (-1)
return "%.5F" % deg
for i in file: # цикл построчного разбора файла
string = i[:-1].split(',')
if len(string) < 2: continue
time = int(string[1].split(".")[0])
if ((time in track) == False):
track[time] = point.copy()
if string[0] == '$GPGGA':
track[time]['ele'] = string[9]
if string[0] == '$GPRMC':
track[time]['lat'] = degmin_to_deg(string[3], string[4])
track[time]['lon'] = degmin_to_deg(string[5], string[6])
stime = string[1].split('.')[0]
sdate = string[9]
date = "20%s-%s-%sT%s:%s:%sZ" % (sdate[4:], sdate[2:4], sdate[:2], stime[:2], stime[2:4], stime[4:])
track[time]['date'] = date
file.close() # закрыли входой файл за ненадобностью
trkpt = '''
<trkpt lat="%(lat)s" lon="%(lon)s">
<time>%(date)s</time>
<ele>%(ele)s</ele>
</trkpt>'''
gpx_str = '''<?xml version="1.0" encoding="UTF-8"?>
<gpx xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.1" xmlns="http://www.topografix.com/GPX/1/1" creator="Canon EOS 6D Mark II" xsi:schemaLocation="http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd">
<trk>
<name>Canon EOS 6D Mark II</name>
<trkseg>%s
</trkseg>
</trk>
</gpx>'''
track_string = ''
for i in sorted(track.keys()):
track_string = track_string + trkpt % (track[i])
gpx = gpx_str % track_string
gpx_file_name = "%s.gpx" % file_name
gpx_file = open(gpx_file_name, 'w')
gpx_file.write(gpx)
gpx_file.close()