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