Show station and airport icons on the map

This commit is contained in:
Edward Betts 2024-01-12 16:54:52 +00:00
parent 0c02d9c899
commit 4b8b1f7556
2 changed files with 45 additions and 17 deletions

View file

@ -6,7 +6,7 @@
{% block style %} {% block style %}
{% if station_coordinates %} {% if coordinates %}
<link rel="stylesheet" href="https://unpkg.com/leaflet@1.9.4/dist/leaflet.css" <link rel="stylesheet" href="https://unpkg.com/leaflet@1.9.4/dist/leaflet.css"
integrity="sha256-p4NxAoJBhIIN+hmNHrzRCf9tD/miZyoHS5obTRR9BMY=" integrity="sha256-p4NxAoJBhIIN+hmNHrzRCf9tD/miZyoHS5obTRR9BMY="
crossorigin=""/> crossorigin=""/>
@ -77,7 +77,7 @@
{% endfor %} {% endfor %}
</div> </div>
{% if station_coordinates %} {% if coordinates %}
<div id="map"></div> <div id="map"></div>
{% endif %} {% endif %}
@ -86,25 +86,41 @@
{% block scripts %} {% block scripts %}
{% if station_coordinates %} {% if coordinates %}
<script src="https://unpkg.com/leaflet@1.9.4/dist/leaflet.js" <script src="https://unpkg.com/leaflet@1.9.4/dist/leaflet.js"
integrity="sha256-20nQCchB9co0qIjJZRGuk2/Z9VM+kNiyxNV1lvTlZBo=" integrity="sha256-20nQCchB9co0qIjJZRGuk2/Z9VM+kNiyxNV1lvTlZBo="
crossorigin=""></script> crossorigin=""></script>
<script> <script>
var station_coordinates = {{ station_coordinates | tojson }}; var coordinates = {{ coordinates | tojson }};
// Initialize the map // Initialize the map
var map = L.map('map').fitBounds(station_coordinates); var map = L.map('map').fitBounds(coordinates.map(function(station) {
return [station.latitude, station.longitude];
}));
// Set up the tile layer // Set up the tile layer
L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', { L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
attribution: '&copy; <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors' attribution: '&copy; <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
}).addTo(map); }).addTo(map);
// Add markers to the map var stationIcon = L.divIcon({
station_coordinates.forEach(function(coord) { className: 'custom-div-icon',
L.marker(coord).addTo(map); html: "<div style='font-size: 24px;'>🚉</div>",
iconSize: [30, 42],
});
var airportIcon = L.divIcon({
className: 'custom-div-icon',
html: "<div style='font-size: 24px;'>✈️</div>",
iconSize: [30, 42],
});
// Add markers with appropriate icons to the map
coordinates.forEach(function(item) {
var icon = item.type === "station" ? stationIcon : airportIcon;
var marker = L.marker([item.latitude, item.longitude], { icon: icon }).addTo(map);
marker.bindPopup(item.name);
}); });
</script> </script>

View file

@ -3,7 +3,6 @@
"""Web page to show upcoming events.""" """Web page to show upcoming events."""
import inspect import inspect
import itertools
import operator import operator
import os.path import os.path
import sys import sys
@ -182,8 +181,8 @@ def load_trains() -> list[StrDict]:
for leg in train["legs"]: for leg in train["legs"]:
assert leg["from"] in by_name assert leg["from"] in by_name
assert leg["to"] in by_name assert leg["to"] in by_name
leg["from_station"] = by_name[train["from"]] leg["from_station"] = by_name[leg["from"]]
leg["to_station"] = by_name[train["to"]] leg["to_station"] = by_name[leg["to"]]
return trains return trains
@ -256,8 +255,8 @@ def trip_list() -> str:
) )
def collect_station_coordinates(trip: Trip) -> list[tuple[float, float]]: def collect_trip_coordinates(trip: Trip) -> list[StrDict]:
"""Extract and deduplicate station coordinates from trip.""" """Extract and deduplicate airport and station coordinates from trip."""
stations = {} stations = {}
station_list = [] station_list = []
airports = {} airports = {}
@ -279,8 +278,21 @@ def collect_station_coordinates(trip: Trip) -> list[tuple[float, float]]:
stations[s["uic"]] = s stations[s["uic"]] = s
return [ return [
(s["latitude"], s["longitude"]) {
for s in itertools.chain(stations.values(), airports.values()) "name": s["name"],
"type": "station",
"latitude": s["latitude"],
"longitude": s["longitude"],
}
for s in stations.values()
] + [
{
"name": s["name"],
"type": "airport",
"latitude": s["latitude"],
"longitude": s["longitude"],
}
for s in airports.values()
] ]
@ -295,13 +307,13 @@ def trip_page(start: str) -> str:
if not trip: if not trip:
flask.abort(404) flask.abort(404)
station_coordinates = collect_station_coordinates(trip) coordinates = collect_trip_coordinates(trip)
return flask.render_template( return flask.render_template(
"trip_page.html", "trip_page.html",
trip=trip, trip=trip,
today=today, today=today,
station_coordinates=station_coordinates, coordinates=coordinates,
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,
) )