git-svn-id: svn+ssh://asterix.ieap.uni-kiel.de/home/subversion/stephan/solo/eda/cospi/host@6933 bc5caf13-1734-44f8-af43-603852e9ee25
143 lines
3.9 KiB
Python
Executable file
143 lines
3.9 KiB
Python
Executable file
#! /usr/bin/python
|
|
|
|
import xml.etree.ElementTree as ET
|
|
|
|
class Track_Point(ET.Element):
|
|
|
|
def __init__(self, lat, lon, **tags):
|
|
ET.Element.__init__(self,
|
|
tag="trkpt",
|
|
attrib = {
|
|
"lat": "%.7f" % float(lat),
|
|
"lon": "%.7f" % float(lon),
|
|
})
|
|
self.add(**tags)
|
|
|
|
def add(self, **tags):
|
|
for tag in tags:
|
|
self.append(ET.Element(tag=tag))
|
|
self[-1].text=str(tags[tag])
|
|
|
|
class Track_Segment(ET.Element):
|
|
def __init__(self):
|
|
ET.Element.__init__(self, tag="trkseg")
|
|
|
|
class Track(ET.Element):
|
|
def __init__(self, name=None):
|
|
ET.Element.__init__(self, tag="trk")
|
|
if name:
|
|
self.append(ET.Element(tag="name"))
|
|
self[-1].text = name
|
|
self.new_Segment()
|
|
|
|
def new_Segment(self):
|
|
self.append(Track_Segment())
|
|
|
|
class gpx(ET.ElementTree):
|
|
|
|
def __init__(self, name=None, creator=None):
|
|
root = ET.Element(
|
|
tag = "gpx",
|
|
attrib = {
|
|
"xmlns": "http://www.topografix.com/GPX/1/1",
|
|
"version": "1.1",
|
|
"xmlns:xsi": "http://www.w3.org/2001/XMLSchema-instance" ,
|
|
"xsi:schemaLocation" :"http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd ",
|
|
})
|
|
if creator:
|
|
root.set("creator", creator)
|
|
ET.ElementTree.__init__(self, root)
|
|
self.new_Track(name)
|
|
|
|
def append(self, trkpt):
|
|
self.getroot()[-1][-1].append(trkpt)
|
|
|
|
def new_Track(self, name=None):
|
|
self.getroot().append(Track(name))
|
|
|
|
def new_Segment(self):
|
|
self.getroot()[-1].new_Segment()
|
|
|
|
indent = " "
|
|
|
|
def __str__(self):
|
|
l = ET.tostringlist(self.getroot(), encoding="UTF-8")
|
|
indent = 0
|
|
last=""
|
|
for i,ll in enumerate(l):
|
|
if indent and ll[:2] == "</":
|
|
indent -= 1
|
|
if ll[0]=="<" and i:
|
|
if ("</"+last[1:]+">") != ll:
|
|
l[i] = "\n" + self.indent*indent + l[i]
|
|
last = ll
|
|
if i and ll[0]=="<" and ll[:2] != "</" and ll[-2:]!="/>":
|
|
indent += 1
|
|
elif indent and ll[-2:]=="/>":
|
|
indent -= 1
|
|
return "".join(l)
|
|
|
|
GPGGA = {}
|
|
def parse_gpgga(l):
|
|
try:
|
|
ll = l.strip().split(',')
|
|
if ll[0] != "$GPGGA":
|
|
return None
|
|
if ll[11]=='M':
|
|
GPGGA["ele"] = float(ll[10])
|
|
except:
|
|
pass
|
|
|
|
def parse_gprmc(l):
|
|
try:
|
|
ll = l.strip().split(',')
|
|
if ll[0] == "$GPGGA":
|
|
return parse_gpgga(l)
|
|
if ll[0] != "$GPRMC" or ll[2] != 'A':
|
|
return None
|
|
time = strftime(strptime(ll[1][:6]+ll[9], "%H%M%S%d%m%y"), "%Y-%m-%dT%H:%M:%SZ")
|
|
lat = int(ll[3][:2])+float(ll[3][2:])/60
|
|
if ll[3]=='S':
|
|
lat = -lat
|
|
lon = int(ll[5][:3])+float(ll[5][3:])/60
|
|
if ll[3]=='W':
|
|
lon = -lon
|
|
tp = Track_Point(lat, lon, time=time, **GPGGA)
|
|
GPGGA.clear()
|
|
except:
|
|
pass
|
|
|
|
def parse_strato3(l):
|
|
try:
|
|
ll = l.strip().split(';')
|
|
if ll[0]!= '$':
|
|
return None
|
|
if ll[4] != 'Y':
|
|
return None
|
|
lll = ll[3].split(".")
|
|
lll.reverse()
|
|
time = "-".join(lll)+"T"+ll[2]+"Z"
|
|
ele = float(ll[11])
|
|
lll = ll[6].split()
|
|
lat = int(lll[0]) + float(lll[1])/60
|
|
if lll[0]=='S':
|
|
lat = -lat
|
|
lll = ll[7].split()
|
|
lon = int(lll[0]) + float(lll[1])/60
|
|
if lll[0]=='W':
|
|
lon = -lon
|
|
return Track_Point(lat, lon, time=t, ele=ele)
|
|
except:
|
|
pass
|
|
|
|
def main(files=None):
|
|
import fileinput
|
|
g = gpx(creator="etgpx")
|
|
for l in fileinput.input(files):
|
|
tp = parse_strato3(l) or parse_gprmc(l)
|
|
if tp:
|
|
g.append(tp)
|
|
print(str(g))
|
|
|
|
if __name__=="__main__":
|
|
main()
|