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