Sat, 22 Sep 2018 00:08:43 +0300
split up Ruissalo
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: |
114 | 13 | def __init__(self, reference, route, service, length, block_id, shape): |
71
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) |
114 | 18 | self.shape = str(shape) |
0 | 19 | def __repr__(self): |
24
e6bdb9c54096
Yhtenäistetty ohjelmakoodin kieli englanniksi
Teemu Piippo <teemu@hecknology.net>
parents:
23
diff
changeset
|
20 | return 'all_trips[%r]' % self.name |
e6bdb9c54096
Yhtenäistetty ohjelmakoodin kieli englanniksi
Teemu Piippo <teemu@hecknology.net>
parents:
23
diff
changeset
|
21 | def contains_stop(self, stop): |
e6bdb9c54096
Yhtenäistetty ohjelmakoodin kieli englanniksi
Teemu Piippo <teemu@hecknology.net>
parents:
23
diff
changeset
|
22 | for halt in self.schedule: |
e6bdb9c54096
Yhtenäistetty ohjelmakoodin kieli englanniksi
Teemu Piippo <teemu@hecknology.net>
parents:
23
diff
changeset
|
23 | if halt.stop is stop: |
e6bdb9c54096
Yhtenäistetty ohjelmakoodin kieli englanniksi
Teemu Piippo <teemu@hecknology.net>
parents:
23
diff
changeset
|
24 | return halt |
0 | 25 | else: |
26 | return None | |
24
e6bdb9c54096
Yhtenäistetty ohjelmakoodin kieli englanniksi
Teemu Piippo <teemu@hecknology.net>
parents:
23
diff
changeset
|
27 | def is_served_at(self, day): |
0 | 28 | try: |
24
e6bdb9c54096
Yhtenäistetty ohjelmakoodin kieli englanniksi
Teemu Piippo <teemu@hecknology.net>
parents:
23
diff
changeset
|
29 | return self.service in services_for_day[day] |
0 | 30 | except KeyError: |
31 | return False | |
24
e6bdb9c54096
Yhtenäistetty ohjelmakoodin kieli englanniksi
Teemu Piippo <teemu@hecknology.net>
parents:
23
diff
changeset
|
32 | def concise_schedule(self, starting_stop = None): |
72 | 33 | if starting_stop and starting_stop in self.schedule: |
34 | schedule = copy(self.schedule) | |
35 | schedule = schedule[schedule.index(starting_stop):] | |
36 | else: | |
37 | schedule = self.schedule | |
71
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
38 | if profile['regions']['use-regions']: |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
39 | used_areas = set() |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
40 | result = [] |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
41 | for halt in schedule: |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
42 | stop = halt.stop |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
43 | 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
|
44 | used_areas.add(stop.region) |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
45 | result.append(stop.region) |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
46 | return result |
5 | 47 | else: |
72 | 48 | return [halt.stop.name for halt in schedule] |
0 | 49 | |
24
e6bdb9c54096
Yhtenäistetty ohjelmakoodin kieli englanniksi
Teemu Piippo <teemu@hecknology.net>
parents:
23
diff
changeset
|
50 | class BusRoute: |
e6bdb9c54096
Yhtenäistetty ohjelmakoodin kieli englanniksi
Teemu Piippo <teemu@hecknology.net>
parents:
23
diff
changeset
|
51 | def __init__(self, entry): |
e6bdb9c54096
Yhtenäistetty ohjelmakoodin kieli englanniksi
Teemu Piippo <teemu@hecknology.net>
parents:
23
diff
changeset
|
52 | self.id = entry['route_id'] |
e6bdb9c54096
Yhtenäistetty ohjelmakoodin kieli englanniksi
Teemu Piippo <teemu@hecknology.net>
parents:
23
diff
changeset
|
53 | 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
|
54 | self.trips = set() |
90 | 55 | self.service = None |
0 | 56 | def __repr__(self): |
24
e6bdb9c54096
Yhtenäistetty ohjelmakoodin kieli englanniksi
Teemu Piippo <teemu@hecknology.net>
parents:
23
diff
changeset
|
57 | return 'routes[%r]' % self.reference |
0 | 58 | |
24
e6bdb9c54096
Yhtenäistetty ohjelmakoodin kieli englanniksi
Teemu Piippo <teemu@hecknology.net>
parents:
23
diff
changeset
|
59 | class BusService: |
e6bdb9c54096
Yhtenäistetty ohjelmakoodin kieli englanniksi
Teemu Piippo <teemu@hecknology.net>
parents:
23
diff
changeset
|
60 | def __init__(self, reference): |
e6bdb9c54096
Yhtenäistetty ohjelmakoodin kieli englanniksi
Teemu Piippo <teemu@hecknology.net>
parents:
23
diff
changeset
|
61 | self.reference = reference |
e6bdb9c54096
Yhtenäistetty ohjelmakoodin kieli englanniksi
Teemu Piippo <teemu@hecknology.net>
parents:
23
diff
changeset
|
62 | self.dates = set() |
0 | 63 | def __repr__(self): |
24
e6bdb9c54096
Yhtenäistetty ohjelmakoodin kieli englanniksi
Teemu Piippo <teemu@hecknology.net>
parents:
23
diff
changeset
|
64 | return 'services[%r]' % self.reference |
0 | 65 | |
24
e6bdb9c54096
Yhtenäistetty ohjelmakoodin kieli englanniksi
Teemu Piippo <teemu@hecknology.net>
parents:
23
diff
changeset
|
66 | class BusStop: |
29 | 67 | def __init__(self, reference, name, location, code = None): |
24
e6bdb9c54096
Yhtenäistetty ohjelmakoodin kieli englanniksi
Teemu Piippo <teemu@hecknology.net>
parents:
23
diff
changeset
|
68 | self.reference, self.name, self.location = reference, name, location |
29 | 69 | self.code = code or reference |
15
a22cdf28930f
Lisätty bussipysäkkien ryhmittely
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
70 | self.cluster = None |
a22cdf28930f
Lisätty bussipysäkkien ryhmittely
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
71 | 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
|
72 | self.involved_trips = set() |
90 | 73 | self.services = set() |
0 | 74 | def __repr__(self): |
24
e6bdb9c54096
Yhtenäistetty ohjelmakoodin kieli englanniksi
Teemu Piippo <teemu@hecknology.net>
parents:
23
diff
changeset
|
75 | 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
|
76 | def schedule(self, *, max_amount = 50, arrivals = False): |
0 | 77 | ''' |
78 | 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
|
79 | lasketa. Palauttaa pysähdykset listana jossa alkiot ovat muotoa (aika, halt), |
0 | 80 | jossa: |
81 | - `aika` on saapumishetki muotoa datetime ja | |
24
e6bdb9c54096
Yhtenäistetty ohjelmakoodin kieli englanniksi
Teemu Piippo <teemu@hecknology.net>
parents:
23
diff
changeset
|
82 | - `halt` on vastaava BusHalt olio. |
0 | 83 | |
84 | Mikäli pysäkille ei ole määrätty riittävästi pysähdyksiä kalenterissa, tuloslista | |
85 | jää alimittaiseksi, mahdollisesti jopa tyhjäksi. | |
86 | ''' | |
17
fa3c822859b5
Refaktorioitu aikatauluhaku
Teemu Piippo <teemu@hecknology.net>
parents:
15
diff
changeset
|
87 | result = [] |
20
3199e289ae62
- Sivusto hieman edustuksellisempi
Teemu Piippo <teemu@hecknology.net>
parents:
19
diff
changeset
|
88 | # -1 päivää yövuoroja varten |
24
e6bdb9c54096
Yhtenäistetty ohjelmakoodin kieli englanniksi
Teemu Piippo <teemu@hecknology.net>
parents:
23
diff
changeset
|
89 | date = today() - timedelta(days = 1) |
0 | 90 | # Niin kauan kuin aikatauluja ei ole vielä tarpeeksi, |
17
fa3c822859b5
Refaktorioitu aikatauluhaku
Teemu Piippo <teemu@hecknology.net>
parents:
15
diff
changeset
|
91 | while len(result) < max_amount: |
0 | 92 | try: |
93 | # 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
|
94 | result += self.schedule_for_day(date, arrivals = arrivals) |
17
fa3c822859b5
Refaktorioitu aikatauluhaku
Teemu Piippo <teemu@hecknology.net>
parents:
15
diff
changeset
|
95 | except ValueError: |
0 | 96 | # paitsi jos mentiin kalenterin ulkopuolelle, jolloin lopetetaan, |
97 | break | |
98 | # ja siirry seuraavaan päivään. | |
17
fa3c822859b5
Refaktorioitu aikatauluhaku
Teemu Piippo <teemu@hecknology.net>
parents:
15
diff
changeset
|
99 | date += timedelta(1) |
0 | 100 | # Typistä lopputulos haluttuun tulosmäärään. |
17
fa3c822859b5
Refaktorioitu aikatauluhaku
Teemu Piippo <teemu@hecknology.net>
parents:
15
diff
changeset
|
101 | return result[:max_amount] |
109 | 102 | def schedule_for_day(self, date, *, arrivals = False, allow_gone = False): |
17
fa3c822859b5
Refaktorioitu aikatauluhaku
Teemu Piippo <teemu@hecknology.net>
parents:
15
diff
changeset
|
103 | ''' |
fa3c822859b5
Refaktorioitu aikatauluhaku
Teemu Piippo <teemu@hecknology.net>
parents:
15
diff
changeset
|
104 | Hakee pysäkin aikataulut tiettynä päivänä. |
fa3c822859b5
Refaktorioitu aikatauluhaku
Teemu Piippo <teemu@hecknology.net>
parents:
15
diff
changeset
|
105 | ''' |
fa3c822859b5
Refaktorioitu aikatauluhaku
Teemu Piippo <teemu@hecknology.net>
parents:
15
diff
changeset
|
106 | # 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
|
107 | # palautettaisiin tyhjä result, niin algoritmi jatkaisi etsintää loputtomiin. |
17
fa3c822859b5
Refaktorioitu aikatauluhaku
Teemu Piippo <teemu@hecknology.net>
parents:
15
diff
changeset
|
108 | if date > viimeinen_käyttöpäivä: |
fa3c822859b5
Refaktorioitu aikatauluhaku
Teemu Piippo <teemu@hecknology.net>
parents:
15
diff
changeset
|
109 | 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
|
110 | result = [] |
fa3c822859b5
Refaktorioitu aikatauluhaku
Teemu Piippo <teemu@hecknology.net>
parents:
15
diff
changeset
|
111 | # Jokaiselle ajovuorolle, |
28
670ffa424ded
Bussipysäkit tallentavat ajovuoronsa välimuistiin suoritusajan nopeuttamiseksi
Teemu Piippo <teemu@hecknology.net>
parents:
26
diff
changeset
|
112 | for trip in self.involved_trips: |
17
fa3c822859b5
Refaktorioitu aikatauluhaku
Teemu Piippo <teemu@hecknology.net>
parents:
15
diff
changeset
|
113 | # 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
|
114 | if trip.is_served_at(date): |
e6bdb9c54096
Yhtenäistetty ohjelmakoodin kieli englanniksi
Teemu Piippo <teemu@hecknology.net>
parents:
23
diff
changeset
|
115 | # 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
|
116 | # päätepysäkille, |
24
e6bdb9c54096
Yhtenäistetty ohjelmakoodin kieli englanniksi
Teemu Piippo <teemu@hecknology.net>
parents:
23
diff
changeset
|
117 | 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
|
118 | 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
|
119 | # ja jos tämä halt on tulevaisuudessa, |
75 | 120 | stop_time = datetime.combine(date, time()) + stop.departure_time |
109 | 121 | if allow_gone or (stop_time + timedelta(minutes = 1) >= now()): |
24
e6bdb9c54096
Yhtenäistetty ohjelmakoodin kieli englanniksi
Teemu Piippo <teemu@hecknology.net>
parents:
23
diff
changeset
|
122 | # lisää halt listaan. |
18 | 123 | result.append({ |
77 | 124 | 'date': date, |
125 | 'offset': stop.departure_time, | |
18 | 126 | 'time': stop_time, |
127 | 'trip': trip, | |
128 | 'stop': stop, | |
129 | }) | |
17
fa3c822859b5
Refaktorioitu aikatauluhaku
Teemu Piippo <teemu@hecknology.net>
parents:
15
diff
changeset
|
130 | # Lajittele lopputulos saapumisajan mukaan. |
18 | 131 | result.sort(key = lambda schedule_entry: schedule_entry['time']) |
17
fa3c822859b5
Refaktorioitu aikatauluhaku
Teemu Piippo <teemu@hecknology.net>
parents:
15
diff
changeset
|
132 | return result |
90 | 133 | @property |
134 | def typename(self): | |
135 | if self.services == {'train'}: | |
136 | return 'train-station' | |
137 | elif self.services == {'tram'}: | |
138 | return 'tram-stop' | |
139 | elif self.services == {'ferry'}: | |
140 | return 'ferry-terminal' | |
141 | else: | |
142 | return 'bus-stop' | |
0 | 143 | |
24
e6bdb9c54096
Yhtenäistetty ohjelmakoodin kieli englanniksi
Teemu Piippo <teemu@hecknology.net>
parents:
23
diff
changeset
|
144 | class BusHalt: |
e6bdb9c54096
Yhtenäistetty ohjelmakoodin kieli englanniksi
Teemu Piippo <teemu@hecknology.net>
parents:
23
diff
changeset
|
145 | 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
|
146 | 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
|
147 | stop, trip |
e6bdb9c54096
Yhtenäistetty ohjelmakoodin kieli englanniksi
Teemu Piippo <teemu@hecknology.net>
parents:
23
diff
changeset
|
148 | self.traveled_distance = traveled_distance |
23 | 149 | @property |
29 | 150 | def is_arrival(self): |
71
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
151 | if profile['regions']['use-regions']: |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
152 | if not hasattr(self, 'cachedIsArrival'): |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
153 | if self.stop.region: |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
154 | iterator = iter(self.trip.schedule) |
29 | 155 | stop = next(iterator) |
71
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
156 | while stop is not self: |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
157 | stop = next(iterator) |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
158 | for stop in iterator: |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
159 | if stop.stop.region != self.stop.region: |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
160 | self.cachedIsArrival = False |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
161 | break |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
162 | else: |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
163 | self.cachedIsArrival = True |
29 | 164 | else: |
71
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
165 | self.cachedIsArrival = False |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
166 | return self.cachedIsArrival |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
167 | else: |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
168 | return self == self.trip.schedule[-1] |
94
e27c18f080d1
added an interesting trip portal
Teemu Piippo <teemu@hecknology.net>
parents:
93
diff
changeset
|
169 | def departure_datetime(self, date): |
e27c18f080d1
added an interesting trip portal
Teemu Piippo <teemu@hecknology.net>
parents:
93
diff
changeset
|
170 | import datetime |
e27c18f080d1
added an interesting trip portal
Teemu Piippo <teemu@hecknology.net>
parents:
93
diff
changeset
|
171 | return datetime.datetime.combine(date, datetime.time()) + self.departure_time |
e27c18f080d1
added an interesting trip portal
Teemu Piippo <teemu@hecknology.net>
parents:
93
diff
changeset
|
172 | def arrival_datetime(self, date): |
e27c18f080d1
added an interesting trip portal
Teemu Piippo <teemu@hecknology.net>
parents:
93
diff
changeset
|
173 | import datetime |
e27c18f080d1
added an interesting trip portal
Teemu Piippo <teemu@hecknology.net>
parents:
93
diff
changeset
|
174 | return datetime.datetime.combine(date, datetime.time()) + self.arrival_time |
0 | 175 | def __repr__(self): |
24
e6bdb9c54096
Yhtenäistetty ohjelmakoodin kieli englanniksi
Teemu Piippo <teemu@hecknology.net>
parents:
23
diff
changeset
|
176 | 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
|
177 | def sign(self, long = False): |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
178 | from busroute import reduce_schedule |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
179 | return reduce_schedule( |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
180 | route = self.trip.concise_schedule(self), |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
181 | 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
|
182 | long = long, |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
183 | ) |
0 | 184 | |
109 | 185 | class BusStopCluster: |
186 | def __init__(self): | |
187 | self.stops = set() | |
188 | self.cached_center = None | |
189 | self.name = None | |
190 | @property | |
191 | def url_name(self): | |
192 | return self.name.lower().replace('(', '').replace(')', '').replace(' ', '-') | |
193 | def add_stop(self, stop): | |
194 | assert not stop.cluster | |
195 | stop.cluster = self | |
196 | self.stops.add(stop) | |
197 | self.cached_center = None | |
198 | @property | |
199 | def center(self): | |
200 | if not self.cached_center: | |
201 | if self.stops: | |
202 | from statistics import median | |
203 | pointtype = type(next(iter(self.stops)).location) | |
204 | self.cached_center = pointtype( | |
205 | median(stop.location.x for stop in self.stops), | |
206 | median(stop.location.y for stop in self.stops), | |
207 | ) | |
208 | else: | |
209 | raise ValueError('an empty cluster has no center point') | |
210 | return self.cached_center | |
211 | def merge(self, other): | |
212 | for bus_stop in other.stops: | |
213 | bus_stop.cluster = self | |
214 | self.stops |= other.stops | |
215 | other.stops = set() | |
216 | other.cached_center = None | |
217 | def schedule(self, *, max_amount = 50): | |
218 | result = [] | |
219 | for stop in self.stops: | |
220 | result += stop.schedule(max_amount = max_amount) | |
221 | result.sort(key = lambda schedule_entry: schedule_entry['time']) | |
222 | return result[:max_amount] | |
114 | 223 | def __lt__(self, other): |
224 | return (self.name and other.name) and (self.name < other.name) or (id(self) < id(other)) | |
109 | 225 | |
226 | class CustomBusStopCluster(BusStopCluster): | |
227 | def __init__(self, *, name, stops): | |
228 | super().__init__() | |
229 | self.name = name | |
230 | self.stops = stops | |
231 | def add_stop(self, stop): | |
232 | return NotImplemented | |
233 | @property | |
234 | def url_name(self): | |
235 | from urllib.request import quote | |
236 | return 'custom?stops=' + ';'.join(stop.code for stop in self.stops) + '&name=' + quote(self.name) | |
237 | ||
114 | 238 | from collections import defaultdict |
24
e6bdb9c54096
Yhtenäistetty ohjelmakoodin kieli englanniksi
Teemu Piippo <teemu@hecknology.net>
parents:
23
diff
changeset
|
239 | routes = {} |
e6bdb9c54096
Yhtenäistetty ohjelmakoodin kieli englanniksi
Teemu Piippo <teemu@hecknology.net>
parents:
23
diff
changeset
|
240 | routes_per_id = {} |
e6bdb9c54096
Yhtenäistetty ohjelmakoodin kieli englanniksi
Teemu Piippo <teemu@hecknology.net>
parents:
23
diff
changeset
|
241 | all_trips = {} |
e6bdb9c54096
Yhtenäistetty ohjelmakoodin kieli englanniksi
Teemu Piippo <teemu@hecknology.net>
parents:
23
diff
changeset
|
242 | services = {} |
e6bdb9c54096
Yhtenäistetty ohjelmakoodin kieli englanniksi
Teemu Piippo <teemu@hecknology.net>
parents:
23
diff
changeset
|
243 | bus_stops = {} |
15
a22cdf28930f
Lisätty bussipysäkkien ryhmittely
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
244 | all_clusters = set() |
30 | 245 | viimeinen_käyttöpäivä = None |
246 | clusters_by_name = {} | |
24
e6bdb9c54096
Yhtenäistetty ohjelmakoodin kieli englanniksi
Teemu Piippo <teemu@hecknology.net>
parents:
23
diff
changeset
|
247 | services_for_day = {} |
114 | 248 | shapes = defaultdict(list) |
0 | 249 | |
93 | 250 | def load_buses(gtfs_zip_path): |
30 | 251 | global viimeinen_käyttöpäivä |
252 | from zipfile import ZipFile | |
253 | with ZipFile(gtfs_zip_path) as gtfs_zip: | |
88
3b86597c5a88
major update, moved the map to an osm patch
Teemu Piippo <teemu@hecknology.net>
parents:
81
diff
changeset
|
254 | print('Loading routes... ', file = stderr, end = '', flush = True) |
30 | 255 | with gtfs_zip.open('routes.txt') as file: |
256 | for row in read_csv(map(bytes.decode, file)): | |
257 | route = BusRoute(row) | |
258 | routes[route.reference] = route | |
259 | routes_per_id[route.id] = route | |
88
3b86597c5a88
major update, moved the map to an osm patch
Teemu Piippo <teemu@hecknology.net>
parents:
81
diff
changeset
|
260 | print('%d routes' % len(routes), file = stderr) |
93 | 261 | # Add services |
262 | import re | |
263 | service_patterns = {} | |
264 | if 'service-patterns' in profile: | |
265 | for service_type, regexps in profile['service-patterns'].items(): | |
266 | service_patterns[service_type] = {re.compile(regexp) for regexp in regexps.split('@')} | |
267 | if 'services' in profile and profile['services'].get('default-service'): | |
268 | print('Tagging services...', end = '') | |
269 | for route in routes.values(): | |
270 | for service_type, regexps in service_patterns.items(): | |
271 | if any(regexp.match(route.reference) for regexp in regexps): | |
272 | route.service = service_type | |
273 | break | |
274 | else: | |
275 | route.service = profile['services']['default-service'] | |
276 | print('') | |
88
3b86597c5a88
major update, moved the map to an osm patch
Teemu Piippo <teemu@hecknology.net>
parents:
81
diff
changeset
|
277 | print('Loading trips... ', file = stderr, end = '', flush = True) |
30 | 278 | shape_distances = {} |
71
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
279 | try: |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
280 | 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
|
281 | for row in read_csv(map(bytes.decode, file)): |
114 | 282 | list.append(shapes[row['shape_id']], (row['shape_pt_lat'], row['shape_pt_lon'])) |
71
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
283 | 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
|
284 | except KeyError: |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
285 | pass |
29 | 286 | |
30 | 287 | with gtfs_zip.open('trips.txt') as file: |
288 | for row in read_csv(map(bytes.decode, file)): | |
289 | if row['service_id'] not in services: | |
290 | services[row['service_id']] = BusService(row['service_id']) | |
291 | route = routes_per_id[row['route_id']] | |
292 | trip = BusTrip( | |
293 | reference = row['trip_id'], | |
294 | route = route, | |
295 | service = services[row['service_id']], | |
71
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
296 | length = shape_distances.get(row.get('shape_id'), 1) * float(profile['metrics']['shape-modifier']), |
90 | 297 | block_id = row.get('block_id') or row['service_id'], |
114 | 298 | shape = row.get('shape_id') |
30 | 299 | ) |
300 | route.trips.add(trip) | |
71
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
301 | if trip.name in all_trips: |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
302 | print('Trip %s already exists' % trip.name) |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
303 | else: |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
304 | all_trips[trip.name] = trip |
88
3b86597c5a88
major update, moved the map to an osm patch
Teemu Piippo <teemu@hecknology.net>
parents:
81
diff
changeset
|
305 | print('%d trips' % len(all_trips), file = stderr) |
0 | 306 | |
30 | 307 | def read_date(teksti): |
308 | return date(int(teksti[:4]), int(teksti[4:6]), int(teksti[6:])) | |
309 | ||
310 | def read_time(teksti): | |
88
3b86597c5a88
major update, moved the map to an osm patch
Teemu Piippo <teemu@hecknology.net>
parents:
81
diff
changeset
|
311 | hour, minute, second = map(int, teksti.split(':')) |
3b86597c5a88
major update, moved the map to an osm patch
Teemu Piippo <teemu@hecknology.net>
parents:
81
diff
changeset
|
312 | return timedelta(hours = hour, minutes = minute, seconds = second) |
30 | 313 | |
93 | 314 | print('Loading dates... ', file = stderr, flush = True) |
30 | 315 | viimeinen_käyttöpäivä = date.today() |
29 | 316 | |
30 | 317 | def date_range(start_date, end_date, *, include_end = False): |
318 | ''' Generates date from start_date to end_date. If include_end is True, then end_date will be yielded. ''' | |
319 | current_date = start_date | |
320 | while current_date < end_date: | |
321 | yield current_date | |
322 | current_date += timedelta(1) | |
323 | if include_end: | |
324 | yield end_date | |
29 | 325 | |
30 | 326 | def add_day_to_service(service_name, day): |
327 | try: | |
328 | service = services[service_name] | |
329 | except KeyError: | |
330 | return | |
331 | else: | |
332 | service.dates.add(day) | |
333 | if day not in services_for_day: | |
334 | services_for_day[day] = set() | |
335 | services_for_day[day].add(service) | |
336 | global viimeinen_käyttöpäivä | |
337 | viimeinen_käyttöpäivä = max(day, viimeinen_käyttöpäivä) | |
29 | 338 | |
30 | 339 | def filter_day(row, day): |
340 | day_names = ['monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday', 'sunday'] | |
341 | return int(row[day_names[day.isoweekday() - 1]]) | |
0 | 342 | |
30 | 343 | with gtfs_zip.open('calendar.txt') as file: |
344 | for row in read_csv(map(bytes.decode, file)): | |
345 | for day in date_range(read_date(row['start_date']), read_date(row['end_date']), include_end = True): | |
346 | if filter_day(row, day): | |
347 | add_day_to_service(service_name = row['service_id'], day = day) | |
348 | ||
349 | with gtfs_zip.open('calendar_dates.txt') as file: | |
350 | for row in read_csv(map(bytes.decode, file)): | |
351 | add_day_to_service(service_name = row['service_id'], day = read_date(row['date'])) | |
352 | ||
353 | def services_available_at(day): | |
354 | for service in services.values(): | |
355 | if day in service.dates: | |
356 | yield service | |
357 | ||
88
3b86597c5a88
major update, moved the map to an osm patch
Teemu Piippo <teemu@hecknology.net>
parents:
81
diff
changeset
|
358 | print('Loading stops... ', file = stderr, end = '', flush = True) |
30 | 359 | with gtfs_zip.open('stops.txt') as file: |
360 | for row in read_csv(map(bytes.decode, file)): | |
88
3b86597c5a88
major update, moved the map to an osm patch
Teemu Piippo <teemu@hecknology.net>
parents:
81
diff
changeset
|
361 | location = Location(float(row['stop_lat']), float(row['stop_lon'])) |
30 | 362 | stop = BusStop( |
363 | reference = row['stop_id'], | |
364 | name = row['stop_name'], | |
365 | location = location, | |
71
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
366 | code = row.get('stop_code', row['stop_id']), |
30 | 367 | ) |
368 | bus_stops[stop.reference] = stop | |
71
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
369 | if profile['regions']['use-regions']: |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
370 | 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
|
371 | 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
|
372 | try: |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
373 | bus_stops[stop_reference].region = region |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
374 | except KeyError: |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
375 | pass |
81 | 376 | for bus_stop in bus_stops.values(): |
377 | if not hasattr(bus_stop, 'region'): | |
378 | bus_stop.region = None | |
88
3b86597c5a88
major update, moved the map to an osm patch
Teemu Piippo <teemu@hecknology.net>
parents:
81
diff
changeset
|
379 | print('%d stops' % len(bus_stops), file = stderr) |
21 | 380 | |
30 | 381 | from collections import defaultdict |
382 | bus_stops_by_name = defaultdict(set) | |
383 | for bus_stop in bus_stops.values(): | |
384 | bus_stops_by_name[bus_stop.name].add(bus_stop) | |
385 | 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
|
386 | |
30 | 387 | # ryhmittele bus_stops nimen mukaan |
41 | 388 | global all_clusters |
30 | 389 | all_clusters = [] |
390 | def cluster_bus_stops(): | |
391 | sorted_bus_stops = sorted(bus_stops.values(), key = lambda bus_stop: bus_stop.name) | |
392 | for bus_stop in sorted_bus_stops: | |
393 | if not bus_stop.cluster: | |
394 | stops_to_cluster = {bus_stop} | |
395 | # etsi pysäkin samannimiset vastaparit | |
396 | for pair_candidate in bus_stops_by_name[bus_stop.name]: | |
88
3b86597c5a88
major update, moved the map to an osm patch
Teemu Piippo <teemu@hecknology.net>
parents:
81
diff
changeset
|
397 | distance = pair_candidate.location.distance(bus_stop.location) |
30 | 398 | if pair_candidate is not bus_stop and distance <= 0.4: |
399 | stops_to_cluster.add(pair_candidate) | |
400 | for stop_to_cluster in stops_to_cluster: | |
401 | if stop_to_cluster.cluster: | |
402 | cluster = stop_to_cluster.cluster | |
403 | break | |
404 | else: | |
405 | cluster = BusStopCluster() | |
406 | all_clusters.append(cluster) | |
407 | for stop_to_cluster in stops_to_cluster: | |
408 | if not stop_to_cluster.cluster: | |
409 | cluster.add_stop(stop_to_cluster) | |
410 | # Merkitse muistiin pysäkkien vastaparit käyttäen hyväksi tämänhetkistä ryhmittelytietoa | |
411 | for bus_stop in bus_stops.values(): | |
412 | if bus_stop.cluster: | |
413 | bus_stop.pairs = bus_stop.cluster.stops - {bus_stop} | |
414 | # Ryhmitä ne bus_stops, joilla ei ollut omaa vastaparia, muiden pysäkkien kanssa | |
415 | for bus_stop in sorted_bus_stops: | |
416 | if len(bus_stop.cluster.stops) == 1: | |
417 | possibilities = set() | |
418 | for cluster in all_clusters: | |
419 | if cluster is not bus_stop.cluster: | |
88
3b86597c5a88
major update, moved the map to an osm patch
Teemu Piippo <teemu@hecknology.net>
parents:
81
diff
changeset
|
420 | distance = cluster.center.distance(bus_stop.location) |
30 | 421 | if distance <= 0.4: |
422 | possibilities.add((distance, cluster)) | |
423 | if possibilities: | |
424 | best = min(possibilities)[1] | |
425 | all_clusters.remove(bus_stop.cluster) | |
426 | best.merge(bus_stop.cluster) | |
15
a22cdf28930f
Lisätty bussipysäkkien ryhmittely
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
427 | |
30 | 428 | def shared_elements_in_n_sets(sets): |
429 | from itertools import combinations | |
430 | result = set() | |
431 | for pair in combinations(sets, 2): | |
432 | result |= pair[0] & pair[1] | |
433 | return result | |
15
a22cdf28930f
Lisätty bussipysäkkien ryhmittely
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
434 | |
30 | 435 | def name_clusters(): |
436 | from collections import defaultdict | |
437 | clusters_per_name = defaultdict(set) | |
438 | for cluster in all_clusters: | |
439 | name_representing_stop = min((len(stop.reference), stop.reference, stop) for stop in cluster.stops)[2] | |
440 | clusters_per_name[name_representing_stop.name].add(cluster) | |
441 | for name, clusters in clusters_per_name.items(): | |
442 | if len(clusters) == 1: | |
88
3b86597c5a88
major update, moved the map to an osm patch
Teemu Piippo <teemu@hecknology.net>
parents:
81
diff
changeset
|
443 | # Simple case: this cluster is the only one that wants this name. |
30 | 444 | next(iter(clusters)).name = name |
445 | else: | |
71
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
446 | if profile['regions']['use-regions']: |
88
3b86597c5a88
major update, moved the map to an osm patch
Teemu Piippo <teemu@hecknology.net>
parents:
81
diff
changeset
|
447 | # Find out if all clusters are in different areas |
71
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
448 | common_regions = shared_elements_in_n_sets({stop.region for stop in cluster.stops} for cluster in clusters) |
88
3b86597c5a88
major update, moved the map to an osm patch
Teemu Piippo <teemu@hecknology.net>
parents:
81
diff
changeset
|
449 | # Proposal: cluster -> the areas unique to the cluster |
71
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
450 | proposal = { |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
451 | 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
|
452 | for cluster in clusters |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
453 | } |
88
3b86597c5a88
major update, moved the map to an osm patch
Teemu Piippo <teemu@hecknology.net>
parents:
81
diff
changeset
|
454 | # If at most one cluster is without its own unique region, name the others by region and this one without any. |
71
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
455 | 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
|
456 | for cluster, unique_areas in proposal.items(): |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
457 | individual_cluster_name = name |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
458 | if unique_areas: |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
459 | individual_cluster_name += ' (' + min(unique_areas) + ')' |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
460 | cluster.name = individual_cluster_name |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
461 | break |
88
3b86597c5a88
major update, moved the map to an osm patch
Teemu Piippo <teemu@hecknology.net>
parents:
81
diff
changeset
|
462 | # If all else fails, just number them. |
71
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
463 | for n, (_, cluster) in enumerate(sorted( |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
464 | min((stop.reference.lower(), cluster) for stop in cluster.stops) |
30 | 465 | for cluster in clusters |
71
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
466 | ), 1): |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
467 | individual_cluster_name = name + '-' + str(n) |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
468 | cluster.name = individual_cluster_name |
30 | 469 | |
88
3b86597c5a88
major update, moved the map to an osm patch
Teemu Piippo <teemu@hecknology.net>
parents:
81
diff
changeset
|
470 | print('Clustering bus stops...') |
30 | 471 | cluster_bus_stops() |
472 | name_clusters() | |
473 | ||
474 | for cluster in all_clusters: | |
475 | if cluster.url_name in clusters_by_name: | |
476 | 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
|
477 | else: |
30 | 478 | 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
|
479 | |
88
3b86597c5a88
major update, moved the map to an osm patch
Teemu Piippo <teemu@hecknology.net>
parents:
81
diff
changeset
|
480 | print('Loading schedules... ', end = '', flush = True, file = stderr) |
30 | 481 | with gtfs_zip.open('stop_times.txt') as file: |
482 | row_count = sum(line.count(b'\n') for line in file) | |
483 | with gtfs_zip.open('stop_times.txt') as file: | |
484 | progress = 0 | |
485 | for row in read_csv(map(bytes.decode, file)): | |
80 | 486 | 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
|
487 | continue |
30 | 488 | trip = all_trips[transform_trip_reference(row['trip_id'])] |
489 | arrival_time = read_time(row['arrival_time']) | |
490 | departure_time = read_time(row['departure_time']) | |
491 | stop = bus_stops[row['stop_id']] | |
71
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
492 | traveled_distance = float(row.get('shape_dist_traveled', 1)) * float(profile['metrics']['shape-modifier']) |
30 | 493 | trip.schedule.append(BusHalt(arrival_time, departure_time, stop, trip, traveled_distance)) |
494 | stop.involved_trips.add(trip) | |
495 | progress += 1 | |
496 | if progress % 1000 == 0: | |
88
3b86597c5a88
major update, moved the map to an osm patch
Teemu Piippo <teemu@hecknology.net>
parents:
81
diff
changeset
|
497 | print('\rLoading schedules... %.1f%%' % (progress * 100 / row_count), end = ' ', file = stderr) |
3b86597c5a88
major update, moved the map to an osm patch
Teemu Piippo <teemu@hecknology.net>
parents:
81
diff
changeset
|
498 | print('\rLoading schedules... complete', file = stderr) |
30 | 499 | |
500 | for trip in all_trips.values(): | |
501 | from busroute import simplify_name | |
502 | schedule = trip.concise_schedule() | |
503 | try: | |
504 | trip.from_place = simplify_name(schedule[0]) | |
505 | trip.to_place = simplify_name(schedule[-1]) | |
506 | except IndexError: | |
507 | trip.from_place = '' | |
508 | trip.to_place = '' | |
28
670ffa424ded
Bussipysäkit tallentavat ajovuoronsa välimuistiin suoritusajan nopeuttamiseksi
Teemu Piippo <teemu@hecknology.net>
parents:
26
diff
changeset
|
509 | |
30 | 510 | for route in routes.values(): |
511 | from collections import Counter | |
512 | from busroute import simplify_name | |
513 | tally = Counter() | |
514 | for trip in route.trips: | |
515 | schedule = trip.concise_schedule() | |
516 | places = set(schedule) | |
517 | do_add = True | |
518 | assert type(schedule) is list | |
519 | for candidate in tally: | |
520 | if places.issubset(set(candidate)): | |
521 | do_add = False | |
522 | tally.update({tuple(candidate)}) | |
523 | if do_add: | |
524 | tally.update({tuple(schedule)}) | |
525 | try: | |
526 | most_common_route = tally.most_common(1)[0][0] | |
527 | route.description = simplify_name(most_common_route[0]) + ' - ' + simplify_name(most_common_route[-1]) | |
528 | except: | |
529 | route.description = '' | |
71
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
530 | 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
|
531 | |
72 | 532 | if 'compatibility' in profile and profile['compatibility'].get('fix-destination-times', False): |
533 | # Fölin datassa on jotain tosi kummaa. Ilmeisesti ajovuoron viimeisen pysähdyksen saapumisaika on ihan täysin | |
534 | # väärin. Arvaan että se on seuraavan lähdön aika, mutta joka tapauksessa se on väärin. | |
535 | # Arvataan mikä se todellinen saapumisaika on. Se ei voi mennä kauhean paljon pahemmin vikaan kuin alkuperäinen | |
536 | # väärin oleva data. | |
537 | for trip in all_trips.values(): | |
538 | if len(trip.schedule) >= 2: | |
539 | bus_speed_coefficient = 750 # metriä minuutissa | |
540 | last_leg_distance = trip.schedule[-1].traveled_distance - trip.schedule[-2].traveled_distance | |
541 | 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
|
542 | |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
543 | global trips_by_vehicle_info |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
544 | trips_by_vehicle_info = {} |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
545 | for trip in all_trips.values(): |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
546 | trips_by_vehicle_info[(trip.block_id, trip.schedule[0].arrival_time)] = trip |
93 | 547 | # Add services to all bus stops |
548 | for route in routes.values(): | |
549 | for trip in route.trips: | |
550 | for halt in trip.schedule: | |
551 | halt.stop.services.add(route.service) | |
30 | 552 | |
553 | if __name__ == '__main__': | |
554 | profile.read('profiles/föli.ini') | |
71
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
555 | load_buses('gtfs.zip') |
88
3b86597c5a88
major update, moved the map to an osm patch
Teemu Piippo <teemu@hecknology.net>
parents:
81
diff
changeset
|
556 | import busroute |
3b86597c5a88
major update, moved the map to an osm patch
Teemu Piippo <teemu@hecknology.net>
parents:
81
diff
changeset
|
557 | from regions import parse_regions |
3b86597c5a88
major update, moved the map to an osm patch
Teemu Piippo <teemu@hecknology.net>
parents:
81
diff
changeset
|
558 | busroute.regions = parse_regions('föli.osm') |