45 |
45 |
46 class BusRoute: |
46 class BusRoute: |
47 def __init__(self, entry): |
47 def __init__(self, entry): |
48 self.id = entry['route_id'] |
48 self.id = entry['route_id'] |
49 self.reference = entry['route_short_name'] |
49 self.reference = entry['route_short_name'] |
|
50 self.trips = set() |
50 def __repr__(self): |
51 def __repr__(self): |
51 return 'routes[%r]' % self.reference |
52 return 'routes[%r]' % self.reference |
52 |
53 |
53 class BusService: |
54 class BusService: |
54 def __init__(self, reference): |
55 def __init__(self, reference): |
60 class BusStop: |
61 class BusStop: |
61 def __init__(self, reference, name, location): |
62 def __init__(self, reference, name, location): |
62 self.reference, self.name, self.location = reference, name, location |
63 self.reference, self.name, self.location = reference, name, location |
63 self.cluster = None |
64 self.cluster = None |
64 self.pairs = set() # samannimiset lähellä olevat pysäkit |
65 self.pairs = set() # samannimiset lähellä olevat pysäkit |
|
66 self.involved_trips = set() |
65 def __repr__(self): |
67 def __repr__(self): |
66 return 'bus_stops[%r]' % self.reference |
68 return 'bus_stops[%r]' % self.reference |
67 def schedule(self, max_amount = 50): |
69 def schedule(self, max_amount = 50): |
68 ''' |
70 ''' |
69 Hakee tämän pysäkin seuraavat `määrä` lähtöä. Päätepysäkille saapuvia busseja ei |
71 Hakee tämän pysäkin seuraavat `määrä` lähtöä. Päätepysäkille saapuvia busseja ei |
98 # palautettaisiin tyhjä result, niin algoritmi jatkaisi etsintää loputtomiin. |
100 # palautettaisiin tyhjä result, niin algoritmi jatkaisi etsintää loputtomiin. |
99 if date > viimeinen_käyttöpäivä: |
101 if date > viimeinen_käyttöpäivä: |
100 raise ValueError('tried to retrieve schedule for date %s which is outside schedule data' % date) |
102 raise ValueError('tried to retrieve schedule for date %s which is outside schedule data' % date) |
101 result = [] |
103 result = [] |
102 # Jokaiselle ajovuorolle, |
104 # Jokaiselle ajovuorolle, |
103 for trip in all_trips.values(): |
105 for trip in self.involved_trips: |
104 # jos tämä ajovuoro ajetaan tänä päivänä |
106 # jos tämä ajovuoro ajetaan tänä päivänä |
105 if trip.is_served_at(date): |
107 if trip.is_served_at(date): |
106 # ja jos tämä trip pysähtyy tällä pysäkillä, ei kuitenkaan saapuen |
108 # ja jos tämä trip pysähtyy tällä pysäkillä, ei kuitenkaan saapuen |
107 # päätepysäkille, |
109 # päätepysäkille, |
108 stop = trip.contains_stop(self) |
110 stop = trip.contains_stop(self) |
173 reference = row['trip_id'], |
175 reference = row['trip_id'], |
174 route = route, |
176 route = route, |
175 service = services[row['service_id']], |
177 service = services[row['service_id']], |
176 length = shape_distances[row['shape_id']] |
178 length = shape_distances[row['shape_id']] |
177 ) |
179 ) |
|
180 route.trips.add(trip) |
178 assert trip.name not in all_trips |
181 assert trip.name not in all_trips |
179 all_trips[trip.name] = trip |
182 all_trips[trip.name] = trip |
180 print('%d ajoa' % len(all_trips), file = stderr) |
183 print('%d ajoa' % len(all_trips), file = stderr) |
181 |
184 |
182 def lue_päiväys(teksti): |
185 def lue_päiväys(teksti): |
366 arrival_time = read_time(row['arrival_time']) |
369 arrival_time = read_time(row['arrival_time']) |
367 departure_time = read_time(row['departure_time']) |
370 departure_time = read_time(row['departure_time']) |
368 stop = bus_stops[row['stop_id']] |
371 stop = bus_stops[row['stop_id']] |
369 traveled_distance = float(row['shape_dist_traveled']) |
372 traveled_distance = float(row['shape_dist_traveled']) |
370 trip.schedule.append(BusHalt(arrival_time, departure_time, stop, trip, traveled_distance)) |
373 trip.schedule.append(BusHalt(arrival_time, departure_time, stop, trip, traveled_distance)) |
|
374 stop.involved_trips.add(trip) |
371 progress += 1 |
375 progress += 1 |
372 if progress % 1000 == 0: |
376 if progress % 1000 == 0: |
373 print('\rLadataan aikataulut... %.1f%%' % (progress * 100 / row_count), end = ' ', file = stderr) |
377 print('\rLadataan aikataulut... %.1f%%' % (progress * 100 / row_count), end = ' ', file = stderr) |
374 print('\rLadataan aikataulut... ladattu', file = stderr) |
378 print('\rLadataan aikataulut... ladattu', file = stderr) |
|
379 |
|
380 for trip in all_trips.values(): |
|
381 from busroute import simplify_name |
|
382 schedule = trip.concise_schedule() |
|
383 try: |
|
384 trip.from_place = simplify_name(schedule[0]) |
|
385 trip.to_place = simplify_name(schedule[-1]) |
|
386 except IndexError: |
|
387 trip.from_place = '' |
|
388 trip.to_place = '' |
|
389 |
|
390 for route in routes.values(): |
|
391 from collections import Counter |
|
392 from busroute import simplify_name |
|
393 tally = Counter() |
|
394 for trip in route.trips: |
|
395 schedule = trip.concise_schedule() |
|
396 places = set(schedule) |
|
397 do_add = True |
|
398 assert type(schedule) is list |
|
399 for candidate in tally: |
|
400 if places.issubset(set(candidate)): |
|
401 do_add = False |
|
402 tally.update({tuple(candidate)}) |
|
403 if do_add: |
|
404 tally.update({tuple(schedule)}) |
|
405 try: |
|
406 most_common_route = tally.most_common(1)[0][0] |
|
407 route.description = simplify_name(most_common_route[0]) + ' - ' + simplify_name(most_common_route[-1]) |
|
408 except: |
|
409 route.description = '' |
|
410 route.trips = sorted(route.trips, key = lambda trip: trip.schedule[0].departure_time) |