1 #!/usr/bin/env python3 |
1 #!/usr/bin/env python3 |
2 |
2 |
3 from configparser import ConfigParser |
3 from configparser import ConfigParser |
|
4 from math import inf |
4 |
5 |
5 replacements = { |
6 replacements = { |
6 "ylioppilaskylä-itä": "ylioppilaskylä", |
7 "ylioppilaskylä-itä": "ylioppilaskylä", |
7 "ylioppilaskylä-länsi": "ylioppilaskylä", |
8 "ylioppilaskylä-länsi": "ylioppilaskylä", |
8 "jokila": "lieto", |
9 "jokila": "lieto", |
9 "piispala": "lieto", |
10 "piispala": "lieto", |
10 'saaro': 'ruissalo', |
11 'saaro': 'ruissalo', |
11 'silvola': 'vahto', |
12 'silvola': 'vahto', |
12 'hemmola': 'vahto', |
13 'hemmola': 'vahto', |
13 'paasniittu': 'ruskon keskusta', |
14 'paasniittu': 'rusko', |
14 'kahari': 'ruskon keskusta', |
15 'kahari': 'rusko', |
15 'kemiö': 'kemiönsaari', |
16 'kemiö': 'kemiönsaari', |
16 'kasnäs': 'kemiönsaari', |
17 'kasnäs': 'kemiönsaari', |
17 'pernon telakka': 'perno', |
18 'pernon telakka': 'perno', |
18 'paimion sairaala': 'paimio', |
19 'paimion sairaala': 'paimio', |
19 'hanhijoki': 'paimio', |
20 'hanhijoki': 'paimio', |
20 } |
21 } |
21 |
22 |
22 # nimiä joista voidaan joustaa tarvittaessa |
|
23 abbreviations = { |
|
24 'raision keskusta': 'raisio', |
|
25 'ruskon keskusta': 'rusko', |
|
26 'naantalin keskusta': 'naantali', |
|
27 'kaarinan keskusta': 'kaarina', |
|
28 'kauppakeskus mylly': 'mylly', |
|
29 } |
|
30 |
|
31 region_info = ConfigParser() |
23 region_info = ConfigParser() |
32 region_info.read('regions.ini') |
24 region_info.read('regions.ini') |
33 municipalities = set(region_info.sections()) |
25 municipalities = set(region_info.sections()) |
34 municipalities_per_region = {} |
26 municipalities_per_region = {} |
35 |
27 |
40 for place_from, place_to in replacements.items(): |
32 for place_from, place_to in replacements.items(): |
41 municipalities_per_region[place_to] = municipalities_per_region[place_from] |
33 municipalities_per_region[place_to] = municipalities_per_region[place_from] |
42 |
34 |
43 def simplify_name(name): |
35 def simplify_name(name): |
44 name = replacements.get(name, name) |
36 name = replacements.get(name, name) |
45 name = abbreviations.get(name, name) |
|
46 return name |
37 return name |
47 |
38 |
48 def reduce_schedule(reitti, trip_length, whole = False, long = False): |
39 def reduce_schedule(route, trip_length, whole = False, long = False): |
49 length = ((trip_length / 600) * 3 + len(reitti) * 2) / 5 |
40 length = ((trip_length / 600) * 3 + len(route) * 2) / 5 |
50 if not reitti: |
41 if not route: |
51 return '' |
42 return '' |
52 have_already = set() |
43 have_already = set() |
53 i = 0 |
44 i = 0 |
54 while i < len(reitti): |
45 while i < len(route): |
55 if reitti[i] in replacements: |
46 if route[i] in replacements: |
56 reitti[i] = replacements[reitti[i]] |
47 route[i] = replacements[route[i]] |
57 if reitti[i] in have_already: |
48 if route[i] in have_already: |
58 del reitti[i] |
49 del route[i] |
59 else: |
50 else: |
60 have_already.add(reitti[i]) |
51 have_already.add(route[i]) |
61 i += 1 |
52 i += 1 |
62 priorities = { |
53 priorities = { |
63 'ihala': 150, |
54 'ihala': 150, |
64 'kauppakeskus mylly': 200, |
55 'mylly': 200, |
65 'kuninkoja': 150, |
56 'kuninkoja': 150, |
66 'moikoinen': 80, |
57 'moikoinen': 100, |
67 'raision keskusta': 150, |
58 'raisio': 150, |
68 'kukola': 50, |
59 'kukola': 50, |
69 'kaistarniemi': 50, |
60 'kaistarniemi': 75, |
70 'papinsaari': 80, |
61 'papinsaari': 80, |
71 'oriniemi': 50, |
62 'oriniemi': 50, |
72 'pikisaari': 50, |
63 'pikisaari': 50, |
73 'häppilä': 20, |
64 'häppilä': 20, |
74 'haarla': 80, |
65 'haarla': 80, |
160 'särkilahti': 25, |
150 'särkilahti': 25, |
161 'urusvuori': 25, |
151 'urusvuori': 25, |
162 'port arthur': 25, |
152 'port arthur': 25, |
163 } |
153 } |
164 |
154 |
165 if 'kauppatori' not in reitti: |
155 if 'kauppatori' not in route: |
166 priorities['länsikeskus'] = priorities['skanssi'] = priorities['kauppatori'] |
156 priorities['länsikeskus'] = priorities['skanssi'] = priorities['kauppatori'] |
167 |
157 |
168 from_place = reitti[0] |
158 from_place = route[0] |
169 destination = reitti[-1] |
159 destination = route[-1] |
170 reitti_arvot = {} |
160 reitti_arvot = {} |
171 f = lambda i: i**-0.3 |
161 f = lambda i: i**-0.3 |
172 factor = 1 / max(f(i + 1) for i in range(len(reitti))) |
162 factor = 1 / max(f(i + 1) for i in range(len(route))) |
173 while priorities.get(reitti[-1], 0) < 0: |
163 while priorities.get(route[-1], 0) < 0: |
174 del reitti[-1] |
164 del route[-1] |
175 destination = reitti[-1] |
165 destination = route[-1] |
176 for i, stop in enumerate(reitti): |
166 for i, stop in enumerate(route): |
177 # muunna indeksi siten että myöhemmät alueet korostuvat |
167 # muunna indeksi siten että myöhemmät alueet korostuvat |
178 i = f(i + 1) * factor |
168 i = f(i + 1) * factor |
179 # ota prioriteetti huomioon, jotkin alueet ovat tärkeämpiä kyltissä kuin toiset |
169 # ota prioriteetti huomioon, jotkin alueet ovat tärkeämpiä kyltissä kuin toiset |
180 i *= priorities.get(stop, 1) |
170 i *= priorities.get(stop, 1) |
181 reitti_arvot[stop] = i |
171 reitti_arvot[stop] = i |
185 # varmista että destination tulee kylttiin |
175 # varmista että destination tulee kylttiin |
186 reitti_arvot[destination] = 1e10 |
176 reitti_arvot[destination] = 1e10 |
187 # muodosta sign-tiedot järjestettynä reittiarvon mukaan |
177 # muodosta sign-tiedot järjestettynä reittiarvon mukaan |
188 weights = sorted([ |
178 weights = sorted([ |
189 (stop, reitti_arvot[stop], i) \ |
179 (stop, reitti_arvot[stop], i) \ |
190 for i, stop in enumerate(reitti) \ |
180 for i, stop in enumerate(route) \ |
191 if reitti_arvot[stop] >= 1 |
181 if reitti_arvot[stop] >= 1 |
192 ], key = lambda stop: -stop[1]) |
182 ], key = lambda stop: -stop[1]) |
193 if long: |
183 if long: |
194 weights = weights[:4] |
184 weights = weights[:4] |
195 else: |
185 else: |
209 # lajitellaan painoarvot uudestaan reittijärjestykseen jotta sign tulee oikeinpäin |
199 # lajitellaan painoarvot uudestaan reittijärjestykseen jotta sign tulee oikeinpäin |
210 weights = sorted(weights, key = lambda weight_data: weight_data[2]) |
200 weights = sorted(weights, key = lambda weight_data: weight_data[2]) |
211 # muodostetaan sign.. |
201 # muodostetaan sign.. |
212 sign = [paino[0] for paino in weights] |
202 sign = [paino[0] for paino in weights] |
213 to_place = sign[-1] |
203 to_place = sign[-1] |
214 nearby_municipalities = {'Turku', 'Raisio', 'Naantali', 'Kaarina', 'Lieto'} |
204 #nearby_municipalities = {'Turku', 'Raisio', 'Naantali', 'Kaarina', 'Lieto'} |
215 if from_place != to_place: |
205 #if from_place != to_place: |
216 municipality_from = municipalities_per_region.get(from_place) |
206 # municipality_from = municipalities_per_region.get(from_place) |
217 municipality_to = municipalities_per_region.get(to_place) |
207 # municipality_to = municipalities_per_region.get(to_place) |
218 if municipality_from and municipality_to and municipality_from not in nearby_municipalities | {municipality_to} and municipality_to not in nearby_municipalities: |
208 # if municipality_from and municipality_to and municipality_from not in nearby_municipalities | {municipality_to} and municipality_to not in nearby_municipalities: |
219 def convert_region(region): |
209 # def convert_region(region): |
220 municipality = municipalities_per_region.get(region, region) |
210 # municipality = municipalities_per_region.get(region, region) |
221 if municipality != municipality_from: |
211 # if municipality != municipality_from: |
222 return municipality |
212 # return municipality |
223 else: |
213 # else: |
224 return region |
214 # return region |
225 sign = [convert_region(region) for region in sign] |
215 # sign = [convert_region(region) for region in sign] |
226 # supista nimet tarvittaessa |
|
227 if len(' - '.join(sign)) > 20: |
|
228 sign = [abbreviations.get(place, place) for place in sign] |
|
229 old_sign = sign.copy() |
216 old_sign = sign.copy() |
230 sign = [] |
217 sign = [] |
231 for place in old_sign: |
218 for place in old_sign: |
232 if place not in sign: |
219 if place not in sign: |
233 sign.append(place) |
220 sign.append(place) |