From e27fdd8c2ded61f3306e4c9fd93de43e03e4e17f Mon Sep 17 00:00:00 2001 From: Alexey Zakharenkov Date: Tue, 18 Oct 2022 17:39:42 +0300 Subject: [PATCH] Add to GTFS only transfers in which both stops are part of routes --- processors/gtfs.py | 15 +++++---- tests/test_gtfs_processor.py | 60 ++++++++++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+), 6 deletions(-) create mode 100644 tests/test_gtfs_processor.py diff --git a/processors/gtfs.py b/processors/gtfs.py index a5186fe..1ae5ee7 100644 --- a/processors/gtfs.py +++ b/processors/gtfs.py @@ -2,6 +2,8 @@ import csv import io import zipfile +from itertools import permutations + from ._common import ( DEFAULT_INTERVAL, format_colour, @@ -346,19 +348,20 @@ def process(cities, transfers, filename, cache_path): for stoparea1 in stoparea_set: for stoparea2 in stoparea_set: if stoparea1.id < stoparea2.id: + stop1_id = f"{stoparea1.id}_st" + stop2_id = f"{stoparea2.id}_st" + if not {stop1_id, stop2_id}.issubset(all_stops): + continue transfer_time = TRANSFER_PENALTY + round( distance(stoparea1.center, stoparea2.center) / SPEED_ON_TRANSFER ) - for id1, id2 in ( - (stoparea1.id, stoparea2.id), - (stoparea2.id, stoparea1.id), - ): + for id1, id2 in permutations((stop1_id, stop2_id)): gtfs_data["transfers"].append( dict_to_row( { - "from_stop_id": f"{id1}_st", - "to_stop_id": f"{id2}_st", + "from_stop_id": id1, + "to_stop_id": id2, "transfer_type": 0, "min_transfer_time": transfer_time, }, diff --git a/tests/test_gtfs_processor.py b/tests/test_gtfs_processor.py new file mode 100644 index 0000000..df0e2b1 --- /dev/null +++ b/tests/test_gtfs_processor.py @@ -0,0 +1,60 @@ +import unittest + +from processors.gtfs import ( + dict_to_row, + GTFS_COLUMNS, +) + + +class TestGTFS(unittest.TestCase): + """Test processors/gtfs.py""" + + def test_dict_to_row(self): + """Test that absent or None values in a GTFS feature item + are converted by dict_to_row() function to empty strings + in right amount. + """ + + if GTFS_COLUMNS["trips"][:3] != ["route_id", "service_id", "trip_id"]: + raise RuntimeError("GTFS column names/order inconsistency") + + test_trips = [ + { + "description": "Absent keys", + "trip_data": { + "route_id": 1, + "service_id": "a", + "trip_id": "tr_123", + }, + }, + { + "description": "None or absent keys", + "trip_data": { + "route_id": 1, + "service_id": "a", + "trip_id": "tr_123", + "trip_headsign": None, + "trip_short_name": None, + "route_pattern_id": None, + }, + }, + { + "description": "None, empty-string or absent keys", + "trip_data": { + "route_id": 1, + "service_id": "a", + "trip_id": "tr_123", + "trip_headsign": "", + "trip_short_name": "", + "route_pattern_id": None, + }, + }, + ] + + answer = [1, "a", "tr_123"] + [""] * (len(GTFS_COLUMNS["trips"]) - 3) + + for test_trip in test_trips: + with self.subTest(msg=test_trip["description"]): + self.assertEqual( + dict_to_row(test_trip["trip_data"], "trips"), answer + )