Sun, 07 Jan 2018 01:05:38 +0200
fixes
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({ |
77 | 121 | 'date': date, |
122 | 'offset': stop.departure_time, | |
18 | 123 | 'time': stop_time, |
124 | 'trip': trip, | |
125 | 'stop': stop, | |
126 | }) | |
17
fa3c822859b5
Refaktorioitu aikatauluhaku
Teemu Piippo <teemu@hecknology.net>
parents:
15
diff
changeset
|
127 | # Lajittele lopputulos saapumisajan mukaan. |
18 | 128 | result.sort(key = lambda schedule_entry: schedule_entry['time']) |
17
fa3c822859b5
Refaktorioitu aikatauluhaku
Teemu Piippo <teemu@hecknology.net>
parents:
15
diff
changeset
|
129 | return result |
0 | 130 | |
24
e6bdb9c54096
Yhtenäistetty ohjelmakoodin kieli englanniksi
Teemu Piippo <teemu@hecknology.net>
parents:
23
diff
changeset
|
131 | class BusHalt: |
e6bdb9c54096
Yhtenäistetty ohjelmakoodin kieli englanniksi
Teemu Piippo <teemu@hecknology.net>
parents:
23
diff
changeset
|
132 | 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
|
133 | 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
|
134 | stop, trip |
e6bdb9c54096
Yhtenäistetty ohjelmakoodin kieli englanniksi
Teemu Piippo <teemu@hecknology.net>
parents:
23
diff
changeset
|
135 | self.traveled_distance = traveled_distance |
23 | 136 | @property |
29 | 137 | def is_arrival(self): |
71
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
138 | if profile['regions']['use-regions']: |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
139 | if not hasattr(self, 'cachedIsArrival'): |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
140 | if self.stop.region: |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
141 | iterator = iter(self.trip.schedule) |
29 | 142 | stop = next(iterator) |
71
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
143 | while stop is not self: |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
144 | stop = next(iterator) |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
145 | for stop in iterator: |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
146 | if stop.stop.region != self.stop.region: |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
147 | self.cachedIsArrival = False |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
148 | break |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
149 | else: |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
150 | self.cachedIsArrival = True |
29 | 151 | else: |
71
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
152 | self.cachedIsArrival = False |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
153 | return self.cachedIsArrival |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
154 | else: |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
155 | return self == self.trip.schedule[-1] |
0 | 156 | def __repr__(self): |
24
e6bdb9c54096
Yhtenäistetty ohjelmakoodin kieli englanniksi
Teemu Piippo <teemu@hecknology.net>
parents:
23
diff
changeset
|
157 | 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
|
158 | def sign(self, long = False): |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
159 | from busroute import reduce_schedule |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
160 | return reduce_schedule( |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
161 | route = self.trip.concise_schedule(self), |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
162 | 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
|
163 | long = long, |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
164 | ) |
0 | 165 | |
24
e6bdb9c54096
Yhtenäistetty ohjelmakoodin kieli englanniksi
Teemu Piippo <teemu@hecknology.net>
parents:
23
diff
changeset
|
166 | routes = {} |
e6bdb9c54096
Yhtenäistetty ohjelmakoodin kieli englanniksi
Teemu Piippo <teemu@hecknology.net>
parents:
23
diff
changeset
|
167 | routes_per_id = {} |
e6bdb9c54096
Yhtenäistetty ohjelmakoodin kieli englanniksi
Teemu Piippo <teemu@hecknology.net>
parents:
23
diff
changeset
|
168 | all_trips = {} |
e6bdb9c54096
Yhtenäistetty ohjelmakoodin kieli englanniksi
Teemu Piippo <teemu@hecknology.net>
parents:
23
diff
changeset
|
169 | services = {} |
e6bdb9c54096
Yhtenäistetty ohjelmakoodin kieli englanniksi
Teemu Piippo <teemu@hecknology.net>
parents:
23
diff
changeset
|
170 | bus_stops = {} |
15
a22cdf28930f
Lisätty bussipysäkkien ryhmittely
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
171 | all_clusters = set() |
30 | 172 | viimeinen_käyttöpäivä = None |
173 | clusters_by_name = {} | |
24
e6bdb9c54096
Yhtenäistetty ohjelmakoodin kieli englanniksi
Teemu Piippo <teemu@hecknology.net>
parents:
23
diff
changeset
|
174 | services_for_day = {} |
0 | 175 | |
71
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
176 | def load_buses(gtfs_zip_path): |
30 | 177 | global viimeinen_käyttöpäivä |
178 | from zipfile import ZipFile | |
179 | with ZipFile(gtfs_zip_path) as gtfs_zip: | |
180 | print('Ladataan linjat... ', file = stderr, end = '', flush = True) | |
181 | with gtfs_zip.open('routes.txt') as file: | |
182 | for row in read_csv(map(bytes.decode, file)): | |
183 | route = BusRoute(row) | |
184 | routes[route.reference] = route | |
185 | routes_per_id[route.id] = route | |
186 | print('%d linjaa' % len(routes), file = stderr) | |
187 | ||
188 | print('Ladataan ajovuorot... ', file = stderr, end = '', flush = True) | |
189 | ||
190 | shape_distances = {} | |
71
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
191 | try: |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
192 | 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
|
193 | 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
|
194 | 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
|
195 | except KeyError: |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
196 | pass |
29 | 197 | |
30 | 198 | with gtfs_zip.open('trips.txt') as file: |
199 | for row in read_csv(map(bytes.decode, file)): | |
200 | if row['service_id'] not in services: | |
201 | services[row['service_id']] = BusService(row['service_id']) | |
202 | route = routes_per_id[row['route_id']] | |
203 | trip = BusTrip( | |
204 | reference = row['trip_id'], | |
205 | route = route, | |
206 | service = services[row['service_id']], | |
71
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
207 | 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
|
208 | block_id = row['block_id'], |
30 | 209 | ) |
210 | route.trips.add(trip) | |
71
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
211 | if trip.name in all_trips: |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
212 | print('Trip %s already exists' % trip.name) |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
213 | else: |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
214 | all_trips[trip.name] = trip |
30 | 215 | print('%d ajoa' % len(all_trips), file = stderr) |
0 | 216 | |
30 | 217 | def read_date(teksti): |
218 | return date(int(teksti[:4]), int(teksti[4:6]), int(teksti[6:])) | |
219 | ||
220 | def read_time(teksti): | |
221 | tunti, minuutti, sekunti = map(int, teksti.split(':')) | |
222 | return timedelta(hours = tunti, minutes = minuutti, seconds = sekunti) | |
223 | ||
224 | print('Ladataan päiväykset... ', file = stderr, flush = True) | |
225 | ||
226 | viimeinen_käyttöpäivä = date.today() | |
29 | 227 | |
30 | 228 | def date_range(start_date, end_date, *, include_end = False): |
229 | ''' Generates date from start_date to end_date. If include_end is True, then end_date will be yielded. ''' | |
230 | current_date = start_date | |
231 | while current_date < end_date: | |
232 | yield current_date | |
233 | current_date += timedelta(1) | |
234 | if include_end: | |
235 | yield end_date | |
29 | 236 | |
30 | 237 | def add_day_to_service(service_name, day): |
238 | try: | |
239 | service = services[service_name] | |
240 | except KeyError: | |
241 | return | |
242 | else: | |
243 | service.dates.add(day) | |
244 | if day not in services_for_day: | |
245 | services_for_day[day] = set() | |
246 | services_for_day[day].add(service) | |
247 | global viimeinen_käyttöpäivä | |
248 | viimeinen_käyttöpäivä = max(day, viimeinen_käyttöpäivä) | |
29 | 249 | |
30 | 250 | def filter_day(row, day): |
251 | day_names = ['monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday', 'sunday'] | |
252 | return int(row[day_names[day.isoweekday() - 1]]) | |
0 | 253 | |
30 | 254 | with gtfs_zip.open('calendar.txt') as file: |
255 | for row in read_csv(map(bytes.decode, file)): | |
256 | for day in date_range(read_date(row['start_date']), read_date(row['end_date']), include_end = True): | |
257 | if filter_day(row, day): | |
258 | add_day_to_service(service_name = row['service_id'], day = day) | |
259 | ||
260 | with gtfs_zip.open('calendar_dates.txt') as file: | |
261 | for row in read_csv(map(bytes.decode, file)): | |
262 | add_day_to_service(service_name = row['service_id'], day = read_date(row['date'])) | |
263 | ||
264 | def services_available_at(day): | |
265 | for service in services.values(): | |
266 | if day in service.dates: | |
267 | yield service | |
268 | ||
269 | print('Ladataan pysäkit... ', file = stderr, end = '', flush = True) | |
270 | with gtfs_zip.open('stops.txt') as file: | |
271 | for row in read_csv(map(bytes.decode, file)): | |
272 | location = Sijainti(float(row['stop_lat']), float(row['stop_lon'])) | |
273 | stop = BusStop( | |
274 | reference = row['stop_id'], | |
275 | name = row['stop_name'], | |
276 | location = location, | |
71
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
277 | code = row.get('stop_code', row['stop_id']), |
30 | 278 | ) |
279 | bus_stops[stop.reference] = stop | |
71
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
280 | if profile['regions']['use-regions']: |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
281 | 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
|
282 | 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
|
283 | try: |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
284 | bus_stops[stop_reference].region = region |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
285 | except KeyError: |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
286 | pass |
81 | 287 | for bus_stop in bus_stops.values(): |
288 | if not hasattr(bus_stop, 'region'): | |
289 | bus_stop.region = None | |
30 | 290 | print('%d pysäkkiä' % len(bus_stops), file = stderr) |
0 | 291 | |
21 | 292 | |
30 | 293 | class BusStopCluster: |
294 | def __init__(self): | |
295 | self.stops = set() | |
296 | self._center = None | |
297 | self.name = None | |
298 | @property | |
299 | def url_name(self): | |
300 | return self.name.lower().replace('(', '').replace(')', '').replace(' ', '-') | |
301 | def add_stop(self, stop): | |
302 | assert not stop.cluster | |
303 | stop.cluster = self | |
304 | self.stops.add(stop) | |
305 | self._center = None | |
306 | @property | |
307 | def center(self): | |
308 | if not self._center: | |
309 | if self.stops: | |
310 | from statistics import median | |
311 | pointtype = type(next(iter(self.stops)).location) | |
312 | self._center = pointtype( | |
313 | median(stop.location.x for stop in self.stops), | |
314 | median(stop.location.y for stop in self.stops), | |
315 | ) | |
316 | else: | |
317 | raise ValueError('an empty cluster has no center point') | |
318 | return self._center | |
319 | def merge(self, other): | |
320 | for bus_stop in other.stops: | |
321 | bus_stop.cluster = self | |
322 | self.stops |= other.stops | |
323 | other.stops = set() | |
324 | other._center = None | |
31
60045b362d71
- Ajovuoroa ei enää esitetä kahdessa välilehdessä vaan puukuvaimessa
Teemu Piippo <teemu@hecknology.net>
parents:
30
diff
changeset
|
325 | def schedule(self, *, max_amount = 50): |
30 | 326 | result = [] |
327 | 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
|
328 | result += stop.schedule(max_amount = max_amount) |
30 | 329 | result.sort(key = lambda schedule_entry: schedule_entry['time']) |
330 | return result[:max_amount] | |
15
a22cdf28930f
Lisätty bussipysäkkien ryhmittely
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
331 | |
30 | 332 | from collections import defaultdict |
333 | bus_stops_by_name = defaultdict(set) | |
334 | for bus_stop in bus_stops.values(): | |
335 | bus_stops_by_name[bus_stop.name].add(bus_stop) | |
336 | 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
|
337 | |
30 | 338 | # ryhmittele bus_stops nimen mukaan |
41 | 339 | global all_clusters |
30 | 340 | all_clusters = [] |
341 | def cluster_bus_stops(): | |
342 | sorted_bus_stops = sorted(bus_stops.values(), key = lambda bus_stop: bus_stop.name) | |
343 | for bus_stop in sorted_bus_stops: | |
344 | if not bus_stop.cluster: | |
345 | stops_to_cluster = {bus_stop} | |
346 | # etsi pysäkin samannimiset vastaparit | |
347 | for pair_candidate in bus_stops_by_name[bus_stop.name]: | |
348 | distance = pair_candidate.location.etäisyys(bus_stop.location) | |
349 | if pair_candidate is not bus_stop and distance <= 0.4: | |
350 | stops_to_cluster.add(pair_candidate) | |
351 | for stop_to_cluster in stops_to_cluster: | |
352 | if stop_to_cluster.cluster: | |
353 | cluster = stop_to_cluster.cluster | |
354 | break | |
355 | else: | |
356 | cluster = BusStopCluster() | |
357 | all_clusters.append(cluster) | |
358 | for stop_to_cluster in stops_to_cluster: | |
359 | if not stop_to_cluster.cluster: | |
360 | cluster.add_stop(stop_to_cluster) | |
361 | # Merkitse muistiin pysäkkien vastaparit käyttäen hyväksi tämänhetkistä ryhmittelytietoa | |
362 | for bus_stop in bus_stops.values(): | |
363 | if bus_stop.cluster: | |
364 | bus_stop.pairs = bus_stop.cluster.stops - {bus_stop} | |
365 | # Ryhmitä ne bus_stops, joilla ei ollut omaa vastaparia, muiden pysäkkien kanssa | |
366 | for bus_stop in sorted_bus_stops: | |
367 | if len(bus_stop.cluster.stops) == 1: | |
368 | possibilities = set() | |
369 | for cluster in all_clusters: | |
370 | if cluster is not bus_stop.cluster: | |
371 | distance = cluster.center.etäisyys(bus_stop.location) | |
372 | if distance <= 0.4: | |
373 | possibilities.add((distance, cluster)) | |
374 | if possibilities: | |
375 | best = min(possibilities)[1] | |
376 | all_clusters.remove(bus_stop.cluster) | |
377 | best.merge(bus_stop.cluster) | |
15
a22cdf28930f
Lisätty bussipysäkkien ryhmittely
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
378 | |
30 | 379 | def shared_elements_in_n_sets(sets): |
380 | from itertools import combinations | |
381 | result = set() | |
382 | for pair in combinations(sets, 2): | |
383 | result |= pair[0] & pair[1] | |
384 | return result | |
15
a22cdf28930f
Lisätty bussipysäkkien ryhmittely
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
385 | |
30 | 386 | def name_clusters(): |
387 | from collections import defaultdict | |
388 | from pprint import pprint | |
389 | clusters_per_name = defaultdict(set) | |
390 | for cluster in all_clusters: | |
391 | name_representing_stop = min((len(stop.reference), stop.reference, stop) for stop in cluster.stops)[2] | |
392 | clusters_per_name[name_representing_stop.name].add(cluster) | |
393 | for name, clusters in clusters_per_name.items(): | |
394 | if len(clusters) == 1: | |
395 | # Ryhmä on ainoa jolla on varaus tälle nimelle. Sen kuin vaan. | |
396 | next(iter(clusters)).name = name | |
397 | else: | |
71
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
398 | if profile['regions']['use-regions']: |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
399 | # Olisiko kaikki klusterit eri alueilla? |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
400 | 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
|
401 | # 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
|
402 | proposal = { |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
403 | 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
|
404 | for cluster in clusters |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
405 | } |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
406 | # 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
|
407 | # 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
|
408 | # 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
|
409 | 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
|
410 | for cluster, unique_areas in proposal.items(): |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
411 | individual_cluster_name = name |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
412 | if unique_areas: |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
413 | individual_cluster_name += ' (' + min(unique_areas) + ')' |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
414 | cluster.name = individual_cluster_name |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
415 | break |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
416 | # Typerä reunatapaus. Indeksoidaan numeroin... |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
417 | for n, (_, cluster) in enumerate(sorted( |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
418 | min((stop.reference.lower(), cluster) for stop in cluster.stops) |
30 | 419 | for cluster in clusters |
71
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
420 | ), 1): |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
421 | individual_cluster_name = name + '-' + str(n) |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
422 | cluster.name = individual_cluster_name |
30 | 423 | |
424 | print('Ryhmitellään pysäkit...') | |
425 | cluster_bus_stops() | |
426 | name_clusters() | |
427 | ||
428 | for cluster in all_clusters: | |
429 | if cluster.url_name in clusters_by_name: | |
430 | 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
|
431 | else: |
30 | 432 | 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
|
433 | |
30 | 434 | print('Ladataan aikataulut... ', end = '', flush = True, file = stderr) |
435 | with gtfs_zip.open('stop_times.txt') as file: | |
436 | row_count = sum(line.count(b'\n') for line in file) | |
437 | with gtfs_zip.open('stop_times.txt') as file: | |
438 | progress = 0 | |
439 | for row in read_csv(map(bytes.decode, file)): | |
80 | 440 | if int(row.get('pickup_type', '') or '0') and int(row.get('drop_off_type', '') or '0'): |
71
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
441 | continue |
30 | 442 | trip = all_trips[transform_trip_reference(row['trip_id'])] |
443 | arrival_time = read_time(row['arrival_time']) | |
444 | departure_time = read_time(row['departure_time']) | |
445 | stop = bus_stops[row['stop_id']] | |
71
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
446 | traveled_distance = float(row.get('shape_dist_traveled', 1)) * float(profile['metrics']['shape-modifier']) |
30 | 447 | trip.schedule.append(BusHalt(arrival_time, departure_time, stop, trip, traveled_distance)) |
448 | stop.involved_trips.add(trip) | |
449 | progress += 1 | |
450 | if progress % 1000 == 0: | |
451 | print('\rLadataan aikataulut... %.1f%%' % (progress * 100 / row_count), end = ' ', file = stderr) | |
452 | print('\rLadataan aikataulut... ladattu', file = stderr) | |
453 | ||
454 | for trip in all_trips.values(): | |
455 | from busroute import simplify_name | |
456 | schedule = trip.concise_schedule() | |
457 | try: | |
458 | trip.from_place = simplify_name(schedule[0]) | |
459 | trip.to_place = simplify_name(schedule[-1]) | |
460 | except IndexError: | |
461 | trip.from_place = '' | |
462 | trip.to_place = '' | |
28
670ffa424ded
Bussipysäkit tallentavat ajovuoronsa välimuistiin suoritusajan nopeuttamiseksi
Teemu Piippo <teemu@hecknology.net>
parents:
26
diff
changeset
|
463 | |
30 | 464 | for route in routes.values(): |
465 | from collections import Counter | |
466 | from busroute import simplify_name | |
467 | tally = Counter() | |
468 | for trip in route.trips: | |
469 | schedule = trip.concise_schedule() | |
470 | places = set(schedule) | |
471 | do_add = True | |
472 | assert type(schedule) is list | |
473 | for candidate in tally: | |
474 | if places.issubset(set(candidate)): | |
475 | do_add = False | |
476 | tally.update({tuple(candidate)}) | |
477 | if do_add: | |
478 | tally.update({tuple(schedule)}) | |
479 | try: | |
480 | most_common_route = tally.most_common(1)[0][0] | |
481 | route.description = simplify_name(most_common_route[0]) + ' - ' + simplify_name(most_common_route[-1]) | |
482 | except: | |
483 | route.description = '' | |
71
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
484 | 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
|
485 | |
72 | 486 | if 'compatibility' in profile and profile['compatibility'].get('fix-destination-times', False): |
487 | # Fölin datassa on jotain tosi kummaa. Ilmeisesti ajovuoron viimeisen pysähdyksen saapumisaika on ihan täysin | |
488 | # väärin. Arvaan että se on seuraavan lähdön aika, mutta joka tapauksessa se on väärin. | |
489 | # Arvataan mikä se todellinen saapumisaika on. Se ei voi mennä kauhean paljon pahemmin vikaan kuin alkuperäinen | |
490 | # väärin oleva data. | |
491 | for trip in all_trips.values(): | |
492 | if len(trip.schedule) >= 2: | |
493 | bus_speed_coefficient = 750 # metriä minuutissa | |
494 | last_leg_distance = trip.schedule[-1].traveled_distance - trip.schedule[-2].traveled_distance | |
495 | 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
|
496 | |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
497 | global trips_by_vehicle_info |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
498 | trips_by_vehicle_info = {} |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
499 | for trip in all_trips.values(): |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
500 | trips_by_vehicle_info[(trip.block_id, trip.schedule[0].arrival_time)] = trip |
30 | 501 | |
502 | if __name__ == '__main__': | |
503 | profile.read('profiles/föli.ini') | |
71
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
504 | load_buses('gtfs.zip') |