183 all_clusters = set() |
183 all_clusters = set() |
184 viimeinen_käyttöpäivä = None |
184 viimeinen_käyttöpäivä = None |
185 clusters_by_name = {} |
185 clusters_by_name = {} |
186 services_for_day = {} |
186 services_for_day = {} |
187 |
187 |
188 def load_buses(gtfs_zip_path, regions): |
188 def load_buses(gtfs_zip_path): |
189 global viimeinen_käyttöpäivä |
189 global viimeinen_käyttöpäivä |
190 from zipfile import ZipFile |
190 from zipfile import ZipFile |
191 with ZipFile(gtfs_zip_path) as gtfs_zip: |
191 with ZipFile(gtfs_zip_path) as gtfs_zip: |
192 print('Loading routes... ', file = stderr, end = '', flush = True) |
192 print('Loading routes... ', file = stderr, end = '', flush = True) |
193 with gtfs_zip.open('routes.txt') as file: |
193 with gtfs_zip.open('routes.txt') as file: |
194 for row in read_csv(map(bytes.decode, file)): |
194 for row in read_csv(map(bytes.decode, file)): |
195 route = BusRoute(row) |
195 route = BusRoute(row) |
196 routes[route.reference] = route |
196 routes[route.reference] = route |
197 routes_per_id[route.id] = route |
197 routes_per_id[route.id] = route |
198 print('%d routes' % len(routes), file = stderr) |
198 print('%d routes' % len(routes), file = stderr) |
|
199 # Add services |
|
200 import re |
|
201 service_patterns = {} |
|
202 if 'service-patterns' in profile: |
|
203 for service_type, regexps in profile['service-patterns'].items(): |
|
204 service_patterns[service_type] = {re.compile(regexp) for regexp in regexps.split('@')} |
|
205 if 'services' in profile and profile['services'].get('default-service'): |
|
206 print('Tagging services...', end = '') |
|
207 for route in routes.values(): |
|
208 for service_type, regexps in service_patterns.items(): |
|
209 if any(regexp.match(route.reference) for regexp in regexps): |
|
210 route.service = service_type |
|
211 break |
|
212 else: |
|
213 route.service = profile['services']['default-service'] |
|
214 print('') |
199 print('Loading trips... ', file = stderr, end = '', flush = True) |
215 print('Loading trips... ', file = stderr, end = '', flush = True) |
200 shape_distances = {} |
216 shape_distances = {} |
201 try: |
217 try: |
202 with gtfs_zip.open('shapes.txt') as file: |
218 with gtfs_zip.open('shapes.txt') as file: |
203 for row in read_csv(map(bytes.decode, file)): |
219 for row in read_csv(map(bytes.decode, file)): |
229 |
245 |
230 def read_time(teksti): |
246 def read_time(teksti): |
231 hour, minute, second = map(int, teksti.split(':')) |
247 hour, minute, second = map(int, teksti.split(':')) |
232 return timedelta(hours = hour, minutes = minute, seconds = second) |
248 return timedelta(hours = hour, minutes = minute, seconds = second) |
233 |
249 |
234 print('Ladataan päiväykset... ', file = stderr, flush = True) |
250 print('Loading dates... ', file = stderr, flush = True) |
235 |
|
236 viimeinen_käyttöpäivä = date.today() |
251 viimeinen_käyttöpäivä = date.today() |
237 |
252 |
238 def date_range(start_date, end_date, *, include_end = False): |
253 def date_range(start_date, end_date, *, include_end = False): |
239 ''' Generates date from start_date to end_date. If include_end is True, then end_date will be yielded. ''' |
254 ''' Generates date from start_date to end_date. If include_end is True, then end_date will be yielded. ''' |
240 current_date = start_date |
255 current_date = start_date |
502 |
517 |
503 global trips_by_vehicle_info |
518 global trips_by_vehicle_info |
504 trips_by_vehicle_info = {} |
519 trips_by_vehicle_info = {} |
505 for trip in all_trips.values(): |
520 for trip in all_trips.values(): |
506 trips_by_vehicle_info[(trip.block_id, trip.schedule[0].arrival_time)] = trip |
521 trips_by_vehicle_info[(trip.block_id, trip.schedule[0].arrival_time)] = trip |
507 if 'services' in profile and profile['services'].get('default-service'): |
522 # Add services to all bus stops |
508 for route in routes.values(): |
523 for route in routes.values(): |
509 if not route.service: |
524 for trip in route.trips: |
510 route.service = profile['services']['default-service'] |
525 for halt in trip.schedule: |
511 for trip in route.trips: |
526 halt.stop.services.add(route.service) |
512 for halt in trip.schedule: |
|
513 halt.stop.services.add(route.service) |
|
514 |
527 |
515 if __name__ == '__main__': |
528 if __name__ == '__main__': |
516 profile.read('profiles/föli.ini') |
529 profile.read('profiles/föli.ini') |
517 load_buses('gtfs.zip') |
530 load_buses('gtfs.zip') |
518 import busroute |
531 import busroute |