Add GPX support.

debian-sid
Tom Payne 15 years ago
parent b482f860e5
commit df18b37aa3

@ -24,6 +24,7 @@ import sys
sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..'))
from igc2kmz import Flight, flights2kmz
from igc2kmz.gpx import GPX
from igc2kmz.igc import IGC
from igc2kmz.kml import Verbatim
from igc2kmz.photo import Photo
@ -33,8 +34,14 @@ from igc2kmz.xc import XC
def add_flight(option, opt, value, parser):
"""Add a flight."""
igc = IGC(open(value))
parser.values.flights.append(Flight(igc.track()))
ext = os.path.splitext(value)[1].lower()
if ext == 'igc':
track = IGC(open(value)).track()
elif ext == 'gpx':
track = GPX(open(value)).track()
else:
raise RuntimeError, 'unsupported file type %s' % repr(ext)
parser.values.flights.append(Flight(track()))
def set_flight_option(option, opt, value, parser):

@ -0,0 +1,53 @@
# igc2kmz.py igc2kmz GPX module
# Copyright (C) 2008 Tom Payne
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from datetime import datetime
try:
from xml.etree.cElementTree import parse
except ImportError:
from xml.etree.ElementTree import parse
from coord import Coord
from track import Track
class GPX(object):
def __init__(self, file):
try:
self.filename = file.name
except AttributeError:
self.filename = '(unknown)'
self.coords = []
ns = 'http://www.topografix.com/GPX/1/1'
ele_tag_name = '{%s}ele' % ns
time_tag_name = '{%s}time' % ns
for trkpt in parse(file).findall('/{%s}trk/{%s}trkseg/{%s}trkpt'
% (ns, ns, ns)):
lat = float(trkpt.get('lat'))
lon = float(trkpt.get('lon'))
ele_tag = trkpt.find(ele_tag_name)
ele = 0 if ele_tag is None else float(ele_tag.text)
time = trkpt.find(time_tag_name)
if time is None:
continue
dt = datetime.strptime(time.text, '%Y-%m-%dT%H:%M:%SZ')
coord = Coord(lat, lon, ele, dt)
self.coords.append(coord)
def track(self):
return Track(self.coords, filename=self.filename)
Loading…
Cancel
Save