Thu, 07 Dec 2017 16:55:44 +0200
Stop_display muutoksia
0 | 1 | #!/usr/bin/env python3 |
2 | 2 | import enum, json |
3 | from sys import stderr | |
0 | 4 | from datetime import date, time, datetime, timedelta |
5 | 5 | from copy import copy |
2 | 6 | from misc import * |
7
f3791dccfd03
Käännetty tiedostojen nimet englanniksi
Teemu Piippo <teemu@hecknology.net>
parents:
5
diff
changeset
|
7 | from geometry import * |
0 | 8 | |
24
e6bdb9c54096
Yhtenäistetty ohjelmakoodin kieli englanniksi
Teemu Piippo <teemu@hecknology.net>
parents:
23
diff
changeset
|
9 | def transform_trip_reference(reference): |
e6bdb9c54096
Yhtenäistetty ohjelmakoodin kieli englanniksi
Teemu Piippo <teemu@hecknology.net>
parents:
23
diff
changeset
|
10 | return reference |
0 | 11 | |
24
e6bdb9c54096
Yhtenäistetty ohjelmakoodin kieli englanniksi
Teemu Piippo <teemu@hecknology.net>
parents:
23
diff
changeset
|
12 | class BusTrip: |
71
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
13 | def __init__(self, reference, route, service, length, block_id): |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
14 | self.reference, self.route, self.service, self.block_id = reference, route, service, block_id |
22 | 15 | self.length = length |
24
e6bdb9c54096
Yhtenäistetty ohjelmakoodin kieli englanniksi
Teemu Piippo <teemu@hecknology.net>
parents:
23
diff
changeset
|
16 | self.schedule = [] |
e6bdb9c54096
Yhtenäistetty ohjelmakoodin kieli englanniksi
Teemu Piippo <teemu@hecknology.net>
parents:
23
diff
changeset
|
17 | self.name = transform_trip_reference(reference) |
0 | 18 | def __repr__(self): |
24
e6bdb9c54096
Yhtenäistetty ohjelmakoodin kieli englanniksi
Teemu Piippo <teemu@hecknology.net>
parents:
23
diff
changeset
|
19 | return 'all_trips[%r]' % self.name |
e6bdb9c54096
Yhtenäistetty ohjelmakoodin kieli englanniksi
Teemu Piippo <teemu@hecknology.net>
parents:
23
diff
changeset
|
20 | def contains_stop(self, stop): |
e6bdb9c54096
Yhtenäistetty ohjelmakoodin kieli englanniksi
Teemu Piippo <teemu@hecknology.net>
parents:
23
diff
changeset
|
21 | for halt in self.schedule: |
e6bdb9c54096
Yhtenäistetty ohjelmakoodin kieli englanniksi
Teemu Piippo <teemu@hecknology.net>
parents:
23
diff
changeset
|
22 | if halt.stop is stop: |
e6bdb9c54096
Yhtenäistetty ohjelmakoodin kieli englanniksi
Teemu Piippo <teemu@hecknology.net>
parents:
23
diff
changeset
|
23 | return halt |
0 | 24 | else: |
25 | return None | |
24
e6bdb9c54096
Yhtenäistetty ohjelmakoodin kieli englanniksi
Teemu Piippo <teemu@hecknology.net>
parents:
23
diff
changeset
|
26 | def is_served_at(self, day): |
0 | 27 | try: |
24
e6bdb9c54096
Yhtenäistetty ohjelmakoodin kieli englanniksi
Teemu Piippo <teemu@hecknology.net>
parents:
23
diff
changeset
|
28 | return self.service in services_for_day[day] |
0 | 29 | except KeyError: |
30 | return False | |
24
e6bdb9c54096
Yhtenäistetty ohjelmakoodin kieli englanniksi
Teemu Piippo <teemu@hecknology.net>
parents:
23
diff
changeset
|
31 | def concise_schedule(self, starting_stop = None): |
72 | 32 | if starting_stop and starting_stop in self.schedule: |
33 | schedule = copy(self.schedule) | |
34 | schedule = schedule[schedule.index(starting_stop):] | |
35 | else: | |
36 | schedule = self.schedule | |
71
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
37 | if profile['regions']['use-regions']: |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
38 | used_areas = set() |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
39 | result = [] |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
40 | for halt in schedule: |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
41 | stop = halt.stop |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
42 | if stop.region and stop.region not in used_areas: |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
43 | used_areas.add(stop.region) |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
44 | result.append(stop.region) |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
45 | return result |
5 | 46 | else: |
72 | 47 | return [halt.stop.name for halt in schedule] |
0 | 48 | |
24
e6bdb9c54096
Yhtenäistetty ohjelmakoodin kieli englanniksi
Teemu Piippo <teemu@hecknology.net>
parents:
23
diff
changeset
|
49 | class BusRoute: |
e6bdb9c54096
Yhtenäistetty ohjelmakoodin kieli englanniksi
Teemu Piippo <teemu@hecknology.net>
parents:
23
diff
changeset
|
50 | def __init__(self, entry): |
e6bdb9c54096
Yhtenäistetty ohjelmakoodin kieli englanniksi
Teemu Piippo <teemu@hecknology.net>
parents:
23
diff
changeset
|
51 | self.id = entry['route_id'] |
e6bdb9c54096
Yhtenäistetty ohjelmakoodin kieli englanniksi
Teemu Piippo <teemu@hecknology.net>
parents:
23
diff
changeset
|
52 | self.reference = entry['route_short_name'] |
28
670ffa424ded
Bussipysäkit tallentavat ajovuoronsa välimuistiin suoritusajan nopeuttamiseksi
Teemu Piippo <teemu@hecknology.net>
parents:
26
diff
changeset
|
53 | self.trips = set() |
0 | 54 | def __repr__(self): |
24
e6bdb9c54096
Yhtenäistetty ohjelmakoodin kieli englanniksi
Teemu Piippo <teemu@hecknology.net>
parents:
23
diff
changeset
|
55 | return 'routes[%r]' % self.reference |
0 | 56 | |
24
e6bdb9c54096
Yhtenäistetty ohjelmakoodin kieli englanniksi
Teemu Piippo <teemu@hecknology.net>
parents:
23
diff
changeset
|
57 | class BusService: |
e6bdb9c54096
Yhtenäistetty ohjelmakoodin kieli englanniksi
Teemu Piippo <teemu@hecknology.net>
parents:
23
diff
changeset
|
58 | def __init__(self, reference): |
e6bdb9c54096
Yhtenäistetty ohjelmakoodin kieli englanniksi
Teemu Piippo <teemu@hecknology.net>
parents:
23
diff
changeset
|
59 | self.reference = reference |
e6bdb9c54096
Yhtenäistetty ohjelmakoodin kieli englanniksi
Teemu Piippo <teemu@hecknology.net>
parents:
23
diff
changeset
|
60 | self.dates = set() |
0 | 61 | def __repr__(self): |
24
e6bdb9c54096
Yhtenäistetty ohjelmakoodin kieli englanniksi
Teemu Piippo <teemu@hecknology.net>
parents:
23
diff
changeset
|
62 | return 'services[%r]' % self.reference |
0 | 63 | |
24
e6bdb9c54096
Yhtenäistetty ohjelmakoodin kieli englanniksi
Teemu Piippo <teemu@hecknology.net>
parents:
23
diff
changeset
|
64 | class BusStop: |
29 | 65 | def __init__(self, reference, name, location, code = None): |
24
e6bdb9c54096
Yhtenäistetty ohjelmakoodin kieli englanniksi
Teemu Piippo <teemu@hecknology.net>
parents:
23
diff
changeset
|
66 | self.reference, self.name, self.location = reference, name, location |
29 | 67 | self.code = code or reference |
15
a22cdf28930f
Lisätty bussipysäkkien ryhmittely
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
68 | self.cluster = None |
a22cdf28930f
Lisätty bussipysäkkien ryhmittely
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
69 | self.pairs = set() # samannimiset lähellä olevat pysäkit |
28
670ffa424ded
Bussipysäkit tallentavat ajovuoronsa välimuistiin suoritusajan nopeuttamiseksi
Teemu Piippo <teemu@hecknology.net>
parents:
26
diff
changeset
|
70 | self.involved_trips = set() |
0 | 71 | def __repr__(self): |
24
e6bdb9c54096
Yhtenäistetty ohjelmakoodin kieli englanniksi
Teemu Piippo <teemu@hecknology.net>
parents:
23
diff
changeset
|
72 | return 'bus_stops[%r]' % self.reference |
31
60045b362d71
- Ajovuoroa ei enää esitetä kahdessa välilehdessä vaan puukuvaimessa
Teemu Piippo <teemu@hecknology.net>
parents:
30
diff
changeset
|
73 | def schedule(self, *, max_amount = 50, arrivals = False): |
0 | 74 | ''' |
75 | Hakee tämän pysäkin seuraavat `määrä` lähtöä. Päätepysäkille saapuvia busseja ei | |
24
e6bdb9c54096
Yhtenäistetty ohjelmakoodin kieli englanniksi
Teemu Piippo <teemu@hecknology.net>
parents:
23
diff
changeset
|
76 | lasketa. Palauttaa pysähdykset listana jossa alkiot ovat muotoa (aika, halt), |
0 | 77 | jossa: |
78 | - `aika` on saapumishetki muotoa datetime ja | |
24
e6bdb9c54096
Yhtenäistetty ohjelmakoodin kieli englanniksi
Teemu Piippo <teemu@hecknology.net>
parents:
23
diff
changeset
|
79 | - `halt` on vastaava BusHalt olio. |
0 | 80 | |
81 | Mikäli pysäkille ei ole määrätty riittävästi pysähdyksiä kalenterissa, tuloslista | |
82 | jää alimittaiseksi, mahdollisesti jopa tyhjäksi. | |
83 | ''' | |
17
fa3c822859b5
Refaktorioitu aikatauluhaku
Teemu Piippo <teemu@hecknology.net>
parents:
15
diff
changeset
|
84 | result = [] |
20
3199e289ae62
- Sivusto hieman edustuksellisempi
Teemu Piippo <teemu@hecknology.net>
parents:
19
diff
changeset
|
85 | # -1 päivää yövuoroja varten |
24
e6bdb9c54096
Yhtenäistetty ohjelmakoodin kieli englanniksi
Teemu Piippo <teemu@hecknology.net>
parents:
23
diff
changeset
|
86 | date = today() - timedelta(days = 1) |
0 | 87 | # Niin kauan kuin aikatauluja ei ole vielä tarpeeksi, |
17
fa3c822859b5
Refaktorioitu aikatauluhaku
Teemu Piippo <teemu@hecknology.net>
parents:
15
diff
changeset
|
88 | while len(result) < max_amount: |
0 | 89 | try: |
90 | # hae nykyisen päivän aikataulut ja lisää ne, | |
31
60045b362d71
- Ajovuoroa ei enää esitetä kahdessa välilehdessä vaan puukuvaimessa
Teemu Piippo <teemu@hecknology.net>
parents:
30
diff
changeset
|
91 | result += self.schedule_for_day(date, arrivals = arrivals) |
17
fa3c822859b5
Refaktorioitu aikatauluhaku
Teemu Piippo <teemu@hecknology.net>
parents:
15
diff
changeset
|
92 | except ValueError: |
0 | 93 | # paitsi jos mentiin kalenterin ulkopuolelle, jolloin lopetetaan, |
94 | break | |
95 | # ja siirry seuraavaan päivään. | |
17
fa3c822859b5
Refaktorioitu aikatauluhaku
Teemu Piippo <teemu@hecknology.net>
parents:
15
diff
changeset
|
96 | date += timedelta(1) |
0 | 97 | # Typistä lopputulos haluttuun tulosmäärään. |
17
fa3c822859b5
Refaktorioitu aikatauluhaku
Teemu Piippo <teemu@hecknology.net>
parents:
15
diff
changeset
|
98 | return result[:max_amount] |
31
60045b362d71
- Ajovuoroa ei enää esitetä kahdessa välilehdessä vaan puukuvaimessa
Teemu Piippo <teemu@hecknology.net>
parents:
30
diff
changeset
|
99 | def schedule_for_day(self, date, *, arrivals = False): |
17
fa3c822859b5
Refaktorioitu aikatauluhaku
Teemu Piippo <teemu@hecknology.net>
parents:
15
diff
changeset
|
100 | ''' |
fa3c822859b5
Refaktorioitu aikatauluhaku
Teemu Piippo <teemu@hecknology.net>
parents:
15
diff
changeset
|
101 | Hakee pysäkin aikataulut tiettynä päivänä. |
fa3c822859b5
Refaktorioitu aikatauluhaku
Teemu Piippo <teemu@hecknology.net>
parents:
15
diff
changeset
|
102 | ''' |
fa3c822859b5
Refaktorioitu aikatauluhaku
Teemu Piippo <teemu@hecknology.net>
parents:
15
diff
changeset
|
103 | # Jos päädyttiin aikataulukalenterin ulkopuolelle, niin tuotetaan virhe. Jos vain |
24
e6bdb9c54096
Yhtenäistetty ohjelmakoodin kieli englanniksi
Teemu Piippo <teemu@hecknology.net>
parents:
23
diff
changeset
|
104 | # palautettaisiin tyhjä result, niin algoritmi jatkaisi etsintää loputtomiin. |
17
fa3c822859b5
Refaktorioitu aikatauluhaku
Teemu Piippo <teemu@hecknology.net>
parents:
15
diff
changeset
|
105 | if date > viimeinen_käyttöpäivä: |
fa3c822859b5
Refaktorioitu aikatauluhaku
Teemu Piippo <teemu@hecknology.net>
parents:
15
diff
changeset
|
106 | raise ValueError('tried to retrieve schedule for date %s which is outside schedule data' % date) |
fa3c822859b5
Refaktorioitu aikatauluhaku
Teemu Piippo <teemu@hecknology.net>
parents:
15
diff
changeset
|
107 | result = [] |
fa3c822859b5
Refaktorioitu aikatauluhaku
Teemu Piippo <teemu@hecknology.net>
parents:
15
diff
changeset
|
108 | # Jokaiselle ajovuorolle, |
28
670ffa424ded
Bussipysäkit tallentavat ajovuoronsa välimuistiin suoritusajan nopeuttamiseksi
Teemu Piippo <teemu@hecknology.net>
parents:
26
diff
changeset
|
109 | for trip in self.involved_trips: |
17
fa3c822859b5
Refaktorioitu aikatauluhaku
Teemu Piippo <teemu@hecknology.net>
parents:
15
diff
changeset
|
110 | # jos tämä ajovuoro ajetaan tänä päivänä |
24
e6bdb9c54096
Yhtenäistetty ohjelmakoodin kieli englanniksi
Teemu Piippo <teemu@hecknology.net>
parents:
23
diff
changeset
|
111 | if trip.is_served_at(date): |
e6bdb9c54096
Yhtenäistetty ohjelmakoodin kieli englanniksi
Teemu Piippo <teemu@hecknology.net>
parents:
23
diff
changeset
|
112 | # ja jos tämä trip pysähtyy tällä pysäkillä, ei kuitenkaan saapuen |
17
fa3c822859b5
Refaktorioitu aikatauluhaku
Teemu Piippo <teemu@hecknology.net>
parents:
15
diff
changeset
|
113 | # päätepysäkille, |
24
e6bdb9c54096
Yhtenäistetty ohjelmakoodin kieli englanniksi
Teemu Piippo <teemu@hecknology.net>
parents:
23
diff
changeset
|
114 | stop = trip.contains_stop(self) |
31
60045b362d71
- Ajovuoroa ei enää esitetä kahdessa välilehdessä vaan puukuvaimessa
Teemu Piippo <teemu@hecknology.net>
parents:
30
diff
changeset
|
115 | if stop and (arrivals or not stop.is_arrival) and stop is not trip.schedule[-1]: |
24
e6bdb9c54096
Yhtenäistetty ohjelmakoodin kieli englanniksi
Teemu Piippo <teemu@hecknology.net>
parents:
23
diff
changeset
|
116 | # ja jos tämä halt on tulevaisuudessa, |
75 | 117 | stop_time = datetime.combine(date, time()) + stop.departure_time |
42
0b53b7c70875
Näytä myös aikataulut jotka ovat minuutti sitten menneet
teemu
parents:
41
diff
changeset
|
118 | if stop_time + timedelta(minutes = 1) >= now(): |
24
e6bdb9c54096
Yhtenäistetty ohjelmakoodin kieli englanniksi
Teemu Piippo <teemu@hecknology.net>
parents:
23
diff
changeset
|
119 | # lisää halt listaan. |
18 | 120 | result.append({ |
121 | 'time': stop_time, | |
122 | 'trip': trip, | |
123 | 'stop': stop, | |
124 | }) | |
17
fa3c822859b5
Refaktorioitu aikatauluhaku
Teemu Piippo <teemu@hecknology.net>
parents:
15
diff
changeset
|
125 | # Lajittele lopputulos saapumisajan mukaan. |
18 | 126 | result.sort(key = lambda schedule_entry: schedule_entry['time']) |
17
fa3c822859b5
Refaktorioitu aikatauluhaku
Teemu Piippo <teemu@hecknology.net>
parents:
15
diff
changeset
|
127 | return result |
0 | 128 | |
24
e6bdb9c54096
Yhtenäistetty ohjelmakoodin kieli englanniksi
Teemu Piippo <teemu@hecknology.net>
parents:
23
diff
changeset
|
129 | class BusHalt: |
e6bdb9c54096
Yhtenäistetty ohjelmakoodin kieli englanniksi
Teemu Piippo <teemu@hecknology.net>
parents:
23
diff
changeset
|
130 | def __init__(self, arrival_time, departure_time, stop, trip, traveled_distance): |
e6bdb9c54096
Yhtenäistetty ohjelmakoodin kieli englanniksi
Teemu Piippo <teemu@hecknology.net>
parents:
23
diff
changeset
|
131 | self.arrival_time, self.departure_time, self.stop, self.trip = arrival_time, departure_time, \ |
e6bdb9c54096
Yhtenäistetty ohjelmakoodin kieli englanniksi
Teemu Piippo <teemu@hecknology.net>
parents:
23
diff
changeset
|
132 | stop, trip |
e6bdb9c54096
Yhtenäistetty ohjelmakoodin kieli englanniksi
Teemu Piippo <teemu@hecknology.net>
parents:
23
diff
changeset
|
133 | self.traveled_distance = traveled_distance |
23 | 134 | @property |
29 | 135 | def is_arrival(self): |
71
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
136 | if profile['regions']['use-regions']: |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
137 | if not hasattr(self, 'cachedIsArrival'): |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
138 | if self.stop.region: |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
139 | iterator = iter(self.trip.schedule) |
29 | 140 | stop = next(iterator) |
71
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
141 | while stop is not self: |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
142 | stop = next(iterator) |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
143 | for stop in iterator: |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
144 | if stop.stop.region != self.stop.region: |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
145 | self.cachedIsArrival = False |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
146 | break |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
147 | else: |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
148 | self.cachedIsArrival = True |
29 | 149 | else: |
71
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
150 | self.cachedIsArrival = False |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
151 | return self.cachedIsArrival |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
152 | else: |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
153 | return self == self.trip.schedule[-1] |
0 | 154 | def __repr__(self): |
24
e6bdb9c54096
Yhtenäistetty ohjelmakoodin kieli englanniksi
Teemu Piippo <teemu@hecknology.net>
parents:
23
diff
changeset
|
155 | return 'BusHalt(%r, %r, %r, %r)' % (self.arrival_time, self.departure_time, self.stop, self.trip) |
71
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
156 | def sign(self, long = False): |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
157 | from busroute import reduce_schedule |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
158 | return reduce_schedule( |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
159 | route = self.trip.concise_schedule(self), |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
160 | trip_length = self.trip.length - self.traveled_distance, |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
161 | long = long, |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
162 | ) |
0 | 163 | |
24
e6bdb9c54096
Yhtenäistetty ohjelmakoodin kieli englanniksi
Teemu Piippo <teemu@hecknology.net>
parents:
23
diff
changeset
|
164 | routes = {} |
e6bdb9c54096
Yhtenäistetty ohjelmakoodin kieli englanniksi
Teemu Piippo <teemu@hecknology.net>
parents:
23
diff
changeset
|
165 | routes_per_id = {} |
e6bdb9c54096
Yhtenäistetty ohjelmakoodin kieli englanniksi
Teemu Piippo <teemu@hecknology.net>
parents:
23
diff
changeset
|
166 | all_trips = {} |
e6bdb9c54096
Yhtenäistetty ohjelmakoodin kieli englanniksi
Teemu Piippo <teemu@hecknology.net>
parents:
23
diff
changeset
|
167 | services = {} |
e6bdb9c54096
Yhtenäistetty ohjelmakoodin kieli englanniksi
Teemu Piippo <teemu@hecknology.net>
parents:
23
diff
changeset
|
168 | bus_stops = {} |
15
a22cdf28930f
Lisätty bussipysäkkien ryhmittely
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
169 | all_clusters = set() |
30 | 170 | viimeinen_käyttöpäivä = None |
171 | clusters_by_name = {} | |
24
e6bdb9c54096
Yhtenäistetty ohjelmakoodin kieli englanniksi
Teemu Piippo <teemu@hecknology.net>
parents:
23
diff
changeset
|
172 | services_for_day = {} |
0 | 173 | |
71
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
174 | def load_buses(gtfs_zip_path): |
30 | 175 | global viimeinen_käyttöpäivä |
176 | from zipfile import ZipFile | |
177 | with ZipFile(gtfs_zip_path) as gtfs_zip: | |
178 | print('Ladataan linjat... ', file = stderr, end = '', flush = True) | |
179 | with gtfs_zip.open('routes.txt') as file: | |
180 | for row in read_csv(map(bytes.decode, file)): | |
181 | route = BusRoute(row) | |
182 | routes[route.reference] = route | |
183 | routes_per_id[route.id] = route | |
184 | print('%d linjaa' % len(routes), file = stderr) | |
185 | ||
186 | print('Ladataan ajovuorot... ', file = stderr, end = '', flush = True) | |
187 | ||
188 | shape_distances = {} | |
71
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
189 | try: |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
190 | with gtfs_zip.open('shapes.txt') as file: |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
191 | for row in read_csv(map(bytes.decode, file)): |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
192 | shape_distances[row['shape_id']] = max(shape_distances.get(row['shape_id'], 0), float(row['shape_dist_traveled'])) |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
193 | except KeyError: |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
194 | pass |
29 | 195 | |
30 | 196 | with gtfs_zip.open('trips.txt') as file: |
197 | for row in read_csv(map(bytes.decode, file)): | |
198 | if row['service_id'] not in services: | |
199 | services[row['service_id']] = BusService(row['service_id']) | |
200 | route = routes_per_id[row['route_id']] | |
201 | trip = BusTrip( | |
202 | reference = row['trip_id'], | |
203 | route = route, | |
204 | service = services[row['service_id']], | |
71
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
205 | length = shape_distances.get(row.get('shape_id'), 1) * float(profile['metrics']['shape-modifier']), |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
206 | block_id = row['block_id'], |
30 | 207 | ) |
208 | route.trips.add(trip) | |
71
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
209 | if trip.name in all_trips: |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
210 | print('Trip %s already exists' % trip.name) |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
211 | else: |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
212 | all_trips[trip.name] = trip |
30 | 213 | print('%d ajoa' % len(all_trips), file = stderr) |
0 | 214 | |
30 | 215 | def read_date(teksti): |
216 | return date(int(teksti[:4]), int(teksti[4:6]), int(teksti[6:])) | |
217 | ||
218 | def read_time(teksti): | |
219 | tunti, minuutti, sekunti = map(int, teksti.split(':')) | |
220 | return timedelta(hours = tunti, minutes = minuutti, seconds = sekunti) | |
221 | ||
222 | print('Ladataan päiväykset... ', file = stderr, flush = True) | |
223 | ||
224 | viimeinen_käyttöpäivä = date.today() | |
29 | 225 | |
30 | 226 | def date_range(start_date, end_date, *, include_end = False): |
227 | ''' Generates date from start_date to end_date. If include_end is True, then end_date will be yielded. ''' | |
228 | current_date = start_date | |
229 | while current_date < end_date: | |
230 | yield current_date | |
231 | current_date += timedelta(1) | |
232 | if include_end: | |
233 | yield end_date | |
29 | 234 | |
30 | 235 | def add_day_to_service(service_name, day): |
236 | try: | |
237 | service = services[service_name] | |
238 | except KeyError: | |
239 | return | |
240 | else: | |
241 | service.dates.add(day) | |
242 | if day not in services_for_day: | |
243 | services_for_day[day] = set() | |
244 | services_for_day[day].add(service) | |
245 | global viimeinen_käyttöpäivä | |
246 | viimeinen_käyttöpäivä = max(day, viimeinen_käyttöpäivä) | |
29 | 247 | |
30 | 248 | def filter_day(row, day): |
249 | day_names = ['monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday', 'sunday'] | |
250 | return int(row[day_names[day.isoweekday() - 1]]) | |
0 | 251 | |
30 | 252 | with gtfs_zip.open('calendar.txt') as file: |
253 | for row in read_csv(map(bytes.decode, file)): | |
254 | for day in date_range(read_date(row['start_date']), read_date(row['end_date']), include_end = True): | |
255 | if filter_day(row, day): | |
256 | add_day_to_service(service_name = row['service_id'], day = day) | |
257 | ||
258 | with gtfs_zip.open('calendar_dates.txt') as file: | |
259 | for row in read_csv(map(bytes.decode, file)): | |
260 | add_day_to_service(service_name = row['service_id'], day = read_date(row['date'])) | |
261 | ||
262 | def services_available_at(day): | |
263 | for service in services.values(): | |
264 | if day in service.dates: | |
265 | yield service | |
266 | ||
267 | print('Ladataan pysäkit... ', file = stderr, end = '', flush = True) | |
268 | with gtfs_zip.open('stops.txt') as file: | |
269 | for row in read_csv(map(bytes.decode, file)): | |
270 | location = Sijainti(float(row['stop_lat']), float(row['stop_lon'])) | |
271 | stop = BusStop( | |
272 | reference = row['stop_id'], | |
273 | name = row['stop_name'], | |
274 | location = location, | |
71
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
275 | code = row.get('stop_code', row['stop_id']), |
30 | 276 | ) |
277 | bus_stops[stop.reference] = stop | |
71
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
278 | if profile['regions']['use-regions']: |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
279 | with open('regions-per-stop.json') as file: |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
280 | for stop_reference, region in json.load(file).items(): |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
281 | try: |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
282 | bus_stops[stop_reference].region = region |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
283 | except KeyError: |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
284 | pass |
30 | 285 | print('%d pysäkkiä' % len(bus_stops), file = stderr) |
0 | 286 | |
21 | 287 | |
30 | 288 | class BusStopCluster: |
289 | def __init__(self): | |
290 | self.stops = set() | |
291 | self._center = None | |
292 | self.name = None | |
293 | @property | |
294 | def url_name(self): | |
295 | return self.name.lower().replace('(', '').replace(')', '').replace(' ', '-') | |
296 | def add_stop(self, stop): | |
297 | assert not stop.cluster | |
298 | stop.cluster = self | |
299 | self.stops.add(stop) | |
300 | self._center = None | |
301 | @property | |
302 | def center(self): | |
303 | if not self._center: | |
304 | if self.stops: | |
305 | from statistics import median | |
306 | pointtype = type(next(iter(self.stops)).location) | |
307 | self._center = pointtype( | |
308 | median(stop.location.x for stop in self.stops), | |
309 | median(stop.location.y for stop in self.stops), | |
310 | ) | |
311 | else: | |
312 | raise ValueError('an empty cluster has no center point') | |
313 | return self._center | |
314 | def merge(self, other): | |
315 | for bus_stop in other.stops: | |
316 | bus_stop.cluster = self | |
317 | self.stops |= other.stops | |
318 | other.stops = set() | |
319 | other._center = None | |
31
60045b362d71
- Ajovuoroa ei enää esitetä kahdessa välilehdessä vaan puukuvaimessa
Teemu Piippo <teemu@hecknology.net>
parents:
30
diff
changeset
|
320 | def schedule(self, *, max_amount = 50): |
30 | 321 | result = [] |
322 | for stop in self.stops: | |
31
60045b362d71
- Ajovuoroa ei enää esitetä kahdessa välilehdessä vaan puukuvaimessa
Teemu Piippo <teemu@hecknology.net>
parents:
30
diff
changeset
|
323 | result += stop.schedule(max_amount = max_amount) |
30 | 324 | result.sort(key = lambda schedule_entry: schedule_entry['time']) |
325 | return result[:max_amount] | |
15
a22cdf28930f
Lisätty bussipysäkkien ryhmittely
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
326 | |
30 | 327 | from collections import defaultdict |
328 | bus_stops_by_name = defaultdict(set) | |
329 | for bus_stop in bus_stops.values(): | |
330 | bus_stops_by_name[bus_stop.name].add(bus_stop) | |
331 | bus_stops_by_name = dict(bus_stops_by_name) | |
15
a22cdf28930f
Lisätty bussipysäkkien ryhmittely
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
332 | |
30 | 333 | # ryhmittele bus_stops nimen mukaan |
41 | 334 | global all_clusters |
30 | 335 | all_clusters = [] |
336 | def cluster_bus_stops(): | |
337 | sorted_bus_stops = sorted(bus_stops.values(), key = lambda bus_stop: bus_stop.name) | |
338 | for bus_stop in sorted_bus_stops: | |
339 | if not bus_stop.cluster: | |
340 | stops_to_cluster = {bus_stop} | |
341 | # etsi pysäkin samannimiset vastaparit | |
342 | for pair_candidate in bus_stops_by_name[bus_stop.name]: | |
343 | distance = pair_candidate.location.etäisyys(bus_stop.location) | |
344 | if pair_candidate is not bus_stop and distance <= 0.4: | |
345 | stops_to_cluster.add(pair_candidate) | |
346 | for stop_to_cluster in stops_to_cluster: | |
347 | if stop_to_cluster.cluster: | |
348 | cluster = stop_to_cluster.cluster | |
349 | break | |
350 | else: | |
351 | cluster = BusStopCluster() | |
352 | all_clusters.append(cluster) | |
353 | for stop_to_cluster in stops_to_cluster: | |
354 | if not stop_to_cluster.cluster: | |
355 | cluster.add_stop(stop_to_cluster) | |
356 | # Merkitse muistiin pysäkkien vastaparit käyttäen hyväksi tämänhetkistä ryhmittelytietoa | |
357 | for bus_stop in bus_stops.values(): | |
358 | if bus_stop.cluster: | |
359 | bus_stop.pairs = bus_stop.cluster.stops - {bus_stop} | |
360 | # Ryhmitä ne bus_stops, joilla ei ollut omaa vastaparia, muiden pysäkkien kanssa | |
361 | for bus_stop in sorted_bus_stops: | |
362 | if len(bus_stop.cluster.stops) == 1: | |
363 | possibilities = set() | |
364 | for cluster in all_clusters: | |
365 | if cluster is not bus_stop.cluster: | |
366 | distance = cluster.center.etäisyys(bus_stop.location) | |
367 | if distance <= 0.4: | |
368 | possibilities.add((distance, cluster)) | |
369 | if possibilities: | |
370 | best = min(possibilities)[1] | |
371 | all_clusters.remove(bus_stop.cluster) | |
372 | best.merge(bus_stop.cluster) | |
15
a22cdf28930f
Lisätty bussipysäkkien ryhmittely
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
373 | |
30 | 374 | def shared_elements_in_n_sets(sets): |
375 | from itertools import combinations | |
376 | result = set() | |
377 | for pair in combinations(sets, 2): | |
378 | result |= pair[0] & pair[1] | |
379 | return result | |
15
a22cdf28930f
Lisätty bussipysäkkien ryhmittely
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
380 | |
30 | 381 | def name_clusters(): |
382 | from collections import defaultdict | |
383 | from pprint import pprint | |
384 | clusters_per_name = defaultdict(set) | |
385 | for cluster in all_clusters: | |
386 | name_representing_stop = min((len(stop.reference), stop.reference, stop) for stop in cluster.stops)[2] | |
387 | clusters_per_name[name_representing_stop.name].add(cluster) | |
388 | for name, clusters in clusters_per_name.items(): | |
389 | if len(clusters) == 1: | |
390 | # Ryhmä on ainoa jolla on varaus tälle nimelle. Sen kuin vaan. | |
391 | next(iter(clusters)).name = name | |
392 | else: | |
71
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
393 | if profile['regions']['use-regions']: |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
394 | # Olisiko kaikki klusterit eri alueilla? |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
395 | common_regions = shared_elements_in_n_sets({stop.region for stop in cluster.stops} for cluster in clusters) |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
396 | # Esitys: ryhmä -> ne alueet jotka ovat tälle ryhmälle ainutlaatuisia |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
397 | proposal = { |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
398 | cluster: {stop.region for stop in cluster.stops} - common_regions - {None} |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
399 | for cluster in clusters |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
400 | } |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
401 | # Jos enintään yksi klusteri tässä esityksessä on kokonaan ilman omaa aluetta, jolla se voisi eritellä, |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
402 | # niin nimetään klusterit näiden alueiden mukaan. |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
403 | # Se klusteri jolla ei ole omaa aluetta (jos on) jätetään ilman aluepäätettä. |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
404 | if sum([1 for unique_areas in proposal.values() if not unique_areas]) <= 1: |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
405 | for cluster, unique_areas in proposal.items(): |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
406 | individual_cluster_name = name |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
407 | if unique_areas: |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
408 | individual_cluster_name += ' (' + min(unique_areas) + ')' |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
409 | cluster.name = individual_cluster_name |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
410 | break |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
411 | # Typerä reunatapaus. Indeksoidaan numeroin... |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
412 | for n, (_, cluster) in enumerate(sorted( |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
413 | min((stop.reference.lower(), cluster) for stop in cluster.stops) |
30 | 414 | for cluster in clusters |
71
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
415 | ), 1): |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
416 | individual_cluster_name = name + '-' + str(n) |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
417 | cluster.name = individual_cluster_name |
30 | 418 | |
419 | print('Ryhmitellään pysäkit...') | |
420 | cluster_bus_stops() | |
421 | name_clusters() | |
422 | ||
423 | for cluster in all_clusters: | |
424 | if cluster.url_name in clusters_by_name: | |
425 | print('Warning: Clusters %r and %r share the same URL name: %r' % (cluster.name, clusters_by_name[cluster.url_name].name, cluster.url_name)) | |
15
a22cdf28930f
Lisätty bussipysäkkien ryhmittely
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
426 | else: |
30 | 427 | clusters_by_name[cluster.url_name] = cluster |
19
16fa9fb20b32
Lisätty pysäkkiryhmän aikataulunäkymä
Teemu Piippo <teemu@hecknology.net>
parents:
18
diff
changeset
|
428 | |
30 | 429 | print('Ladataan aikataulut... ', end = '', flush = True, file = stderr) |
430 | with gtfs_zip.open('stop_times.txt') as file: | |
431 | row_count = sum(line.count(b'\n') for line in file) | |
432 | with gtfs_zip.open('stop_times.txt') as file: | |
433 | progress = 0 | |
434 | for row in read_csv(map(bytes.decode, file)): | |
71
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
435 | if int(row.get('pickup_type', 0)) and int(row.get('drop_off_type', 0)): |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
436 | continue |
30 | 437 | trip = all_trips[transform_trip_reference(row['trip_id'])] |
438 | arrival_time = read_time(row['arrival_time']) | |
439 | departure_time = read_time(row['departure_time']) | |
440 | stop = bus_stops[row['stop_id']] | |
71
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
441 | traveled_distance = float(row.get('shape_dist_traveled', 1)) * float(profile['metrics']['shape-modifier']) |
30 | 442 | trip.schedule.append(BusHalt(arrival_time, departure_time, stop, trip, traveled_distance)) |
443 | stop.involved_trips.add(trip) | |
444 | progress += 1 | |
445 | if progress % 1000 == 0: | |
446 | print('\rLadataan aikataulut... %.1f%%' % (progress * 100 / row_count), end = ' ', file = stderr) | |
447 | print('\rLadataan aikataulut... ladattu', file = stderr) | |
448 | ||
449 | for trip in all_trips.values(): | |
450 | from busroute import simplify_name | |
451 | schedule = trip.concise_schedule() | |
452 | try: | |
453 | trip.from_place = simplify_name(schedule[0]) | |
454 | trip.to_place = simplify_name(schedule[-1]) | |
455 | except IndexError: | |
456 | trip.from_place = '' | |
457 | trip.to_place = '' | |
28
670ffa424ded
Bussipysäkit tallentavat ajovuoronsa välimuistiin suoritusajan nopeuttamiseksi
Teemu Piippo <teemu@hecknology.net>
parents:
26
diff
changeset
|
458 | |
30 | 459 | for route in routes.values(): |
460 | from collections import Counter | |
461 | from busroute import simplify_name | |
462 | tally = Counter() | |
463 | for trip in route.trips: | |
464 | schedule = trip.concise_schedule() | |
465 | places = set(schedule) | |
466 | do_add = True | |
467 | assert type(schedule) is list | |
468 | for candidate in tally: | |
469 | if places.issubset(set(candidate)): | |
470 | do_add = False | |
471 | tally.update({tuple(candidate)}) | |
472 | if do_add: | |
473 | tally.update({tuple(schedule)}) | |
474 | try: | |
475 | most_common_route = tally.most_common(1)[0][0] | |
476 | route.description = simplify_name(most_common_route[0]) + ' - ' + simplify_name(most_common_route[-1]) | |
477 | except: | |
478 | route.description = '' | |
71
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
479 | route.trips = sorted(route.trips, key = lambda trip: trip.schedule and trip.schedule[0].departure_time or timedelta()) |
28
670ffa424ded
Bussipysäkit tallentavat ajovuoronsa välimuistiin suoritusajan nopeuttamiseksi
Teemu Piippo <teemu@hecknology.net>
parents:
26
diff
changeset
|
480 | |
72 | 481 | if 'compatibility' in profile and profile['compatibility'].get('fix-destination-times', False): |
482 | # Fölin datassa on jotain tosi kummaa. Ilmeisesti ajovuoron viimeisen pysähdyksen saapumisaika on ihan täysin | |
483 | # väärin. Arvaan että se on seuraavan lähdön aika, mutta joka tapauksessa se on väärin. | |
484 | # Arvataan mikä se todellinen saapumisaika on. Se ei voi mennä kauhean paljon pahemmin vikaan kuin alkuperäinen | |
485 | # väärin oleva data. | |
486 | for trip in all_trips.values(): | |
487 | if len(trip.schedule) >= 2: | |
488 | bus_speed_coefficient = 750 # metriä minuutissa | |
489 | last_leg_distance = trip.schedule[-1].traveled_distance - trip.schedule[-2].traveled_distance | |
490 | trip.schedule[-1].arrival_time = trip.schedule[-2].departure_time + timedelta(minutes = last_leg_distance / bus_speed_coefficient) | |
71
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
491 | |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
492 | global trips_by_vehicle_info |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
493 | trips_by_vehicle_info = {} |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
494 | for trip in all_trips.values(): |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
495 | trips_by_vehicle_info[(trip.block_id, trip.schedule[0].arrival_time)] = trip |
30 | 496 | |
497 | if __name__ == '__main__': | |
498 | profile.read('profiles/föli.ini') | |
71
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
499 | load_buses('gtfs.zip') |