Unbooked flight great circle goes to airport (Closes: #179)

This commit is contained in:
Edward Betts 2024-10-13 09:39:02 +01:00
parent 5f13bff9bd
commit aad968a174
2 changed files with 17 additions and 4 deletions

View file

@ -277,6 +277,19 @@ def collect_trip_coordinates(trip: Trip) -> list[StrDict]:
return coords return coords
def latlon_tuple_prefer_airport(stop: StrDict, data_dir: str) -> tuple[float, float]:
airport_lookup = {
("Berlin", "de"): "BER",
("Hamburg", "de"): "HAM",
}
iata = airport_lookup.get((stop["location"], stop["country"]))
if not iata:
return latlon_tuple(stop)
airports = typing.cast(dict[str, StrDict], travel.parse_yaml("airports", data_dir))
return latlon_tuple(airports[iata])
def latlon_tuple(stop: StrDict) -> tuple[float, float]: def latlon_tuple(stop: StrDict) -> tuple[float, float]:
"""Given a transport stop return the lat/lon as a tuple.""" """Given a transport stop return the lat/lon as a tuple."""
return (stop["latitude"], stop["longitude"]) return (stop["latitude"], stop["longitude"])
@ -287,7 +300,7 @@ def read_geojson(data_dir: str, filename: str) -> str:
return open(os.path.join(data_dir, filename + ".geojson")).read() return open(os.path.join(data_dir, filename + ".geojson")).read()
def get_trip_routes(trip: Trip) -> list[StrDict]: def get_trip_routes(trip: Trip, data_dir: str) -> list[StrDict]:
"""Get routes for given trip to show on map.""" """Get routes for given trip to show on map."""
routes: list[StrDict] = [] routes: list[StrDict] = []
seen_geojson = set() seen_geojson = set()
@ -358,7 +371,7 @@ def get_trip_routes(trip: Trip) -> list[StrDict]:
"type": "unbooked_flight", "type": "unbooked_flight",
"key": f'LHR_{item["location"]}_{item["country"]}', "key": f'LHR_{item["location"]}_{item["country"]}',
"from": lhr, "from": lhr,
"to": latlon_tuple(item), "to": latlon_tuple_prefer_airport(item, data_dir),
} }
for item in trip.conferences for item in trip.conferences
if "latitude" in item if "latitude" in item
@ -385,7 +398,7 @@ def get_coordinates_and_routes(
coordinates.append(stop) coordinates.append(stop)
seen_coordinates.add(key) seen_coordinates.add(key)
for route in get_trip_routes(trip): for route in get_trip_routes(trip, data_dir):
if route["key"] in seen_routes: if route["key"] in seen_routes:
continue continue
routes.append(route) routes.append(route)

View file

@ -542,7 +542,7 @@ def trip_page(start: str) -> str:
today = date.today() today = date.today()
coordinates = agenda.trip.collect_trip_coordinates(trip) coordinates = agenda.trip.collect_trip_coordinates(trip)
routes = agenda.trip.get_trip_routes(trip) routes = agenda.trip.get_trip_routes(trip, app.config["PERSONAL_DATA"])
agenda.trip.add_coordinates_for_unbooked_flights(routes, coordinates) agenda.trip.add_coordinates_for_unbooked_flights(routes, coordinates)