Tue, 01 May 2018 13:23:15 +0300
added a file
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] |
31
60045b362d71
- Ajovuoroa ei enää esitetä kahdessa välilehdessä vaan puukuvaimessa
Teemu Piippo <teemu@hecknology.net>
parents:
30
diff
changeset
|
101 | def schedule_for_day(self, date, *, arrivals = 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 |
42
0b53b7c70875
Näytä myös aikataulut jotka ovat minuutti sitten menneet
teemu
parents:
41
diff
changeset
|
120 | if 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 | |
24
e6bdb9c54096
Yhtenäistetty ohjelmakoodin kieli englanniksi
Teemu Piippo <teemu@hecknology.net>
parents:
23
diff
changeset
|
184 | routes = {} |
e6bdb9c54096
Yhtenäistetty ohjelmakoodin kieli englanniksi
Teemu Piippo <teemu@hecknology.net>
parents:
23
diff
changeset
|
185 | routes_per_id = {} |
e6bdb9c54096
Yhtenäistetty ohjelmakoodin kieli englanniksi
Teemu Piippo <teemu@hecknology.net>
parents:
23
diff
changeset
|
186 | all_trips = {} |
e6bdb9c54096
Yhtenäistetty ohjelmakoodin kieli englanniksi
Teemu Piippo <teemu@hecknology.net>
parents:
23
diff
changeset
|
187 | services = {} |
e6bdb9c54096
Yhtenäistetty ohjelmakoodin kieli englanniksi
Teemu Piippo <teemu@hecknology.net>
parents:
23
diff
changeset
|
188 | bus_stops = {} |
15
a22cdf28930f
Lisätty bussipysäkkien ryhmittely
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
189 | all_clusters = set() |
30 | 190 | viimeinen_käyttöpäivä = None |
191 | clusters_by_name = {} | |
24
e6bdb9c54096
Yhtenäistetty ohjelmakoodin kieli englanniksi
Teemu Piippo <teemu@hecknology.net>
parents:
23
diff
changeset
|
192 | services_for_day = {} |
0 | 193 | |
93 | 194 | def load_buses(gtfs_zip_path): |
30 | 195 | global viimeinen_käyttöpäivä |
196 | from zipfile import ZipFile | |
197 | 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
|
198 | print('Loading routes... ', file = stderr, end = '', flush = True) |
30 | 199 | with gtfs_zip.open('routes.txt') as file: |
200 | for row in read_csv(map(bytes.decode, file)): | |
201 | route = BusRoute(row) | |
202 | routes[route.reference] = route | |
203 | 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
|
204 | print('%d routes' % len(routes), file = stderr) |
93 | 205 | # Add services |
206 | import re | |
207 | service_patterns = {} | |
208 | if 'service-patterns' in profile: | |
209 | for service_type, regexps in profile['service-patterns'].items(): | |
210 | service_patterns[service_type] = {re.compile(regexp) for regexp in regexps.split('@')} | |
211 | if 'services' in profile and profile['services'].get('default-service'): | |
212 | print('Tagging services...', end = '') | |
213 | for route in routes.values(): | |
214 | for service_type, regexps in service_patterns.items(): | |
215 | if any(regexp.match(route.reference) for regexp in regexps): | |
216 | route.service = service_type | |
217 | break | |
218 | else: | |
219 | route.service = profile['services']['default-service'] | |
220 | print('') | |
88
3b86597c5a88
major update, moved the map to an osm patch
Teemu Piippo <teemu@hecknology.net>
parents:
81
diff
changeset
|
221 | print('Loading trips... ', file = stderr, end = '', flush = True) |
30 | 222 | shape_distances = {} |
71
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
223 | try: |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
224 | 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
|
225 | 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
|
226 | 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
|
227 | except KeyError: |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
228 | pass |
29 | 229 | |
30 | 230 | with gtfs_zip.open('trips.txt') as file: |
231 | for row in read_csv(map(bytes.decode, file)): | |
232 | if row['service_id'] not in services: | |
233 | services[row['service_id']] = BusService(row['service_id']) | |
234 | route = routes_per_id[row['route_id']] | |
235 | trip = BusTrip( | |
236 | reference = row['trip_id'], | |
237 | route = route, | |
238 | service = services[row['service_id']], | |
71
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
239 | length = shape_distances.get(row.get('shape_id'), 1) * float(profile['metrics']['shape-modifier']), |
90 | 240 | block_id = row.get('block_id') or row['service_id'], |
30 | 241 | ) |
242 | route.trips.add(trip) | |
71
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
243 | if trip.name in all_trips: |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
244 | print('Trip %s already exists' % trip.name) |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
245 | else: |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
246 | 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
|
247 | print('%d trips' % len(all_trips), file = stderr) |
0 | 248 | |
30 | 249 | def read_date(teksti): |
250 | return date(int(teksti[:4]), int(teksti[4:6]), int(teksti[6:])) | |
251 | ||
252 | def read_time(teksti): | |
88
3b86597c5a88
major update, moved the map to an osm patch
Teemu Piippo <teemu@hecknology.net>
parents:
81
diff
changeset
|
253 | 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
|
254 | return timedelta(hours = hour, minutes = minute, seconds = second) |
30 | 255 | |
93 | 256 | print('Loading dates... ', file = stderr, flush = True) |
30 | 257 | viimeinen_käyttöpäivä = date.today() |
29 | 258 | |
30 | 259 | def date_range(start_date, end_date, *, include_end = False): |
260 | ''' Generates date from start_date to end_date. If include_end is True, then end_date will be yielded. ''' | |
261 | current_date = start_date | |
262 | while current_date < end_date: | |
263 | yield current_date | |
264 | current_date += timedelta(1) | |
265 | if include_end: | |
266 | yield end_date | |
29 | 267 | |
30 | 268 | def add_day_to_service(service_name, day): |
269 | try: | |
270 | service = services[service_name] | |
271 | except KeyError: | |
272 | return | |
273 | else: | |
274 | service.dates.add(day) | |
275 | if day not in services_for_day: | |
276 | services_for_day[day] = set() | |
277 | services_for_day[day].add(service) | |
278 | global viimeinen_käyttöpäivä | |
279 | viimeinen_käyttöpäivä = max(day, viimeinen_käyttöpäivä) | |
29 | 280 | |
30 | 281 | def filter_day(row, day): |
282 | day_names = ['monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday', 'sunday'] | |
283 | return int(row[day_names[day.isoweekday() - 1]]) | |
0 | 284 | |
30 | 285 | with gtfs_zip.open('calendar.txt') as file: |
286 | for row in read_csv(map(bytes.decode, file)): | |
287 | for day in date_range(read_date(row['start_date']), read_date(row['end_date']), include_end = True): | |
288 | if filter_day(row, day): | |
289 | add_day_to_service(service_name = row['service_id'], day = day) | |
290 | ||
291 | with gtfs_zip.open('calendar_dates.txt') as file: | |
292 | for row in read_csv(map(bytes.decode, file)): | |
293 | add_day_to_service(service_name = row['service_id'], day = read_date(row['date'])) | |
294 | ||
295 | def services_available_at(day): | |
296 | for service in services.values(): | |
297 | if day in service.dates: | |
298 | yield service | |
299 | ||
88
3b86597c5a88
major update, moved the map to an osm patch
Teemu Piippo <teemu@hecknology.net>
parents:
81
diff
changeset
|
300 | print('Loading stops... ', file = stderr, end = '', flush = True) |
30 | 301 | with gtfs_zip.open('stops.txt') as file: |
302 | 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
|
303 | location = Location(float(row['stop_lat']), float(row['stop_lon'])) |
30 | 304 | stop = BusStop( |
305 | reference = row['stop_id'], | |
306 | name = row['stop_name'], | |
307 | location = location, | |
71
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
308 | code = row.get('stop_code', row['stop_id']), |
30 | 309 | ) |
310 | bus_stops[stop.reference] = stop | |
71
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
311 | if profile['regions']['use-regions']: |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
312 | 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
|
313 | 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
|
314 | try: |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
315 | bus_stops[stop_reference].region = region |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
316 | except KeyError: |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
317 | pass |
81 | 318 | for bus_stop in bus_stops.values(): |
319 | if not hasattr(bus_stop, 'region'): | |
320 | bus_stop.region = None | |
88
3b86597c5a88
major update, moved the map to an osm patch
Teemu Piippo <teemu@hecknology.net>
parents:
81
diff
changeset
|
321 | print('%d stops' % len(bus_stops), file = stderr) |
21 | 322 | |
30 | 323 | class BusStopCluster: |
324 | def __init__(self): | |
325 | self.stops = set() | |
88
3b86597c5a88
major update, moved the map to an osm patch
Teemu Piippo <teemu@hecknology.net>
parents:
81
diff
changeset
|
326 | self.cached_center = None |
30 | 327 | self.name = None |
328 | @property | |
329 | def url_name(self): | |
330 | return self.name.lower().replace('(', '').replace(')', '').replace(' ', '-') | |
331 | def add_stop(self, stop): | |
332 | assert not stop.cluster | |
333 | stop.cluster = self | |
334 | self.stops.add(stop) | |
88
3b86597c5a88
major update, moved the map to an osm patch
Teemu Piippo <teemu@hecknology.net>
parents:
81
diff
changeset
|
335 | self.cached_center = None |
30 | 336 | @property |
337 | def center(self): | |
88
3b86597c5a88
major update, moved the map to an osm patch
Teemu Piippo <teemu@hecknology.net>
parents:
81
diff
changeset
|
338 | if not self.cached_center: |
30 | 339 | if self.stops: |
340 | from statistics import median | |
341 | pointtype = type(next(iter(self.stops)).location) | |
88
3b86597c5a88
major update, moved the map to an osm patch
Teemu Piippo <teemu@hecknology.net>
parents:
81
diff
changeset
|
342 | self.cached_center = pointtype( |
30 | 343 | median(stop.location.x for stop in self.stops), |
344 | median(stop.location.y for stop in self.stops), | |
345 | ) | |
346 | else: | |
347 | raise ValueError('an empty cluster has no center point') | |
88
3b86597c5a88
major update, moved the map to an osm patch
Teemu Piippo <teemu@hecknology.net>
parents:
81
diff
changeset
|
348 | return self.cached_center |
30 | 349 | def merge(self, other): |
350 | for bus_stop in other.stops: | |
351 | bus_stop.cluster = self | |
352 | self.stops |= other.stops | |
353 | other.stops = set() | |
88
3b86597c5a88
major update, moved the map to an osm patch
Teemu Piippo <teemu@hecknology.net>
parents:
81
diff
changeset
|
354 | other.cached_center = None |
31
60045b362d71
- Ajovuoroa ei enää esitetä kahdessa välilehdessä vaan puukuvaimessa
Teemu Piippo <teemu@hecknology.net>
parents:
30
diff
changeset
|
355 | def schedule(self, *, max_amount = 50): |
30 | 356 | result = [] |
357 | for stop in self.stops: | |
31
60045b362d71
- Ajovuoroa ei enää esitetä kahdessa välilehdessä vaan puukuvaimessa
Teemu Piippo <teemu@hecknology.net>
parents:
30
diff
changeset
|
358 | result += stop.schedule(max_amount = max_amount) |
30 | 359 | result.sort(key = lambda schedule_entry: schedule_entry['time']) |
360 | return result[:max_amount] | |
15
a22cdf28930f
Lisätty bussipysäkkien ryhmittely
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
361 | |
30 | 362 | from collections import defaultdict |
363 | bus_stops_by_name = defaultdict(set) | |
364 | for bus_stop in bus_stops.values(): | |
365 | bus_stops_by_name[bus_stop.name].add(bus_stop) | |
366 | 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
|
367 | |
30 | 368 | # ryhmittele bus_stops nimen mukaan |
41 | 369 | global all_clusters |
30 | 370 | all_clusters = [] |
371 | def cluster_bus_stops(): | |
372 | sorted_bus_stops = sorted(bus_stops.values(), key = lambda bus_stop: bus_stop.name) | |
373 | for bus_stop in sorted_bus_stops: | |
374 | if not bus_stop.cluster: | |
375 | stops_to_cluster = {bus_stop} | |
376 | # etsi pysäkin samannimiset vastaparit | |
377 | 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
|
378 | distance = pair_candidate.location.distance(bus_stop.location) |
30 | 379 | if pair_candidate is not bus_stop and distance <= 0.4: |
380 | stops_to_cluster.add(pair_candidate) | |
381 | for stop_to_cluster in stops_to_cluster: | |
382 | if stop_to_cluster.cluster: | |
383 | cluster = stop_to_cluster.cluster | |
384 | break | |
385 | else: | |
386 | cluster = BusStopCluster() | |
387 | all_clusters.append(cluster) | |
388 | for stop_to_cluster in stops_to_cluster: | |
389 | if not stop_to_cluster.cluster: | |
390 | cluster.add_stop(stop_to_cluster) | |
391 | # Merkitse muistiin pysäkkien vastaparit käyttäen hyväksi tämänhetkistä ryhmittelytietoa | |
392 | for bus_stop in bus_stops.values(): | |
393 | if bus_stop.cluster: | |
394 | bus_stop.pairs = bus_stop.cluster.stops - {bus_stop} | |
395 | # Ryhmitä ne bus_stops, joilla ei ollut omaa vastaparia, muiden pysäkkien kanssa | |
396 | for bus_stop in sorted_bus_stops: | |
397 | if len(bus_stop.cluster.stops) == 1: | |
398 | possibilities = set() | |
399 | for cluster in all_clusters: | |
400 | 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
|
401 | distance = cluster.center.distance(bus_stop.location) |
30 | 402 | if distance <= 0.4: |
403 | possibilities.add((distance, cluster)) | |
404 | if possibilities: | |
405 | best = min(possibilities)[1] | |
406 | all_clusters.remove(bus_stop.cluster) | |
407 | best.merge(bus_stop.cluster) | |
15
a22cdf28930f
Lisätty bussipysäkkien ryhmittely
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
408 | |
30 | 409 | def shared_elements_in_n_sets(sets): |
410 | from itertools import combinations | |
411 | result = set() | |
412 | for pair in combinations(sets, 2): | |
413 | result |= pair[0] & pair[1] | |
414 | return result | |
15
a22cdf28930f
Lisätty bussipysäkkien ryhmittely
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
415 | |
30 | 416 | def name_clusters(): |
417 | from collections import defaultdict | |
418 | clusters_per_name = defaultdict(set) | |
419 | for cluster in all_clusters: | |
420 | name_representing_stop = min((len(stop.reference), stop.reference, stop) for stop in cluster.stops)[2] | |
421 | clusters_per_name[name_representing_stop.name].add(cluster) | |
422 | for name, clusters in clusters_per_name.items(): | |
423 | if len(clusters) == 1: | |
88
3b86597c5a88
major update, moved the map to an osm patch
Teemu Piippo <teemu@hecknology.net>
parents:
81
diff
changeset
|
424 | # Simple case: this cluster is the only one that wants this name. |
30 | 425 | next(iter(clusters)).name = name |
426 | else: | |
71
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
427 | 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
|
428 | # 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
|
429 | 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
|
430 | # 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
|
431 | proposal = { |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
432 | 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
|
433 | for cluster in clusters |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
434 | } |
88
3b86597c5a88
major update, moved the map to an osm patch
Teemu Piippo <teemu@hecknology.net>
parents:
81
diff
changeset
|
435 | # 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
|
436 | 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
|
437 | for cluster, unique_areas in proposal.items(): |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
438 | individual_cluster_name = name |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
439 | if unique_areas: |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
440 | individual_cluster_name += ' (' + min(unique_areas) + ')' |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
441 | cluster.name = individual_cluster_name |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
442 | break |
88
3b86597c5a88
major update, moved the map to an osm patch
Teemu Piippo <teemu@hecknology.net>
parents:
81
diff
changeset
|
443 | # 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
|
444 | for n, (_, cluster) in enumerate(sorted( |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
445 | min((stop.reference.lower(), cluster) for stop in cluster.stops) |
30 | 446 | for cluster in clusters |
71
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
447 | ), 1): |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
448 | individual_cluster_name = name + '-' + str(n) |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
449 | cluster.name = individual_cluster_name |
30 | 450 | |
88
3b86597c5a88
major update, moved the map to an osm patch
Teemu Piippo <teemu@hecknology.net>
parents:
81
diff
changeset
|
451 | print('Clustering bus stops...') |
30 | 452 | cluster_bus_stops() |
453 | name_clusters() | |
454 | ||
455 | for cluster in all_clusters: | |
456 | if cluster.url_name in clusters_by_name: | |
457 | 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
|
458 | else: |
30 | 459 | 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
|
460 | |
88
3b86597c5a88
major update, moved the map to an osm patch
Teemu Piippo <teemu@hecknology.net>
parents:
81
diff
changeset
|
461 | print('Loading schedules... ', end = '', flush = True, file = stderr) |
30 | 462 | with gtfs_zip.open('stop_times.txt') as file: |
463 | row_count = sum(line.count(b'\n') for line in file) | |
464 | with gtfs_zip.open('stop_times.txt') as file: | |
465 | progress = 0 | |
466 | for row in read_csv(map(bytes.decode, file)): | |
80 | 467 | 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
|
468 | continue |
30 | 469 | trip = all_trips[transform_trip_reference(row['trip_id'])] |
470 | arrival_time = read_time(row['arrival_time']) | |
471 | departure_time = read_time(row['departure_time']) | |
472 | stop = bus_stops[row['stop_id']] | |
71
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
473 | traveled_distance = float(row.get('shape_dist_traveled', 1)) * float(profile['metrics']['shape-modifier']) |
30 | 474 | trip.schedule.append(BusHalt(arrival_time, departure_time, stop, trip, traveled_distance)) |
475 | stop.involved_trips.add(trip) | |
476 | progress += 1 | |
477 | if progress % 1000 == 0: | |
88
3b86597c5a88
major update, moved the map to an osm patch
Teemu Piippo <teemu@hecknology.net>
parents:
81
diff
changeset
|
478 | 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
|
479 | print('\rLoading schedules... complete', file = stderr) |
30 | 480 | |
481 | for trip in all_trips.values(): | |
482 | from busroute import simplify_name | |
483 | schedule = trip.concise_schedule() | |
484 | try: | |
485 | trip.from_place = simplify_name(schedule[0]) | |
486 | trip.to_place = simplify_name(schedule[-1]) | |
487 | except IndexError: | |
488 | trip.from_place = '' | |
489 | trip.to_place = '' | |
28
670ffa424ded
Bussipysäkit tallentavat ajovuoronsa välimuistiin suoritusajan nopeuttamiseksi
Teemu Piippo <teemu@hecknology.net>
parents:
26
diff
changeset
|
490 | |
30 | 491 | for route in routes.values(): |
492 | from collections import Counter | |
493 | from busroute import simplify_name | |
494 | tally = Counter() | |
495 | for trip in route.trips: | |
496 | schedule = trip.concise_schedule() | |
497 | places = set(schedule) | |
498 | do_add = True | |
499 | assert type(schedule) is list | |
500 | for candidate in tally: | |
501 | if places.issubset(set(candidate)): | |
502 | do_add = False | |
503 | tally.update({tuple(candidate)}) | |
504 | if do_add: | |
505 | tally.update({tuple(schedule)}) | |
506 | try: | |
507 | most_common_route = tally.most_common(1)[0][0] | |
508 | route.description = simplify_name(most_common_route[0]) + ' - ' + simplify_name(most_common_route[-1]) | |
509 | except: | |
510 | route.description = '' | |
71
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
511 | 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
|
512 | |
72 | 513 | if 'compatibility' in profile and profile['compatibility'].get('fix-destination-times', False): |
514 | # Fölin datassa on jotain tosi kummaa. Ilmeisesti ajovuoron viimeisen pysähdyksen saapumisaika on ihan täysin | |
515 | # väärin. Arvaan että se on seuraavan lähdön aika, mutta joka tapauksessa se on väärin. | |
516 | # Arvataan mikä se todellinen saapumisaika on. Se ei voi mennä kauhean paljon pahemmin vikaan kuin alkuperäinen | |
517 | # väärin oleva data. | |
518 | for trip in all_trips.values(): | |
519 | if len(trip.schedule) >= 2: | |
520 | bus_speed_coefficient = 750 # metriä minuutissa | |
521 | last_leg_distance = trip.schedule[-1].traveled_distance - trip.schedule[-2].traveled_distance | |
522 | 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
|
523 | |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
524 | global trips_by_vehicle_info |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
525 | trips_by_vehicle_info = {} |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
526 | for trip in all_trips.values(): |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
527 | trips_by_vehicle_info[(trip.block_id, trip.schedule[0].arrival_time)] = trip |
93 | 528 | # Add services to all bus stops |
529 | for route in routes.values(): | |
530 | for trip in route.trips: | |
531 | for halt in trip.schedule: | |
532 | halt.stop.services.add(route.service) | |
30 | 533 | |
534 | if __name__ == '__main__': | |
535 | profile.read('profiles/föli.ini') | |
71
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
42
diff
changeset
|
536 | load_buses('gtfs.zip') |
88
3b86597c5a88
major update, moved the map to an osm patch
Teemu Piippo <teemu@hecknology.net>
parents:
81
diff
changeset
|
537 | import busroute |
3b86597c5a88
major update, moved the map to an osm patch
Teemu Piippo <teemu@hecknology.net>
parents:
81
diff
changeset
|
538 | from regions import parse_regions |
3b86597c5a88
major update, moved the map to an osm patch
Teemu Piippo <teemu@hecknology.net>
parents:
81
diff
changeset
|
539 | busroute.regions = parse_regions('föli.osm') |