From 12ecc97b07093f3cac6567c70ff75d57b429c674 Mon Sep 17 00:00:00 2001 From: Ariel Costas Guerrero Date: Tue, 21 Oct 2025 17:38:01 +0200 Subject: Implement new Santiago region (WIP) --- data/download-stops.py | 126 --------------------- data/overrides/amenities.yaml | 23 ---- data/overrides/fix-gregorio-espino.yaml | 20 ---- data/overrides/hide-virtual-stops.yaml | 17 --- data/overrides/improve-coordinates-misc.yaml | 35 ------ data/santiago/download-stops.py | 132 ++++++++++++++++++++++ data/santiago/overrides/.gitkeep | 0 data/stop-overrides.yaml | 30 ----- data/vigo/download-stops.py | 126 +++++++++++++++++++++ data/vigo/overrides/amenities.yaml | 23 ++++ data/vigo/overrides/fix-gregorio-espino.yaml | 20 ++++ data/vigo/overrides/hide-virtual-stops.yaml | 17 +++ data/vigo/overrides/improve-coordinates-misc.yaml | 35 ++++++ 13 files changed, 353 insertions(+), 251 deletions(-) delete mode 100644 data/download-stops.py delete mode 100644 data/overrides/amenities.yaml delete mode 100644 data/overrides/fix-gregorio-espino.yaml delete mode 100644 data/overrides/hide-virtual-stops.yaml delete mode 100644 data/overrides/improve-coordinates-misc.yaml create mode 100644 data/santiago/download-stops.py create mode 100644 data/santiago/overrides/.gitkeep delete mode 100644 data/stop-overrides.yaml create mode 100644 data/vigo/download-stops.py create mode 100644 data/vigo/overrides/amenities.yaml create mode 100644 data/vigo/overrides/fix-gregorio-espino.yaml create mode 100644 data/vigo/overrides/hide-virtual-stops.yaml create mode 100644 data/vigo/overrides/improve-coordinates-misc.yaml (limited to 'data') diff --git a/data/download-stops.py b/data/download-stops.py deleted file mode 100644 index b77eae5..0000000 --- a/data/download-stops.py +++ /dev/null @@ -1,126 +0,0 @@ -# /// script -# requires-python = ">=3.12" -# dependencies = [ -# "PyYAML>=6.0.2", # For YAML support -# ] -# /// -import json -import os -import sys -import urllib.request -import yaml # Add YAML support for overrides - -OVERRIDES_DIR = "overrides" -OUTPUT_FILE = "../src/frontend/public/stops.json" - -def load_stop_overrides(file_path): - """Load stop overrides from a YAML file""" - if not os.path.exists(file_path): - print(f"Warning: Overrides file {file_path} not found") - return {} - - try: - with open(file_path, 'r', encoding='utf-8') as f: - overrides = yaml.safe_load(f) - print(f"Loaded {len(overrides) if overrides else 0} stop overrides") - return overrides or {} - except Exception as e: - print(f"Error loading overrides: {e}", file=sys.stderr) - return {} - -def apply_overrides(stops, overrides): - """Apply overrides to the stop data""" - for stop in stops: - stop_id = stop.get("stopId") - if stop_id in overrides: - override = overrides[stop_id] - - # Apply or add alternate names - if "alternateNames" in override: - for key, value in override["alternateNames"].items(): - stop["name"][key] = value - - # Apply location override - if "location" in override: - if "latitude" in override["location"]: - stop["latitude"] = override["location"]["latitude"] - if "longitude" in override["location"]: - stop["longitude"] = override["location"]["longitude"] - - # Add amenities - if "amenities" in override: - stop["amenities"] = override["amenities"] - - # Mark stop as hidden if needed - if "hide" in override: - stop["hide"] = override["hide"] - - return stops - -def main(): - print("Fetching stop list data...") - - # Download stop list data - url = "https://datos.vigo.org/vci_api_app/api2.jsp?tipo=TRANSPORTE_PARADAS" - req = urllib.request.Request(url) - - try: - with urllib.request.urlopen(req) as response: - # Read the response and decode from ISO-8859-1 to UTF-8 - content = response.read().decode('iso-8859-1') - data = json.loads(content) - - print(f"Downloaded {len(data)} stops") - - # Process the data - processed_stops = [] - for stop in data: - name = stop.get("nombre", "").strip() - # Fix double space equals comma-space: "Castrelos 202" -> "Castrelos, 202"; and remove quotes - name = name.replace(" ", ", ").replace('"', '').replace("'", "") - - processed_stop = { - "stopId": stop.get("id"), - "name": { - "original": name - }, - "latitude": stop.get("lat"), - "longitude": stop.get("lon"), - "lines": [line.strip() for line in stop.get("lineas", "").split(",")] if stop.get("lineas") else [] - } - processed_stops.append(processed_stop) - - # Load and apply overrides - script_dir = os.path.dirname(os.path.abspath(__file__)) - overrides_dir = os.path.join(script_dir, OVERRIDES_DIR) - # For each YML/YAML file in the overrides directory, load and apply the overrides - for filename in os.listdir(overrides_dir): - if not filename.endswith(".yml") and not filename.endswith(".yaml"): - continue - - print(f"Loading overrides from {filename}") - overrides_file = os.path.join(overrides_dir, filename) - overrides = load_stop_overrides(overrides_file) - processed_stops = apply_overrides(processed_stops, overrides) - - # Filter out hidden stops - visible_stops = [stop for stop in processed_stops if not stop.get("hide")] - print(f"Removed {len(processed_stops) - len(visible_stops)} hidden stops") - - # Sort stops by ID ascending - visible_stops.sort(key=lambda x: x["stopId"]) - - output_file = os.path.join(script_dir, OUTPUT_FILE) - - with open(output_file, 'w', encoding='utf-8') as f: - json.dump(visible_stops, f, ensure_ascii=False, indent=2) - - print(f"Saved processed stops data to {output_file}") - return 0 - - except Exception as e: - print(f"Error processing stops data: {e}", file=sys.stderr) - return 1 - -if __name__ == "__main__": - sys.exit(main()) diff --git a/data/overrides/amenities.yaml b/data/overrides/amenities.yaml deleted file mode 100644 index fa2067a..0000000 --- a/data/overrides/amenities.yaml +++ /dev/null @@ -1,23 +0,0 @@ -5520: # García Barbón, 7 - amenities: - - shelter - - display -5530: # García Barbón, 18 - amenities: - - shelter - - display -6620: #Policarpo Sanz, 40 - amenities: - - shelter - - display -14264: # Urzáiz - Príncipe - amenities: - - shelter - - display -20193: # Policarpo Sanz, 25 - amenities: - - shelter - - display -20198: # Policarpo Sanz, 26 - amenities: - - shelter diff --git a/data/overrides/fix-gregorio-espino.yaml b/data/overrides/fix-gregorio-espino.yaml deleted file mode 100644 index 2e035a2..0000000 --- a/data/overrides/fix-gregorio-espino.yaml +++ /dev/null @@ -1,20 +0,0 @@ -# Fix the position of the stops in Gregorio Espino, which are "opposite" to the actual location of the bus stops. -5720: # Gregorio Espino, 33 - location: - latitude: 42.23004933454558 - longitude: -8.706947409683313 - -5710: # Gregorio Espino, 22 - location: - latitude: 42.23003666347398 - longitude: -8.707266671978003 - -5730: # Gregorio Espino, 44 - location: - latitude: 42.227850036119314 - longitude: -8.708105429626789 - -5740: # Gregorio Espino, 57 - location: - latitude: 42.22783722597372 - longitude: -8.707849091551859 \ No newline at end of file diff --git a/data/overrides/hide-virtual-stops.yaml b/data/overrides/hide-virtual-stops.yaml deleted file mode 100644 index a2bf0b1..0000000 --- a/data/overrides/hide-virtual-stops.yaml +++ /dev/null @@ -1,17 +0,0 @@ -# The Vitrasa network has several virtual stops created for internal purposes, like -# end of certain lines with a "nice" name. - -20223: # Castrelos (Pavillón) - Final U1 - hide: true -20146: # García Barbón 7 - final líneas A y 18A - hide: true -20220: # (Samil) COIA-SAMIL - Final L15A - hide: true -20001: # (Samil) Samil por Beiramar - Final L15B - hide: true -20002: # (Samil) Samil por Torrecedeira - Final L15C - hide: true -20144: # (Samil) Samil por Coia - Final C3D+C3i - hide: true -20145: # (Samil) Samil por Bouzas - Final C3D+C3i - hide: true \ No newline at end of file diff --git a/data/overrides/improve-coordinates-misc.yaml b/data/overrides/improve-coordinates-misc.yaml deleted file mode 100644 index 922f103..0000000 --- a/data/overrides/improve-coordinates-misc.yaml +++ /dev/null @@ -1,35 +0,0 @@ -# Improves coordinates for some locations in the dataset to be more accurate, and avoid clustering -6620: # Policarpo Sanz, 40 - location: - latitude: 42.23757846151978 - longitude: -8.721031378896738 - -20193: # Policarpo Sanz, 25 - location: - latitude: 42.23767601188501 - longitude: -8.721582630122455 - -3130: #Avda. de Cesáreo Vázquez 169 - location: - latitude: 42.191024803868736 - longitude: -8.799397387002196 - -3090: # Avda. de Cesáreo Vázquez 182 - location: - latitude: 42.191019711713736 - longitude: -8.799628565094565 - -14294: # Avda. de Ricardo Mella 406 - location: - latitude: 42.190684424876565 - longitude: -8.799308812770041 - -3120: # Cesáreo Vázquez 141 - location: - latitude: 42.187488521491225 - longitude: -8.801226626055183 - -3080: # Cesáreo Vázquez 136 - location: - latitude: 42.1873653089623 - longitude: -8.800886236766305 diff --git a/data/santiago/download-stops.py b/data/santiago/download-stops.py new file mode 100644 index 0000000..f673be5 --- /dev/null +++ b/data/santiago/download-stops.py @@ -0,0 +1,132 @@ +# /// script +# requires-python = ">=3.12" +# dependencies = [ +# "PyYAML>=6.0.2", # For YAML support +# ] +# /// +import json +import os +import sys +import urllib.request +import yaml # Add YAML support for overrides + +OVERRIDES_DIR = "overrides" +OUTPUT_FILE = "../../src/frontend/public/stops/santiago.json" + +def load_stop_overrides(file_path): + """Load stop overrides from a YAML file""" + if not os.path.exists(file_path): + print(f"Warning: Overrides file {file_path} not found") + return {} + + try: + with open(file_path, 'r', encoding='utf-8') as f: + overrides = yaml.safe_load(f) + print(f"Loaded {len(overrides) if overrides else 0} stop overrides") + return overrides or {} + except Exception as e: + print(f"Error loading overrides: {e}", file=sys.stderr) + return {} + +def apply_overrides(stops, overrides): + """Apply overrides to the stop data""" + for stop in stops: + stop_id = stop.get("stopId") + if stop_id in overrides: + override = overrides[stop_id] + + # Apply or add alternate names + if "alternateNames" in override: + for key, value in override["alternateNames"].items(): + stop["name"][key] = value + + # Apply location override + if "location" in override: + if "latitude" in override["location"]: + stop["latitude"] = override["location"]["latitude"] + if "longitude" in override["location"]: + stop["longitude"] = override["location"]["longitude"] + + # Add amenities + if "amenities" in override: + stop["amenities"] = override["amenities"] + + # Mark stop as hidden if needed + if "hide" in override: + stop["hide"] = override["hide"] + + return stops + +def main(): + print("Fetching stop list data...") + + # Download stop list data + url = "https://app.tussa.org/tussa/api/paradas" + body = json.dumps({ "nombre": "" }).encode('utf-8') + req = urllib.request.Request(url, data=body, headers={'Content-Type': 'application/json'}, method='POST') + + try: + with urllib.request.urlopen(req) as response: + content = response.read().decode('utf-8') + data = json.loads(content) + + print(f"Downloaded {len(data)} stops") + + # Process the data + processed_stops = [] + for stop in data: + name = stop.get("nombre", "").strip() + + lines_sinoptic: list[str] = [line.get("sinoptico").strip() for line in stop.get("lineas", [])] if stop.get("lineas") else [] + lines_id: list[str] = [] + + for line in lines_sinoptic: + line_code = line.lstrip('L') + lines_id.append(line_code) + + processed_stop = { + "stopId": stop.get("id"), + "name": { + "original": name + }, + "latitude": stop.get("coordenadas").get("latitud"), + "longitude": stop.get("coordenadas").get("longitud"), + "lines": lines_id, + "hide": len(lines_id) == 0 + } + processed_stops.append(processed_stop) + + # Load and apply overrides + script_dir = os.path.dirname(os.path.abspath(__file__)) + overrides_dir = os.path.join(script_dir, OVERRIDES_DIR) + # For each YML/YAML file in the overrides directory, load and apply the overrides + for filename in os.listdir(overrides_dir): + if not filename.endswith(".yml") and not filename.endswith(".yaml"): + continue + + print(f"Loading overrides from {filename}") + overrides_file = os.path.join(overrides_dir, filename) + overrides = load_stop_overrides(overrides_file) + processed_stops = apply_overrides(processed_stops, overrides) + + # Filter out hidden stops + visible_stops = [stop for stop in processed_stops if not stop.get("hide")] + print(f"Removed {len(processed_stops) - len(visible_stops)} hidden stops") + + # Sort stops by ID ascending + visible_stops.sort(key=lambda x: x["stopId"]) + + output_file = os.path.join(script_dir, OUTPUT_FILE) + + with open(output_file, 'w', encoding='utf-8') as f: + json.dump(visible_stops, f, ensure_ascii=False, indent=2) + + print(f"Saved processed stops data to {output_file}") + return 0 + + except Exception as e: + print(f"Error processing stops data: {e}", file=sys.stderr) + return 1 + +if __name__ == "__main__": + sys.exit(main()) diff --git a/data/santiago/overrides/.gitkeep b/data/santiago/overrides/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/data/stop-overrides.yaml b/data/stop-overrides.yaml deleted file mode 100644 index 1a5674c..0000000 --- a/data/stop-overrides.yaml +++ /dev/null @@ -1,30 +0,0 @@ -# This file contains overrides for specific bus stops. -# Format: -# stopId: # Numeric ID of the stop to override -# name: # Override the name -# alternateNames: # Additional names for the stop -# metro: # Stop name in -# location: # Override location coordinates -# latitude: # New latitude value -# longitude: # New longitude value -# hide: # Hide the stop from the map and list -# amenities: # List of amenities available at this stop -# - shelter # Marquesina -# - real-time display # Display with real-time information - -#6930: # Praza de América 1 -# alternateNames: - -# intersect: "Praza América - Camelias" -# amenities: -# - shelter -# - real-time display - -#14264: # Urzáiz - Príncipe -# alternateNames: -# intersect: "Urzáiz - Príncipe" -# amenities: -# - shelter -# - real-time display - - diff --git a/data/vigo/download-stops.py b/data/vigo/download-stops.py new file mode 100644 index 0000000..a57d30f --- /dev/null +++ b/data/vigo/download-stops.py @@ -0,0 +1,126 @@ +# /// script +# requires-python = ">=3.12" +# dependencies = [ +# "PyYAML>=6.0.2", # For YAML support +# ] +# /// +import json +import os +import sys +import urllib.request +import yaml # Add YAML support for overrides + +OVERRIDES_DIR = "overrides" +OUTPUT_FILE = "../../src/frontend/public/stops/vigo.json" + +def load_stop_overrides(file_path): + """Load stop overrides from a YAML file""" + if not os.path.exists(file_path): + print(f"Warning: Overrides file {file_path} not found") + return {} + + try: + with open(file_path, 'r', encoding='utf-8') as f: + overrides = yaml.safe_load(f) + print(f"Loaded {len(overrides) if overrides else 0} stop overrides") + return overrides or {} + except Exception as e: + print(f"Error loading overrides: {e}", file=sys.stderr) + return {} + +def apply_overrides(stops, overrides): + """Apply overrides to the stop data""" + for stop in stops: + stop_id = stop.get("stopId") + if stop_id in overrides: + override = overrides[stop_id] + + # Apply or add alternate names + if "alternateNames" in override: + for key, value in override["alternateNames"].items(): + stop["name"][key] = value + + # Apply location override + if "location" in override: + if "latitude" in override["location"]: + stop["latitude"] = override["location"]["latitude"] + if "longitude" in override["location"]: + stop["longitude"] = override["location"]["longitude"] + + # Add amenities + if "amenities" in override: + stop["amenities"] = override["amenities"] + + # Mark stop as hidden if needed + if "hide" in override: + stop["hide"] = override["hide"] + + return stops + +def main(): + print("Fetching stop list data...") + + # Download stop list data + url = "https://datos.vigo.org/vci_api_app/api2.jsp?tipo=TRANSPORTE_PARADAS" + req = urllib.request.Request(url) + + try: + with urllib.request.urlopen(req) as response: + # Read the response and decode from ISO-8859-1 to UTF-8 + content = response.read().decode('iso-8859-1') + data = json.loads(content) + + print(f"Downloaded {len(data)} stops") + + # Process the data + processed_stops = [] + for stop in data: + name = stop.get("nombre", "").strip() + # Fix double space equals comma-space: "Castrelos 202" -> "Castrelos, 202"; and remove quotes + name = name.replace(" ", ", ").replace('"', '').replace("'", "") + + processed_stop = { + "stopId": stop.get("id"), + "name": { + "original": name + }, + "latitude": stop.get("lat"), + "longitude": stop.get("lon"), + "lines": [line.strip() for line in stop.get("lineas", "").split(",")] if stop.get("lineas") else [] + } + processed_stops.append(processed_stop) + + # Load and apply overrides + script_dir = os.path.dirname(os.path.abspath(__file__)) + overrides_dir = os.path.join(script_dir, OVERRIDES_DIR) + # For each YML/YAML file in the overrides directory, load and apply the overrides + for filename in os.listdir(overrides_dir): + if not filename.endswith(".yml") and not filename.endswith(".yaml"): + continue + + print(f"Loading overrides from {filename}") + overrides_file = os.path.join(overrides_dir, filename) + overrides = load_stop_overrides(overrides_file) + processed_stops = apply_overrides(processed_stops, overrides) + + # Filter out hidden stops + visible_stops = [stop for stop in processed_stops if not stop.get("hide")] + print(f"Removed {len(processed_stops) - len(visible_stops)} hidden stops") + + # Sort stops by ID ascending + visible_stops.sort(key=lambda x: x["stopId"]) + + output_file = os.path.join(script_dir, OUTPUT_FILE) + + with open(output_file, 'w', encoding='utf-8') as f: + json.dump(visible_stops, f, ensure_ascii=False, indent=2) + + print(f"Saved processed stops data to {output_file}") + return 0 + + except Exception as e: + print(f"Error processing stops data: {e}", file=sys.stderr) + return 1 + +if __name__ == "__main__": + sys.exit(main()) diff --git a/data/vigo/overrides/amenities.yaml b/data/vigo/overrides/amenities.yaml new file mode 100644 index 0000000..fa2067a --- /dev/null +++ b/data/vigo/overrides/amenities.yaml @@ -0,0 +1,23 @@ +5520: # García Barbón, 7 + amenities: + - shelter + - display +5530: # García Barbón, 18 + amenities: + - shelter + - display +6620: #Policarpo Sanz, 40 + amenities: + - shelter + - display +14264: # Urzáiz - Príncipe + amenities: + - shelter + - display +20193: # Policarpo Sanz, 25 + amenities: + - shelter + - display +20198: # Policarpo Sanz, 26 + amenities: + - shelter diff --git a/data/vigo/overrides/fix-gregorio-espino.yaml b/data/vigo/overrides/fix-gregorio-espino.yaml new file mode 100644 index 0000000..2e035a2 --- /dev/null +++ b/data/vigo/overrides/fix-gregorio-espino.yaml @@ -0,0 +1,20 @@ +# Fix the position of the stops in Gregorio Espino, which are "opposite" to the actual location of the bus stops. +5720: # Gregorio Espino, 33 + location: + latitude: 42.23004933454558 + longitude: -8.706947409683313 + +5710: # Gregorio Espino, 22 + location: + latitude: 42.23003666347398 + longitude: -8.707266671978003 + +5730: # Gregorio Espino, 44 + location: + latitude: 42.227850036119314 + longitude: -8.708105429626789 + +5740: # Gregorio Espino, 57 + location: + latitude: 42.22783722597372 + longitude: -8.707849091551859 \ No newline at end of file diff --git a/data/vigo/overrides/hide-virtual-stops.yaml b/data/vigo/overrides/hide-virtual-stops.yaml new file mode 100644 index 0000000..a2bf0b1 --- /dev/null +++ b/data/vigo/overrides/hide-virtual-stops.yaml @@ -0,0 +1,17 @@ +# The Vitrasa network has several virtual stops created for internal purposes, like +# end of certain lines with a "nice" name. + +20223: # Castrelos (Pavillón) - Final U1 + hide: true +20146: # García Barbón 7 - final líneas A y 18A + hide: true +20220: # (Samil) COIA-SAMIL - Final L15A + hide: true +20001: # (Samil) Samil por Beiramar - Final L15B + hide: true +20002: # (Samil) Samil por Torrecedeira - Final L15C + hide: true +20144: # (Samil) Samil por Coia - Final C3D+C3i + hide: true +20145: # (Samil) Samil por Bouzas - Final C3D+C3i + hide: true \ No newline at end of file diff --git a/data/vigo/overrides/improve-coordinates-misc.yaml b/data/vigo/overrides/improve-coordinates-misc.yaml new file mode 100644 index 0000000..922f103 --- /dev/null +++ b/data/vigo/overrides/improve-coordinates-misc.yaml @@ -0,0 +1,35 @@ +# Improves coordinates for some locations in the dataset to be more accurate, and avoid clustering +6620: # Policarpo Sanz, 40 + location: + latitude: 42.23757846151978 + longitude: -8.721031378896738 + +20193: # Policarpo Sanz, 25 + location: + latitude: 42.23767601188501 + longitude: -8.721582630122455 + +3130: #Avda. de Cesáreo Vázquez 169 + location: + latitude: 42.191024803868736 + longitude: -8.799397387002196 + +3090: # Avda. de Cesáreo Vázquez 182 + location: + latitude: 42.191019711713736 + longitude: -8.799628565094565 + +14294: # Avda. de Ricardo Mella 406 + location: + latitude: 42.190684424876565 + longitude: -8.799308812770041 + +3120: # Cesáreo Vázquez 141 + location: + latitude: 42.187488521491225 + longitude: -8.801226626055183 + +3080: # Cesáreo Vázquez 136 + location: + latitude: 42.1873653089623 + longitude: -8.800886236766305 -- cgit v1.3