Show lines connecting transport stops on map

Closes: #104
This commit is contained in:
Edward Betts 2024-01-12 17:17:12 +00:00
parent 4b8b1f7556
commit e993329939
2 changed files with 33 additions and 0 deletions

View file

@ -93,6 +93,7 @@
<script> <script>
var coordinates = {{ coordinates | tojson }}; var coordinates = {{ coordinates | tojson }};
var routes = {{ routes | tojson }};
// Initialize the map // Initialize the map
var map = L.map('map').fitBounds(coordinates.map(function(station) { var map = L.map('map').fitBounds(coordinates.map(function(station) {
@ -123,6 +124,11 @@ coordinates.forEach(function(item) {
marker.bindPopup(item.name); marker.bindPopup(item.name);
}); });
// Draw lines for routes
routes.forEach(function(route) {
L.polyline(route, {color: 'blue'}).addTo(map);
});
</script> </script>
{% endif %} {% endif %}
{% endblock %} {% endblock %}

View file

@ -296,6 +296,31 @@ def collect_trip_coordinates(trip: Trip) -> list[StrDict]:
] ]
def latlon_tuple(stop: StrDict) -> tuple[float, float]:
"""Given a transport stop return the lat/lon as a tuple."""
return (stop["latitude"], stop["longitude"])
def get_trip_routes(
trip: Trip,
) -> list[tuple[tuple[float, float], tuple[float, float]]]:
routes = []
for t in trip.travel:
if t["type"] == "flight":
if "from_airport" not in t or "to_airport" not in t:
continue
fly_from, fly_to = t["from_airport"], t["to_airport"]
routes.append((latlon_tuple(fly_from), latlon_tuple(fly_to)))
else:
assert t["type"] == "train"
for leg in t["legs"]:
train_from, train_to = leg["from_station"], leg["to_station"]
routes.append((latlon_tuple(train_from), latlon_tuple(train_to)))
return routes
@app.route("/trip/<start>") @app.route("/trip/<start>")
def trip_page(start: str) -> str: def trip_page(start: str) -> str:
"""Individual trip page.""" """Individual trip page."""
@ -308,12 +333,14 @@ def trip_page(start: str) -> str:
flask.abort(404) flask.abort(404)
coordinates = collect_trip_coordinates(trip) coordinates = collect_trip_coordinates(trip)
routes = get_trip_routes(trip)
return flask.render_template( return flask.render_template(
"trip_page.html", "trip_page.html",
trip=trip, trip=trip,
today=today, today=today,
coordinates=coordinates, coordinates=coordinates,
routes=routes,
get_country=agenda.get_country, get_country=agenda.get_country,
format_list_with_ampersand=format_list_with_ampersand, format_list_with_ampersand=format_list_with_ampersand,
) )