Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Functions and Table Structure to Allow Ground Positions #42

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion db-updater/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,11 @@
sa.Column("wind_direction", sa.Integer, key="wind_dir"),
sa.Column("wind_quality", sa.Integer),
sa.Column("wind_speed", sa.Integer),
# New columns from ground_positions
sa.Column("air_ground", sa.String),
sa.Column("airport", sa.String),
)
VALID_EVENTS = {"position"}
VALID_EVENTS = {"position", "ground_position"}

engine_args: dict = {}
db_url: str = os.environ["DB_URL"]
Expand Down Expand Up @@ -480,6 +483,11 @@ def process_keepalive_message(data: dict) -> None:
print(f'Based on keepalive["pitr"], we are {behind} behind realtime')


def process_ground_position_message(data: dict) -> None:
"""Groundposition message type"""
return add_to_cache(data)


def disambiguate_altitude(data: dict):
"""Replaces the alt field in the passed dict with an unambiguous field name"""

Expand Down Expand Up @@ -534,6 +542,7 @@ def main():
"flightplan": process_flightplan_message,
"keepalive": process_keepalive_message,
"position": process_position_message,
"ground_position": process_ground_position_message,
}

consumer = None
Expand Down
46 changes: 42 additions & 4 deletions fids/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,15 @@
UTC = timezone.utc


def _get_ground_positions(flight_id: str) -> Iterable:
return (
positions_engine.execute(
positions.select().where(and_(positions.c.id == flight_id, positions.c.air_ground == "G")).order_by(positions.c.time.desc())
)
or []
)


def _get_positions(flight_id: str) -> Iterable:
return (
positions_engine.execute(
Expand All @@ -57,6 +66,15 @@ def _get_positions(flight_id: str) -> Iterable:
)


@app.route("/ground_positions/<flight_id>")
def get_ground_positions(flight_id: str) -> Response:
"""Get ground positions for a specific flight_id"""
result = _get_ground_positions(flight_id)
if not result:
abort(404)
return jsonify([dict(e) for e in result])


@app.route("/positions/<flight_id>")
def get_positions(flight_id: str) -> Response:
"""Get positions for a specific flight_id"""
Expand Down Expand Up @@ -240,18 +258,38 @@ def get_map(flight_id: str) -> bytes:
positions = list(_get_positions(flight_id))
if not positions:
abort(404)

# Do ground positions
ground_positions = list(_get_ground_positions(flight_id))
do_gp = True
if not ground_positions:
do_gp = False
bearing = 0
if len(positions) > 1:
coord1 = (float(positions[1].latitude), float(positions[1].longitude))
coord2 = (float(positions[0].latitude), float(positions[0].longitude))
if do_gp:
result = min(ground_positions[:2], positions[:2], key=lambda x: x[0].time)
# result = ground_positions[:2]
else:
result = positions[:2]

coord1 = (float(result[1].latitude), float(result[1].longitude))
coord2 = (float(result[0].latitude), float(result[0].longitude))
bearing = trig.get_cardinal_for_angle(trig.get_bearing_degrees(coord1, coord2))
else:
result = positions
coords = "|".join(f"{pos.latitude},{pos.longitude}" for pos in positions)
gp_coords = "|".join(f"{pos.latitude},{pos.longitude}" for pos in ground_positions)

google_maps_url = "https://maps.googleapis.com/maps/api/staticmap"
google_maps_params = {
"size": "640x400",
"markers": f"anchor:center|icon:https://github.com/flightaware/fids_frontend/raw/master/images/aircraft_{bearing}.png|{positions[0].latitude},{positions[0].longitude}",
"path": f"color:0x0000ff|weight:5|{coords}",
"markers": [
f"anchor:center|icon:https://github.com/flightaware/fids_frontend/raw/master/images/aircraft_{bearing}.png|{result[0].latitude},{result[0].longitude}",
],
"path": [
f"color:0x0000ff|weight:5|{coords}",
f"color:0xff0000|weight:5|{gp_coords}" if gp_coords else "",
],
"key": google_maps_api_key,
}
response = requests.get(google_maps_url, google_maps_params)
Expand Down