Esempio n. 1
0
def main(argv):
    parser = OptionParser(description='olc2002 to GPX converter')
    parser.add_option('-l', '--league', metavar='STRING')
    parser.add_option('-o', '--output', metavar='FILENAME')
    parser.add_option('--debug', action='store_true')
    parser.set_defaults(debug=False)
    parser.set_defaults(league='OLC')
    options, args = parser.parse_args(argv)
    #
    routes = []
    date = None
    for line in fileinput.input(args[1:]):
        line = line.rstrip()
        m = DEBUG_DATE_RE.match(line)
        if m:
            day, mon, year = map(int, m.groups())
            date = datetime.date(year + 2000, mon, day)
            continue
        m = OUT_TYPE_RE.match(line)
        if m:
            name = PRETTY_NAME[m.group(1)]
            circuit = m.group(1) in CIRCUITS
            route = Route(name, options.league, None, None, None, circuit, [])
            routes.append(route)
            last_time = None
            continue
        m = OUT_FLIGHT_KM_RE.match(line)
        if m:
            route.distance = float(m.group(1))
            continue
        m = OUT_FLIGHT_POINTS_RE.match(line)
        if m:
            route.score = float(m.group(1))
            route.multiplier = route.score / route.distance
            continue
        m = OUT_P_RE.match(line)
        if m:
            name = 'TP%d' % len(route.tps) if route.tps else 'Start'
            lat = int(m.group(5)) + float(m.group(6)) / 60.0
            if m.group(4) == 'S':
                lat = -lat
            lon = int(m.group(8)) + float(m.group(9)) / 60.0
            if m.group(7) == 'W':
                lon = -lon
            time = datetime.time(*map(int, m.group(1, 2, 3)))
            if not last_time is None and time < last_time:
                date += datetime.timedelta(1)
            dt = datetime.datetime.combine(date, time)
            coord = Coord(lat, lon, 0, dt)
            tp = Turnpoint(name, coord)
            route.tps.append(tp)
            last_time = time
            continue
        if options.debug:
            logging.warning(line)
    for route in routes:
        route.tps[-1].name = 'Finish'
    xc = XC(routes)
    with gpx_tag(TreeBuilder()) as tb:
        element = xc.build_tree(tb).close()
    output = open(options.output, 'w') if options.output else sys.stdout
    output.write('<?xml version="1.0" encoding="UTF-8"?>')
    ElementTree(element).write(output)
Esempio n. 2
0
def main(argv):
    parser = OptionParser(description='olc2002 to GPX converter')
    parser.add_option('-l', '--league', metavar='STRING')
    parser.add_option('-o', '--output', metavar='FILENAME')
    parser.add_option('--debug', action='store_true')
    parser.set_defaults(debug=False)
    parser.set_defaults(league='OLC')
    options, args = parser.parse_args(argv)
    #
    routes = []
    date = None
    for line in fileinput.input(args[1:]):
        line = line.rstrip()
        m = DEBUG_DATE_RE.match(line)
        if m:
            day, mon, year = map(int, m.groups())
            date = datetime.date(year + 2000, mon,day)
            continue
        m = OUT_TYPE_RE.match(line)
        if m:
            name = PRETTY_NAME[m.group(1)]
            circuit = m.group(1) in CIRCUITS
            route = Route(name, options.league, None, None, None, circuit, [])
            routes.append(route)
            last_time = None
            continue
        m = OUT_FLIGHT_KM_RE.match(line)
        if m:
            route.distance = float(m.group(1))
            continue
        m = OUT_FLIGHT_POINTS_RE.match(line)
        if m:
            route.score = float(m.group(1))
            route.multiplier = route.score / route.distance
            continue
        m = OUT_P_RE.match(line)
        if m:
            name = 'TP%d' % len(route.tps) if route.tps else 'Start'
            lat = int(m.group(5)) + float(m.group(6)) / 60.0
            if m.group(4) == 'S':
                lat = -lat
            lon = int(m.group(8)) + float(m.group(9)) / 60.0
            if m.group(7) == 'W':
                lon = -lon
            time = datetime.time(*map(int, m.group(1, 2, 3)))
            if not last_time is None and time < last_time:
                date += datetime.timedelta(1)
            dt = datetime.datetime.combine(date, time)
            coord = Coord(lat, lon, 0, dt)
            tp = Turnpoint(name, coord)
            route.tps.append(tp)
            last_time = time
            continue
        if options.debug:
            logging.warning(line)
    for route in routes:
        route.tps[-1].name = 'Finish'
    xc = XC(routes)
    with gpx_tag(TreeBuilder()) as tb:
        element = xc.build_tree(tb).close()
    output = open(options.output, 'w') if options.output else sys.stdout
    output.write('<?xml version="1.0" encoding="UTF-8"?>')
    ElementTree(element).write(output)
