From 5cc0b1d520849e93b5dbbd0c71515b9cfad8ce8b Mon Sep 17 00:00:00 2001 From: Francia Csaba Date: Fri, 12 Jan 2024 15:40:24 +0100 Subject: [PATCH] #21 improved grouping algorithm --- .../plugins/enhancer/services/gtfs_export.py | 23 +++++++++++-------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/amarillo/plugins/enhancer/services/gtfs_export.py b/amarillo/plugins/enhancer/services/gtfs_export.py index b792c61..0a3f83b 100644 --- a/amarillo/plugins/enhancer/services/gtfs_export.py +++ b/amarillo/plugins/enhancer/services/gtfs_export.py @@ -86,17 +86,21 @@ class GtfsExport: ungrouped_trips = dict(trips) route_groups = list() current_route_id = 1 - for trip_id, trip in trips.items(): - if len(ungrouped_trips) == 0: break - #find trips whose start and end stops are within a specified distance of the current trip - group = {key: value for key, value in ungrouped_trips.items() if self.trips_are_close(trip, value)} + while len(ungrouped_trips) > 0: + trip_id, current_trip = ungrouped_trips.popitem() - route_groups.append(group) - for key, grouped_trip in group.items(): - grouped_trip.route_id = str(current_route_id) - ungrouped_trips.pop(key) + current_group = {trip_id: current_trip} + current_trip.route_id = current_route_id + for other_id, other_trip in list(ungrouped_trips.items()): + # if an ungrouped trip is close to any of the grouped trips, add it to the route group + if (any(self.trips_are_close(other_trip, grouped_trip) for grouped_trip in current_group.values())): + current_group[other_id] = ungrouped_trips.pop(other_id) + current_group[other_id].route_id = current_route_id + + + route_groups.append(current_group) current_route_id += 1 return route_groups, trips @@ -108,7 +112,8 @@ class GtfsExport: trip2_start = trip2.path.coordinates[0] trip2_end = trip2.path.coordinates[-1] - return self.within_range(trip1_start, trip2_start) and self.within_range(trip1_end, trip2_end) + res = self.within_range(trip1_start, trip2_start) and self.within_range(trip1_end, trip2_end) + return res def within_range(self, stop1, stop2): MERGE_RANGE_M = 500