49 class BusRoute: |
49 class BusRoute: |
50 def __init__(self, entry): |
50 def __init__(self, entry): |
51 self.id = entry['route_id'] |
51 self.id = entry['route_id'] |
52 self.reference = entry['route_short_name'] |
52 self.reference = entry['route_short_name'] |
53 self.trips = set() |
53 self.trips = set() |
|
54 self.service = None |
54 def __repr__(self): |
55 def __repr__(self): |
55 return 'routes[%r]' % self.reference |
56 return 'routes[%r]' % self.reference |
56 |
57 |
57 class BusService: |
58 class BusService: |
58 def __init__(self, reference): |
59 def __init__(self, reference): |
66 self.reference, self.name, self.location = reference, name, location |
67 self.reference, self.name, self.location = reference, name, location |
67 self.code = code or reference |
68 self.code = code or reference |
68 self.cluster = None |
69 self.cluster = None |
69 self.pairs = set() # samannimiset lähellä olevat pysäkit |
70 self.pairs = set() # samannimiset lähellä olevat pysäkit |
70 self.involved_trips = set() |
71 self.involved_trips = set() |
|
72 self.services = set() |
71 def __repr__(self): |
73 def __repr__(self): |
72 return 'bus_stops[%r]' % self.reference |
74 return 'bus_stops[%r]' % self.reference |
73 def schedule(self, *, max_amount = 50, arrivals = False): |
75 def schedule(self, *, max_amount = 50, arrivals = False): |
74 ''' |
76 ''' |
75 Hakee tämän pysäkin seuraavat `määrä` lähtöä. Päätepysäkille saapuvia busseja ei |
77 Hakee tämän pysäkin seuraavat `määrä` lähtöä. Päätepysäkille saapuvia busseja ei |
125 'stop': stop, |
127 'stop': stop, |
126 }) |
128 }) |
127 # Lajittele lopputulos saapumisajan mukaan. |
129 # Lajittele lopputulos saapumisajan mukaan. |
128 result.sort(key = lambda schedule_entry: schedule_entry['time']) |
130 result.sort(key = lambda schedule_entry: schedule_entry['time']) |
129 return result |
131 return result |
|
132 @property |
|
133 def typename(self): |
|
134 if self.services == {'train'}: |
|
135 return 'train-station' |
|
136 elif self.services == {'tram'}: |
|
137 return 'tram-stop' |
|
138 elif self.services == {'ferry'}: |
|
139 return 'ferry-terminal' |
|
140 else: |
|
141 return 'bus-stop' |
130 |
142 |
131 class BusHalt: |
143 class BusHalt: |
132 def __init__(self, arrival_time, departure_time, stop, trip, traveled_distance): |
144 def __init__(self, arrival_time, departure_time, stop, trip, traveled_distance): |
133 self.arrival_time, self.departure_time, self.stop, self.trip = arrival_time, departure_time, \ |
145 self.arrival_time, self.departure_time, self.stop, self.trip = arrival_time, departure_time, \ |
134 stop, trip |
146 stop, trip |
201 trip = BusTrip( |
213 trip = BusTrip( |
202 reference = row['trip_id'], |
214 reference = row['trip_id'], |
203 route = route, |
215 route = route, |
204 service = services[row['service_id']], |
216 service = services[row['service_id']], |
205 length = shape_distances.get(row.get('shape_id'), 1) * float(profile['metrics']['shape-modifier']), |
217 length = shape_distances.get(row.get('shape_id'), 1) * float(profile['metrics']['shape-modifier']), |
206 block_id = row['block_id'], |
218 block_id = row.get('block_id') or row['service_id'], |
207 ) |
219 ) |
208 route.trips.add(trip) |
220 route.trips.add(trip) |
209 if trip.name in all_trips: |
221 if trip.name in all_trips: |
210 print('Trip %s already exists' % trip.name) |
222 print('Trip %s already exists' % trip.name) |
211 else: |
223 else: |
490 |
502 |
491 global trips_by_vehicle_info |
503 global trips_by_vehicle_info |
492 trips_by_vehicle_info = {} |
504 trips_by_vehicle_info = {} |
493 for trip in all_trips.values(): |
505 for trip in all_trips.values(): |
494 trips_by_vehicle_info[(trip.block_id, trip.schedule[0].arrival_time)] = trip |
506 trips_by_vehicle_info[(trip.block_id, trip.schedule[0].arrival_time)] = trip |
|
507 if 'services' in profile and profile['services'].get('default-service'): |
|
508 for route in routes.values(): |
|
509 if not route.service: |
|
510 route.service = profile['services']['default-service'] |
|
511 for trip in route.trips: |
|
512 for halt in trip.schedule: |
|
513 halt.stop.services.add(route.service) |
495 |
514 |
496 if __name__ == '__main__': |
515 if __name__ == '__main__': |
497 profile.read('profiles/föli.ini') |
516 profile.read('profiles/föli.ini') |
498 load_buses('gtfs.zip') |
517 load_buses('gtfs.zip') |
499 import busroute |
518 import busroute |