Show rail routes using GeoJSON

This commit is contained in:
Edward Betts 2024-01-12 22:29:10 +00:00
parent 4e719a07ab
commit e2fdd1d198
2 changed files with 46 additions and 8 deletions

View file

@ -124,10 +124,20 @@ coordinates.forEach(function(item) {
marker.bindPopup(item.name); marker.bindPopup(item.name);
}); });
// Draw lines for routes // Draw routes
routes.forEach(function(route) { routes.forEach(function(route) {
var color = route[0] === "train" ? "green" : "red"; // Green for trains, red for flights if (route.geojson) {
L.polyline([route[1], route[2]], {color: color}).addTo(map); // If route is defined as GeoJSON
L.geoJSON(JSON.parse(route.geojson), {
style: function(feature) {
return {color: route.type === "train" ? "green" : "blue"}; // Green for trains, blue for flights
}
}).addTo(map);
} else {
// If route is defined by 'from' and 'to' coordinates
var color = route.type === "train" ? "green" : "red"; // Green for trains, red for flights
L.polyline([route.from, route.to], {color: color}).addTo(map);
}
}); });
</script> </script>

View file

@ -301,23 +301,51 @@ def latlon_tuple(stop: StrDict) -> tuple[float, float]:
return (stop["latitude"], stop["longitude"]) return (stop["latitude"], stop["longitude"])
def get_trip_routes( def read_geojson(filename: str) -> str:
trip: Trip, data_dir = app.config["PERSONAL_DATA"]
) -> list[tuple[str, tuple[float, float], tuple[float, float]]]: return open(os.path.join(data_dir, "train_routes", filename + ".geojson")).read()
def get_trip_routes(trip: Trip) -> list[StrDict]:
routes = [] routes = []
seen_geojson = set()
for t in trip.travel: for t in trip.travel:
if t["type"] == "flight": if t["type"] == "flight":
if "from_airport" not in t or "to_airport" not in t: if "from_airport" not in t or "to_airport" not in t:
continue continue
fly_from, fly_to = t["from_airport"], t["to_airport"] fly_from, fly_to = t["from_airport"], t["to_airport"]
routes.append(("flight", latlon_tuple(fly_from), latlon_tuple(fly_to))) routes.append(
{
"type": "flight",
"from": latlon_tuple(fly_from),
"to": latlon_tuple(fly_to),
}
)
else: else:
assert t["type"] == "train" assert t["type"] == "train"
for leg in t["legs"]: for leg in t["legs"]:
train_from, train_to = leg["from_station"], leg["to_station"] train_from, train_to = leg["from_station"], leg["to_station"]
geojson_filename = train_from.get("routes", {}).get(train_to["uic"])
if not geojson_filename:
routes.append( routes.append(
("train", latlon_tuple(train_from), latlon_tuple(train_to)) {
"type": "train",
"from": latlon_tuple(train_from),
"to": latlon_tuple(train_to),
}
)
continue
if geojson_filename in seen_geojson:
continue
seen_geojson.add(geojson_filename)
routes.append(
{
"type": "train",
"geojson": read_geojson(geojson_filename),
}
) )
return routes return routes