Remark if different airport used for takeoff or landing

pull/52/head
Konstantin Gründger 2016-05-31 21:27:24 +02:00
rodzic afd46eeb32
commit 2b59894664
2 zmienionych plików z 72 dodań i 18 usunięć

Wyświetl plik

@ -23,8 +23,8 @@ def compute_takeoff_and_landing():
radius = 0.05 # the points must not exceed this radius (degree!) around the 2nd point
# takeoff / landing has to be near an airport
airport_radius = 0.05 # takeoff / landing must not exceed this radius (degree!) around the airport
airport_delta = 200 # takeoff / landing must not exceed this altitude offset above/below the airport
airport_radius = 0.025 # takeoff / landing must not exceed this radius (degree!) around the airport
airport_delta = 100 # takeoff / landing must not exceed this altitude offset above/below the airport
# calculate the start (and stop) timestamp for the computatio
last_takeoff_landing_query = app.session.query(func.max(TakeoffLanding.timestamp))

Wyświetl plik

@ -5,6 +5,7 @@ from datetime import timedelta
from sqlalchemy.sql import func, null
from sqlalchemy import and_, or_
from sqlalchemy.sql.expression import true, false, label
from sqlalchemy.orm import aliased
from ogn.model import Device, TakeoffLanding, Airport
@ -84,38 +85,76 @@ def show(airport_name, utc_delta_hours=0):
.over(order_by=and_(func.date(TakeoffLanding.timestamp+utc_timedelta),
TakeoffLanding.device_id,
TakeoffLanding.timestamp+utc_timedelta))
.label('is_takeoff_next')) \
.filter(TakeoffLanding.airport_id == airport.id) \
.label('is_takeoff_next'),
TakeoffLanding.airport_id,
func.lag(TakeoffLanding.airport_id)
.over(order_by=and_(func.date(TakeoffLanding.timestamp+utc_timedelta),
TakeoffLanding.device_id,
TakeoffLanding.timestamp+utc_timedelta))
.label('airport_id_prev'),
func.lead(TakeoffLanding.airport_id)
.over(order_by=and_(func.date(TakeoffLanding.timestamp+utc_timedelta),
TakeoffLanding.device_id,
TakeoffLanding.timestamp+utc_timedelta))
.label('airport_id_next')) \
.subquery()
# find complete flights (with takeoff and landing) with duration < 1 day
complete_flight_query = session.query(sq.c.timestamp.label('reftime'), sq.c.device_id.label('device_id'), sq.c.timestamp.label('takeoff'), sq.c.track.label('takeoff_track'), sq.c.timestamp_next.label('landing'), sq.c.track_next.label('landing_track'), label('duration', sq.c.timestamp_next - sq.c.timestamp)) \
complete_flight_query = session.query(sq.c.timestamp.label('reftime'),
sq.c.device_id.label('device_id'),
sq.c.timestamp.label('takeoff'), sq.c.track.label('takeoff_track'), sq.c.airport_id.label('takeoff_airport_id'),
sq.c.timestamp_next.label('landing'), sq.c.track_next.label('landing_track'), sq.c.airport_id_next.label('landing_airport_id'),
label('duration', sq.c.timestamp_next - sq.c.timestamp)) \
.filter(and_(sq.c.is_takeoff == true(), sq.c.is_takeoff_next == false())) \
.filter(sq.c.device_id == sq.c.device_id_next) \
.filter(sq.c.timestamp_next - sq.c.timestamp < timedelta(days=1))
.filter(sq.c.timestamp_next - sq.c.timestamp < timedelta(days=1)) \
.filter(or_(sq.c.airport_id == airport.id,
sq.c.airport_id_next == airport.id))
# split complete flights (with takeoff and landing) with duration > 1 day into one takeoff and one landing
split_start_query = session.query(sq.c.timestamp.label('reftime'), sq.c.device_id.label('device_id'), sq.c.timestamp.label('takeoff'), sq.c.track.label('takeoff_track'), null().label('landing'), null().label('landing_track'), null().label('duration')) \
split_start_query = session.query(sq.c.timestamp.label('reftime'),
sq.c.device_id.label('device_id'),
sq.c.timestamp.label('takeoff'), sq.c.track.label('takeoff_track'), sq.c.airport_id.label('takeoff_airport_id'),
null().label('landing'), null().label('landing_track'), null().label('landing_airport_id'),
null().label('duration')) \
.filter(and_(sq.c.is_takeoff == true(), sq.c.is_takeoff_next == false())) \
.filter(sq.c.device_id == sq.c.device_id_next) \
.filter(sq.c.timestamp_next - sq.c.timestamp >= timedelta(days=1))
.filter(sq.c.timestamp_next - sq.c.timestamp >= timedelta(days=1)) \
.filter(and_(sq.c.airport_id == airport.id,
sq.c.airport_id_next == airport.id))
split_landing_query = session.query(sq.c.timestamp_next.label('reftime'), sq.c.device_id.label('device_id'), null().label('takeoff'), null().label('takeoff_track'), sq.c.timestamp_next.label('landing'), sq.c.track_next.label('landing_track'), null().label('duration')) \
split_landing_query = session.query(sq.c.timestamp_next.label('reftime'),
sq.c.device_id.label('device_id'),
null().label('takeoff'), null().label('takeoff_track'), null().label('takeoff_airport_id'),
sq.c.timestamp_next.label('landing'), sq.c.track_next.label('landing_track'), sq.c.airport_id_next.label('landing_airport_id'),
null().label('duration')) \
.filter(and_(sq.c.is_takeoff == true(), sq.c.is_takeoff_next == false())) \
.filter(sq.c.device_id == sq.c.device_id_next) \
.filter(sq.c.timestamp_next - sq.c.timestamp >= timedelta(days=1))
.filter(sq.c.timestamp_next - sq.c.timestamp >= timedelta(days=1)) \
.filter(and_(sq.c.airport_id == airport.id,
sq.c.airport_id_next == airport.id))
# find landings without start
only_landings_query = session.query(sq.c.timestamp.label('reftime'), sq.c.device_id.label('device_id'), null().label('takeoff'), null().label('takeoff_track'), sq.c.timestamp.label('landing'), sq.c.track_next.label('landing_track'), null().label('duration')) \
only_landings_query = session.query(sq.c.timestamp.label('reftime'),
sq.c.device_id.label('device_id'),
null().label('takeoff'), null().label('takeoff_track'), null().label('takeoff_airport_id'),
sq.c.timestamp.label('landing'), sq.c.track_next.label('landing_track'), sq.c.airport_id_next.label('landing_airport_id'),
null().label('duration')) \
.filter(sq.c.is_takeoff == false()) \
.filter(or_(sq.c.device_id != sq.c.device_id_prev,
sq.c.is_takeoff_prev == false()))
sq.c.is_takeoff_prev == false())) \
.filter(sq.c.airport_id_next == airport.id)
# find starts without landing
only_starts_query = session.query(sq.c.timestamp.label('reftime'), sq.c.device_id.label('device_id'), sq.c.timestamp.label('takeoff'), sq.c.track.label('takeoff_track'), null().label('landing'), null().label('landing_track'), null().label('duration')) \
only_starts_query = session.query(sq.c.timestamp.label('reftime'),
sq.c.device_id.label('device_id'),
sq.c.timestamp.label('takeoff'), sq.c.track.label('takeoff_track'), sq.c.airport_id.label('takeoff_airport_id'),
null().label('landing'), null().label('landing_track'), null().label('landing_airport_id'),
null().label('duration')) \
.filter(sq.c.is_takeoff == true()) \
.filter(or_(sq.c.device_id != sq.c.device_id_next,
sq.c.is_takeoff_next == true()))
sq.c.is_takeoff_next == true())) \
.filter(sq.c.airport_id == airport.id)
# unite all
union_query = complete_flight_query.union(
@ -125,16 +164,22 @@ def show(airport_name, utc_delta_hours=0):
only_starts_query) \
.subquery()
# get aircraft informations and sort all entries by the reference time
# get aircraft and airport informations and sort all entries by the reference time
takeoff_airport = aliased(Airport, name='takeoff_airport')
landing_airport = aliased(Airport, name='landing_airport')
logbook_query = session.query(
union_query.c.reftime,
union_query.c.takeoff,
union_query.c.takeoff_track,
takeoff_airport,
union_query.c.landing,
union_query.c.landing_track,
landing_airport,
union_query.c.duration,
Device) \
.outerjoin(Device, union_query.c.device_id == Device.id) \
.outerjoin(takeoff_airport, union_query.c.takeoff_airport_id == takeoff_airport.id) \
.outerjoin(landing_airport, union_query.c.landing_airport_id == landing_airport.id) \
.order_by(union_query.c.reftime)
print('--- Logbook ({}) ---'.format(airport_name))
@ -154,8 +199,16 @@ def show(airport_name, utc_delta_hours=0):
def none_aircraft_replacer(device_object):
return '(unknown)' if device_object.aircraft is None else device_object.aircraft
for [reftime, takeoff, takeoff_track, landing, landing_track, duration, device] in logbook_query.all():
print('%10s %8s (%2s) %8s (%2s) %8s %8s %s' % (
def airport_marker(takeoff_airport_object, landing_airport_object):
if takeoff_airport_object is not None and takeoff_airport_object.name is not airport.name:
return ('FROM: {}'.format(takeoff_airport_object.name))
elif landing_airport_object is not None and landing_airport_object.name is not airport.name:
return ('TO: {}'.format(landing_airport_object.name))
else:
return ('')
for [reftime, takeoff, takeoff_track, takeoff_airport, landing, landing_track, landing_airport, duration, device] in logbook_query.all():
print('%10s %8s (%2s) %8s (%2s) %8s %8s %17s %20s' % (
reftime.date(),
none_datetime_replacer(takeoff),
none_track_replacer(takeoff_track),
@ -163,4 +216,5 @@ def show(airport_name, utc_delta_hours=0):
none_track_replacer(landing_track),
none_timedelta_replacer(duration),
none_registration_replacer(device),
none_aircraft_replacer(device)))
none_aircraft_replacer(device),
airport_marker(takeoff_airport, landing_airport)))