Esempio n. 3
0
def main(argv):
    parser = optparse.OptionParser(
        usage='Usage: %prog [options] flightID',
        description='IGC to Google Earth converter (Leonardo integration)')
    parser.add_option('-o',
                      '--output',
                      metavar='FILENAME',
                      help='set output filename')
    parser.add_option('-n', '--name', metavar='STRING')
    parser.add_option('-i', '--icon', metavar='URL')
    parser.add_option('-u', '--url', metavar='URL', help='set URL')
    parser.add_option('-d',
                      '--directory',
                      metavar='PATH',
                      help='set directory')
    parser.add_option('-e', '--engine', metavar='URL', help='set engine')
    parser.add_option('-z',
                      '--tz-offset',
                      metavar='HOURS',
                      type='int',
                      help='set timezone offset')
    parser.add_option('-t',
                      '--table-prefix',
                      metavar='STRING',
                      help='set table prefix')
    parser.add_option('-x',
                      '--igc-suffix',
                      metavar='STRING',
                      help='set IGC file suffix')
    parser.add_option('-I',
                      '--igc-path',
                      metavar='STRING',
                      help='set IGC path')
    parser.add_option('-P',
                      '--photos-path',
                      metavar='STRING',
                      help='set photos path')
    parser.add_option('-U',
                      '--photos-url',
                      metavar='STRING',
                      help='set photos URL')
    parser.set_defaults(output='igc2kmz.kmz')
    parser.set_defaults(name=DEFAULT_NAME)
    parser.set_defaults(icon=DEFAULT_ICON)
    parser.set_defaults(url=DEFAULT_URL)
    parser.set_defaults(directory=DEFAULT_DIRECTORY)
    parser.set_defaults(tz_offset=0)
    parser.set_defaults(table_prefix=DEFAULT_TABLE_PREFIX)
    parser.set_defaults(igc_path=DEFAULT_IGC_PATH)
    parser.set_defaults(photos_path=DEFAULT_PHOTOS_PATH)
    parser.set_defaults(photos_url=DEFAULT_PHOTOS_URL)
    parser.set_defaults(igc_suffix='.saned.full.igc')
    options, args = parser.parse_args(argv)
    #
    flights_dir = os.path.join(options.directory, 'modules', 'leonardo',
                               'flights')
    #
    roots = []
    roots.append(make_banner(options))
    #
    metadata = MetaData(options.engine)
    pilots_table = Table(options.table_prefix + '_pilots',
                         metadata,
                         autoload=True)
    flights_table = Table(options.table_prefix + '_flights',
                          metadata,
                          autoload=True)
    flights_score_table = Table(options.table_prefix + '_flights_score',
                                metadata,
                                autoload=True)
    photos_table = Table(options.table_prefix + '_photos',
                         metadata,
                         autoload=True)
    waypoints_table = Table(options.table_prefix + '_waypoints',
                            metadata,
                            autoload=True)
    #
    flights = []
    for flightID in args[1:]:
        select = flights_table.select(flights_table.c.ID == int(flightID))
        flight_row = select.execute().fetchone()
        if flight_row is None:
            raise KeyError, id
        if flight_row.userServerID:
            pilot_id = '%(userServerID)d_%(userID)d' % flight_row
        else:
            pilot_id = flight_row.userID
        substitutions = {
            'PILOTID': str(pilot_id),
            'YEAR': str(flight_row.DATE.year),
        }
        igc_path = os.path.join(options.directory,
                                substitute(options.igc_path, substitutions),
                                flight_row.filename + options.igc_suffix)
        track = IGC(open(igc_path), date=flight_row.DATE).track()
        flight = Flight(track)
        flight.glider_type = flight_row.glider
        flight.url = options.url + SHOW_FLIGHT_URL % flight_row
        #
        select = pilots_table.select(
            (pilots_table.c.pilotID == flight_row.userID)
            & (pilots_table.c.serverID == flight_row.userServerID))
        pilot_row = select.execute().fetchone()
        if pilot_row is None:
            raise KeyError, '(%(userID)s, %(userServerID)s)' % flight_row
        flight.pilot_name = '%(FirstName)s %(LastName)s' % pilot_row
        #
        routes = []
        select = flights_score_table.select(
            flights_score_table.c.flightID == flight_row.ID)
        for flight_score_row in select.execute().fetchall():
            route_name = ROUTE_NAME[flight_score_row.type]
            league = LEAGUE[flight_score_row.method]
            distance = flight_score_row.distance
            score = flight_score_row.score
            multiplier = round(score / distance, 2)
            circuit = CIRCUIT[flight_score_row.type]
            tps = []
            for i in xrange(1, 8):
                m = B_RECORD_RE.match(flight_score_row['turnpoint%d' % i])
                if not m:
                    continue
                time = datetime.time(*map(int, m.group(1, 2, 3)))
                dt = datetime.datetime.combine(flight_row.DATE, time)
                lat = int(m.group(4)) + int(m.group(5)) / 60000.0
                if m.group(6) == 'S':
                    lat = -lat
                lon = int(m.group(7)) + int(m.group(8)) / 60000.0
                if m.group(9) == 'W':
                    lon = -lon
                coord = Coord.deg(lat, lon, 0, dt)
                name = 'Start' if i == 1 else 'TP%d' % (i - 1)
                tp = Turnpoint(name, coord)
                tps.append(tp)
            tps[-1].name = 'Finish'
            route = Route(route_name, league, distance, multiplier, score,
                          circuit, tps)
            routes.append(route)
        flight.xc = XC(routes)
        #
        if flight_row.hasPhotos:
            select = photos_table.select(
                photos_table.c.flightID == flight_row.ID)
            for photo_row in select.execute().fetchall():
                photo_url = options.url \
                        + substitute(options.photos_url, substitutions) \
                        + '/' + photo_row.name
                photo_path = os.path.join(
                    options.directory,
                    substitute(options.photos_path, substitutions),
                    photo_row.name)
                photo = Photo(photo_url, path=photo_path)
                if photo_row.description:
                    photo.description = photo_row.description
                flight.photos.append(photo)
        #
        flights.append(flight)
        #
        select = waypoints_table.select(
            waypoints_table.c.ID == flight_row.takeoffID)
        takeoff_row = select.execute().fetchone()
        if takeoff_row:
            roots.append(make_takeoff_placemark(takeoff_row))
    #
    kmz = flights2kmz(flights, roots=roots, tz_offset=options.tz_offset)
    kmz.write(options.output, '2.2')
Esempio n. 4
0
def set_flight_xc(option, opt, value, parser):
    """Set the XC of the last flight."""
    flight = parser.values.flights[-1]
    xc = XC.from_file(open(value))
    flight.xc = xc
Esempio n. 5
0
def set_flight_xc(option, opt, value, parser):
    """Set the XC of the last flight."""
    flight = parser.values.flights[-1]
    xc = XC.from_file(open(value))
    flight.xc = xc