Add task infrastructure.

debian-sid
Tom Payne 15 years ago
parent 6bb88e80f0
commit 58f8acf103

@ -0,0 +1,152 @@
#!/usr/bin/python
#
# bin/igc2task.py IGC to task converter
# 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 timedelta
try:
from xml.etree.cElementTree import ElementTree
except ImportError:
from xml.etree.ElementTree import ElementTree
from optparse import OptionParser
import os
import re
import sys
sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..'))
from igc2kmz.coord import Coord
from igc2kmz.igc import IGC
from igc2kmz.task import Task, Turnpoint
def find_nth(function, iterable, n=0):
for element in iterable:
if function(element):
if n <= 0:
return element
else:
n -= 1
return None
def main(argv):
parser = OptionParser(
usage='Usage: %prog [options] filename.igc',
description='IGC to task converter')
parser.add_option('-o', '--output', metavar='FILENAME',
help='set output filename')
parser.add_option('-n', '--name', metavar='NAME',
help='set task name')
parser.add_option('-z', '--tz-offset', metavar='HOURS', type='int',
help='set timezone offset')
parser.add_option('--start', metavar='NAME',
help='set start turnpoint')
parser.add_option('--start-count', metavar='NUMBER', type='int',
help='set start count')
parser.add_option('--start-radius', metavar='RADIUS', type='int',
help='set start radius in meters')
parser.add_option('--start-time', metavar='TIME',
help='set start time')
parser.add_option('--ess', metavar='NAME',
help='set end of speed section turnpoint')
parser.add_option('--ess-count', metavar='NUMBER', type='int',
help='set end of speed section count')
parser.add_option('--ess-radius', metavar='RADIUS', type='int',
help='set end of speed section radius in meters')
parser.add_option('--goal', metavar='NAME',
help='set goal turnpoint')
parser.add_option('--goal-count', metavar='NUMBER', type='int',
help='set goal count')
parser.add_option('--goal-radius', metavar='RADIUS', type='int',
help='set start radius in meters')
parser.set_defaults(tz_offset=0)
parser.set_defaults(start_count=0)
parser.set_defaults(ess_count=0)
parser.set_defaults(goal_count=0)
#
options, args = parser.parse_args(argv)
if len(args) < 2:
parser.error('no IGC file specified')
if len(args) > 2:
parser.error('excess arguments on command line: %s' % repr(args[2:]))
#
igc = IGC(open(argv[1]))
if not igc.c:
parser.error('%s does not contain a task' % repr(argv[1]))
tps = []
for c in igc.c:
if c.name == 'TAKEOFF' or c.name == 'LANDING':
continue
m = re.match(r'([A-Z][0-9]{2})([0-9]{3})', c.name)
if m:
name = m.group(1)
ele = 10 * int(m.group(2))
else:
name = c.name
ele = 0
coord = Coord(c.lat, c.lon, ele)
tp = Turnpoint(name, coord)
tps.append(tp)
task = Task(options.name, tps)
#
if options.start:
start = find_nth(lambda tp: tp.name == options.start, task.tps,
options.start_count)
if not start:
parser.error('start turnpoint %s not found' % repr(options.start))
else:
start = task.tps[0]
if options.start_radius:
start.radius = int(options.start_radius)
if options.start_time:
m = re.match(r'(\d+):(\d\d)\Z', options.start_time)
if not m:
parser.error('invalid start time %s' % repr(options.start_time))
hour, minute = map(int, m.group(1, 2))
start.coord.dt = igc.b[0].dt.replace(hour=hour,
minute=minute,
second=0) \
- timedelta(seconds=3600 * options.tz_offset)
#
if options.ess:
ess = find_nth(lambda tp: tp.name == options.ess, task.tps,
options.ess_count)
if not ess:
parser.error('end of speed section turnpoint %s not found'
% repr(options.ess))
else:
options.ess = task.tps[-2]
if options.ess_radius:
ess.radius = int(options.ess_radius)
#
if options.goal:
goal = find_nth(lambda tp: tp.name == options.goal, task.tps,
options.goal_count)
if not goal:
parser.error('goal turnpoint %s not found' % repr(options.goal))
else:
goal = task.tps[-1]
if options.goal_radius:
goal.radius = int(options.goal_radius)
#
output = open(options.output, 'w') if options.output else sys.stdout
output.write('<?xml version="1.0" encoding="utf-8"?>')
ElementTree(task.to_element()).write(output)
if __name__ == '__main__':
main(sys.argv)

@ -0,0 +1,124 @@
# igc2kmz.py igc2kmz competition task 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 __future__ import with_statement
import datetime
try:
from xml.etree.cElementTree import ElementTree, TreeBuilder
except ImportError:
from xml.etree.ElementTree import ElementTree, TreeBuilder
from coord import Coord
from etree import tag
class Turnpoint(object):
def __init__(self, name, coord, radius=400, enter=True, desc=None):
self.name = 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
else:
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(self.coord.lat), 'lon': str(self.coord.lon)}
with tag(tb, 'rtept', attrs):
with tag(tb, 'name'):
tb.data(self.name)
if self.desc:
with tag(tb, 'desc'):
tb.data(self.desc)
if self.coord.ele:
with tag(tb, 'ele'):
tb.data(str(self.coord.ele))
if self.coord.dt:
with tag(tb, 'time'):
tb.data(self.coord.dt.strftime('%Y-%m-%dT%H:%M:%SZ'))
if self.radius != 400 or not self.enter:
with tag(tb, 'extensions'):
if self.radius != 400:
with tag(tb, 'radius'):
tb.data('%d' % self.radius)
if not self.enter:
with tag(tb, 'exit'):
pass
return tb
@classmethod
def from_element(cls, element):
name = element.findtext('name').encode('utf_8')
desc_tag = element.find('desc')
desc = desc_tag.text.encode('utf_8') if desc_tag else None
lat = float(element.get('lat'))
lon = float(element.get('lon'))
ele_tag = element.findtag('ele')
ele = int(ele_tag.text) if ele_tag else 0
time_tag = element.find('time')
if time_tag:
dt = datetime.datetime.strptime(time_tag.text, '%Y-%m-%dT%H:%M:%SZ')
else:
dt = None
coord = Coord(lat, lon, ele, dt)
radius_tag = element.find('extensions/radius')
radius = int(radius_tag.text) if radius_tag else 400
enter = element.find('extensions/exit') is None
return cls(name, coord, radius, enter, desc)
class Task(object):
def __init__(self, name, tps):
self.name = name
self.tps = tps
def build_tree(self, tb):
with tag(tb, 'rte'):
if self.name:
with tag(tb, 'name'):
tb.data(self.name)
for tp in self.tps:
tp.build_tree(tb)
return tb
def to_element(self):
return self.build_tree(TreeBuilder()).close()
@classmethod
def from_element(self, element):
name = etree.findtext('name').encord('utf_8')
tps = map(Turnpoint.from_element, element.findall('rtept'))
return cls(name, tps)
Loading…
Cancel
Save