diff options
| -rw-r--r-- | build_renfe/build_static_feed.py | 4 | ||||
| -rw-r--r-- | build_xunta/build_static_feed.py | 39 |
2 files changed, 37 insertions, 6 deletions
diff --git a/build_renfe/build_static_feed.py b/build_renfe/build_static_feed.py index 6c12c1f..63ba90e 100644 --- a/build_renfe/build_static_feed.py +++ b/build_renfe/build_static_feed.py @@ -457,8 +457,6 @@ if __name__ == "__main__": stop_times_by_trip[tid] = [] stop_times_by_trip[tid].append(st) - shapes_file = open("shapes_debug.txt", "w", encoding="utf-8") - for trip_id in tqdm(trip_ids, total=shape_ids_total, desc="Generating shapes"): shape_id = get_shape_id(trip_id) if shape_id in shape_ids_generated: @@ -488,8 +486,6 @@ if __name__ == "__main__": coords_str = ";".join(coordinates) osrm_url = f"{OSRM_BASE_URL}{coords_str}?overview=full&geometries=geojson&continue_straight=false" - shapes_file.write(f"{trip_id} ({shape_id}): {osrm_url}\n") - try: response = requests.get(osrm_url, timeout=10) if response.status_code == 200: diff --git a/build_xunta/build_static_feed.py b/build_xunta/build_static_feed.py index 9c2a3b1..4f1a823 100644 --- a/build_xunta/build_static_feed.py +++ b/build_xunta/build_static_feed.py @@ -170,8 +170,7 @@ if __name__ == "__main__": TRIPS_FILE = os.path.join(INPUT_GTFS_PATH, "trips.txt") # Copy unchanged files - for filename in ["trips.txt", - "calendar.txt", "calendar_dates.txt", + for filename in ["calendar.txt", "calendar_dates.txt", "shapes.txt"]: src = os.path.join(INPUT_GTFS_PATH, filename) if os.path.exists(src): @@ -206,6 +205,23 @@ if __name__ == "__main__": routes = list(reader) route_fieldnames = set(reader.fieldnames or routes[0].keys()) + # Drop routes with ID ending in 12 (return, duplicates of the same route with ID ending in 11) + original_count = len(routes) + routes_2 = [] + route_ids_set = set() + for r in routes: + neutralised_id = r["route_id"][:-2] + if neutralised_id not in route_ids_set: + r["route_id"] = neutralised_id + routes_2.append(r) + route_ids_set.add(neutralised_id) + + routes = routes_2 + dropped_count = original_count - len(routes) + if dropped_count: + logging.info("Dropped %d routes with route_id ending in '12' (return duplicates).", dropped_count) + + for route in routes: short_name = route.get("route_short_name", "") agency_key = short_name[:5] if len(short_name) >= 5 else "" @@ -231,6 +247,25 @@ if __name__ == "__main__": writer.writeheader() writer.writerows(routes) + # Trips: update route_id to remove the last character + with open(os.path.join(INPUT_GTFS_PATH, "trips.txt"), encoding="utf-8-sig", newline="") as trips_fh: + reader = csv.DictReader(trips_fh) + trips = list(reader) + trip_fieldnames = list(reader.fieldnames or trips[0].keys()) + + for trip in trips: + # convert direction_id from 11/12 to 0/1 (11 => ida, 12 => vuelta) + trip_route_last_char = trip["route_id"][-1] if trip["route_id"] else "" + trip["direction_id"] = "0" if trip_route_last_char == "1" else "1" + trip["route_id"] = trip["route_id"][:-2] # remove last two character (11/12 suffix) + # Ideally we'd remove only one, but at the time of writing there are TWO routes that have variants '11', '12', '21' and '22' + + with open(os.path.join(OUTPUT_GTFS_PATH, "trips.txt"), "w", encoding="utf-8", newline="") as trips_out: + writer = csv.DictWriter(trips_out, fieldnames=trip_fieldnames, extrasaction="ignore") + writer.writeheader() + writer.writerows(trips) + + # Build stops.txt with stop_desc logging.info("Enriching stops with parish/municipality descriptions …") with open(STOPS_FILE, encoding="utf-8-sig", newline="") as in_fh: |
