diff --git a/src/osm_geojson/pt/core.py b/src/osm_geojson/pt/core.py index cef2e38..9463c8d 100644 --- a/src/osm_geojson/pt/core.py +++ b/src/osm_geojson/pt/core.py @@ -119,6 +119,27 @@ def nearest_coord_index(lon: float, lat: float, route_coords: list[Coord]) -> in return best_i +def fetch_relation_name(relation_id: int) -> str | None: + """Return the name tag of a relation, or None if unavailable. + + Uses a short timeout and swallows all errors — intended for best-effort + use in page titles where a fallback is acceptable. + """ + url = f"{OSM_API}/relation/{relation_id}.json" + try: + resp = requests.get(url, headers={"User-Agent": "osm-pt-geojson/1.0"}, timeout=10) + except requests.RequestException: + return None + if resp.status_code != 200: + return None + data: dict[str, Any] = resp.json() + for elem in data.get("elements", []): + if elem["type"] == "relation" and elem["id"] == relation_id: + name: str | None = elem.get("tags", {}).get("name") + return name + return None + + def fetch_sibling_routes(relation_id: int) -> list[dict[str, Any]]: """Return sibling route relations from the same route_master, excluding self. diff --git a/web/app.py b/web/app.py index d27babd..3ee47fa 100644 --- a/web/app.py +++ b/web/app.py @@ -9,6 +9,7 @@ from osm_geojson.pt.core import ( OsmError, build_route_coords, fetch_relation_full, + fetch_relation_name, fetch_route_master_routes, fetch_sibling_routes, make_geojson, @@ -96,7 +97,8 @@ def index() -> ResponseReturnValue: @app.route("/") def route_page(relation_id: int) -> ResponseReturnValue: """Render the page with a relation pre-loaded.""" - return render_template("index.html", relation_id=relation_id, error=None) + name = fetch_relation_name(relation_id) + return render_template("index.html", relation_id=relation_id, error=None, route_name=name) @app.route("/load", methods=["POST"]) diff --git a/web/templates/index.html b/web/templates/index.html index 96170b9..0929062 100644 --- a/web/templates/index.html +++ b/web/templates/index.html @@ -3,7 +3,8 @@ - OSM Public Transport → GeoJSON + {% if route_name %}{{ route_name }} – {% endif %}OSM Public Transport → GeoJSON + {% if route_name %}{% endif %}