Add maps to trip pages

Closes: #102
This commit is contained in:
Edward Betts 2024-01-12 15:04:08 +00:00
parent a9c9c719a4
commit 60070d07fd
2 changed files with 65 additions and 1 deletions

View file

@ -5,6 +5,13 @@
{% set row = { "flight": flight_row, "train": train_row } %}
{% block style %}
{% if station_coordinates %}
<link rel="stylesheet" href="https://unpkg.com/leaflet@1.9.4/dist/leaflet.css"
integrity="sha256-p4NxAoJBhIIN+hmNHrzRCf9tD/miZyoHS5obTRR9BMY="
crossorigin=""/>
{% endif %}
{% set conference_column_count = 6 %}
{% set accommodation_column_count = 7 %}
{% set travel_column_count = 7 %}
@ -33,6 +40,11 @@
.grid-item {
/* Additional styling for grid items can go here */
}
#map {
height: 400px;
}
</style>
{% endblock %}
@ -65,7 +77,36 @@
{% endfor %}
</div>
{# <pre>{{ trip | pprint }}</pre> #}
{% if station_coordinates %}
<div id="map"></div>
{% endif %}
</div>
{% endblock %}
{% block scripts %}
{% if station_coordinates %}
<script src="https://unpkg.com/leaflet@1.9.4/dist/leaflet.js"
integrity="sha256-20nQCchB9co0qIjJZRGuk2/Z9VM+kNiyxNV1lvTlZBo="
crossorigin=""></script>
<script>
var station_coordinates = {{ station_coordinates | tojson }};
// Initialize the map
var map = L.map('map').fitBounds(station_coordinates);
// Set up the tile layer
L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
attribution: '&copy; <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
}).addTo(map);
// Add markers to the map
station_coordinates.forEach(function(coord) {
L.marker(coord).addTo(map);
});
</script>
{% endif %}
{% endblock %}

View file

@ -235,6 +235,26 @@ def trip_list() -> str:
)
def collect_station_coordinates(trip: Trip) -> list[tuple[float, float]]:
"""Extract and deduplicate station coordinates from trip."""
stations = {}
station_list = []
for t in trip.travel:
if t["type"] != "train":
continue
station_list += [t["from_station"], t["to_station"]]
for leg in t["legs"]:
station_list.append(leg["from_station"])
station_list.append(leg["to_station"])
for s in station_list:
if s["uic"] in stations:
continue
stations[s["uic"]] = s
return [(s["latitude"], s["longitude"]) for s in stations.values()]
@app.route("/trip/<start>")
def trip_page(start: str) -> str:
"""Individual trip page."""
@ -246,10 +266,13 @@ def trip_page(start: str) -> str:
if not trip:
flask.abort(404)
station_coordinates = collect_station_coordinates(trip)
return flask.render_template(
"trip_page.html",
trip=trip,
today=today,
station_coordinates=station_coordinates,
get_country=agenda.get_country,
format_list_with_ampersand=format_list_with_ampersand,
)