You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

137 lines
4.8 KiB

# igc2kmz competition task functions
# 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
# 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 <>.
from __future__ import with_statement
import datetime
import re
from xml.etree.cElementTree import ElementTree, TreeBuilder, parse
except ImportError:
from xml.etree.ElementTree import ElementTree, TreeBuilder, parse
from coord import Coord
from etree import tag
from gpx import GPX_DATETIME_FORMAT
class Turnpoint(object):
def __init__(self, name, coord, radius=400, enter=True, desc=None): = name
self.coord = coord
self.radius = radius
self.enter = enter
self.desc = desc
def trigger(self, coord0, coord1):
if self.enter:
if self.coord.distance_to(coord0) < self.radius:
return False
if self.coord.distance_to(coord1) > self.radius:
return False
if coord0.dt < self.dt:
return False
return True
if self.coord.distance_to(coord0) > self.radius:
return False
if self.coord.distance_to(coord1) < self.radius:
return False
if coord0.dt < self.dt:
return False
return True
def build_tree(self, tb):
attrs = {'lat': str(, 'lon': str(self.coord.lon)}
with tag(tb, 'rtept', attrs):
with tag(tb, 'name'):
if self.desc:
with tag(tb, 'desc'):
if self.coord.ele:
with tag(tb, 'ele'):
if self.coord.dt:
with tag(tb, 'time'):
if self.radius != 400 or not self.enter:
with tag(tb, 'extensions'):
if self.radius != 400:
with tag(tb, 'radius'):'%d' % self.radius)
if not self.enter:
with tag(tb, 'exit'):
return tb
def from_element(cls, element, namespace):
name = element.findtext('{%s}name' % namespace).encode('utf_8')
desc_tag = element.find('{%s}desc' % namespace)
desc = None if desc_tag is None else desc_tag.text.encode('utf_8')
lat = float(element.get('lat'))
lon = float(element.get('lon'))
ele_tag = element.find('{%s}ele' % namespace)
ele = 0 if ele_tag is None else int(ele_tag.text)
time_tag = element.find('{%s}time' % namespace)
if time_tag is None:
dt = None
dt = datetime.datetime.strptime(time_tag.text, GPX_DATETIME_FORMAT)
coord = Coord.deg(lat, lon, ele, dt)
radius_tag = element.find('{%s}extensions/{%s}radius'
% (namespace, namespace))
radius = 400 if radius_tag is None else int(radius_tag.text)
enter = element.find('{%s}extensions/{%s}exit'
% (namespace, namespace)) is None
return cls(name, coord, radius, enter, desc)
class Task(object):
def __init__(self, name, tps): = name
self.tps = tps
def build_tree(self, tb):
with tag(tb, 'rte'):
with tag(tb, 'name'):
for tp in self.tps:
return tb
def to_element(self):
return self.build_tree(TreeBuilder()).close()
def from_element(cls, element, namespace):
name_tag = element.find('{%s}name' % namespace)
name = None if name_tag is None else name_tag.text.encode('utf_8')
rtepts = element.findall('{%s}rtept' % namespace)
tps = map(Turnpoint.from_element, rtepts)
return cls(name, tps)
def from_file(cls, file):
element = parse(file)
namespace = re.match('\{(.*)\}', element.getroot().tag).group(1)
return cls.from_element(element.find('/{%s}rte' % namespace), element)