Thu, 26 Apr 2018 16:12:23 +0300
added ferry support
5 | 1 | #!/usr/bin/env python3 |
27 | 2 | |
72 | 3 | from misc import profile |
88
3b86597c5a88
major update, moved the map to an osm patch
Teemu Piippo <teemu@hecknology.net>
parents:
87
diff
changeset
|
4 | regions = {} |
52 | 5 | |
88
3b86597c5a88
major update, moved the map to an osm patch
Teemu Piippo <teemu@hecknology.net>
parents:
87
diff
changeset
|
6 | def priority(region_name): |
3b86597c5a88
major update, moved the map to an osm patch
Teemu Piippo <teemu@hecknology.net>
parents:
87
diff
changeset
|
7 | if region_name in regions: |
3b86597c5a88
major update, moved the map to an osm patch
Teemu Piippo <teemu@hecknology.net>
parents:
87
diff
changeset
|
8 | return float(regions[region_name]['priority']) |
3b86597c5a88
major update, moved the map to an osm patch
Teemu Piippo <teemu@hecknology.net>
parents:
87
diff
changeset
|
9 | else: |
3b86597c5a88
major update, moved the map to an osm patch
Teemu Piippo <teemu@hecknology.net>
parents:
87
diff
changeset
|
10 | return 0 |
27 | 11 | |
88
3b86597c5a88
major update, moved the map to an osm patch
Teemu Piippo <teemu@hecknology.net>
parents:
87
diff
changeset
|
12 | def simplify_name(region_name): |
3b86597c5a88
major update, moved the map to an osm patch
Teemu Piippo <teemu@hecknology.net>
parents:
87
diff
changeset
|
13 | region = regions.get(region_name) |
3b86597c5a88
major update, moved the map to an osm patch
Teemu Piippo <teemu@hecknology.net>
parents:
87
diff
changeset
|
14 | if region: |
3b86597c5a88
major update, moved the map to an osm patch
Teemu Piippo <teemu@hecknology.net>
parents:
87
diff
changeset
|
15 | return region.get('short_name', region_name) |
3b86597c5a88
major update, moved the map to an osm patch
Teemu Piippo <teemu@hecknology.net>
parents:
87
diff
changeset
|
16 | else: |
3b86597c5a88
major update, moved the map to an osm patch
Teemu Piippo <teemu@hecknology.net>
parents:
87
diff
changeset
|
17 | return region_name |
87 | 18 | |
91
209f5930d038
show variants in week schedules
Teemu Piippo <teemu@hecknology.net>
parents:
88
diff
changeset
|
19 | def greatly_simplify_name(region_name): |
209f5930d038
show variants in week schedules
Teemu Piippo <teemu@hecknology.net>
parents:
88
diff
changeset
|
20 | region = regions.get(region_name) |
209f5930d038
show variants in week schedules
Teemu Piippo <teemu@hecknology.net>
parents:
88
diff
changeset
|
21 | if region: |
92
16a5c37e4e67
stop_week now shows rare variants with less emphasis
Teemu Piippo <teemu@hecknology.net>
parents:
91
diff
changeset
|
22 | if 'greater_replacement' in region: |
91
209f5930d038
show variants in week schedules
Teemu Piippo <teemu@hecknology.net>
parents:
88
diff
changeset
|
23 | return simplify_name(region['greater_replacement']) |
92
16a5c37e4e67
stop_week now shows rare variants with less emphasis
Teemu Piippo <teemu@hecknology.net>
parents:
91
diff
changeset
|
24 | if 'replacement' in region: |
91
209f5930d038
show variants in week schedules
Teemu Piippo <teemu@hecknology.net>
parents:
88
diff
changeset
|
25 | return simplify_name(region['replacement']) |
209f5930d038
show variants in week schedules
Teemu Piippo <teemu@hecknology.net>
parents:
88
diff
changeset
|
26 | else: |
209f5930d038
show variants in week schedules
Teemu Piippo <teemu@hecknology.net>
parents:
88
diff
changeset
|
27 | return simplify_name(region_name) |
209f5930d038
show variants in week schedules
Teemu Piippo <teemu@hecknology.net>
parents:
88
diff
changeset
|
28 | else: |
209f5930d038
show variants in week schedules
Teemu Piippo <teemu@hecknology.net>
parents:
88
diff
changeset
|
29 | return region_name |
209f5930d038
show variants in week schedules
Teemu Piippo <teemu@hecknology.net>
parents:
88
diff
changeset
|
30 | |
87 | 31 | def reduce_schedule(route, trip_length, whole = False, format = 'medium'): |
71
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
68
diff
changeset
|
32 | length = ((trip_length / 600) * 3 + len(route) * 2) / 5 |
87 | 33 | have_already = set() |
34 | i = 0 | |
71
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
68
diff
changeset
|
35 | if not route: |
5 | 36 | return '' |
71
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
68
diff
changeset
|
37 | while i < len(route): |
88
3b86597c5a88
major update, moved the map to an osm patch
Teemu Piippo <teemu@hecknology.net>
parents:
87
diff
changeset
|
38 | region = regions.get(route[i]) |
3b86597c5a88
major update, moved the map to an osm patch
Teemu Piippo <teemu@hecknology.net>
parents:
87
diff
changeset
|
39 | if region and region.get('replacement'): |
3b86597c5a88
major update, moved the map to an osm patch
Teemu Piippo <teemu@hecknology.net>
parents:
87
diff
changeset
|
40 | route[i] = region['replacement'] |
3b86597c5a88
major update, moved the map to an osm patch
Teemu Piippo <teemu@hecknology.net>
parents:
87
diff
changeset
|
41 | if not route[i] or route[i] in have_already: |
71
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
68
diff
changeset
|
42 | del route[i] |
5 | 43 | else: |
71
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
68
diff
changeset
|
44 | have_already.add(route[i]) |
5 | 45 | i += 1 |
71
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
68
diff
changeset
|
46 | from_place = route[0] |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
68
diff
changeset
|
47 | destination = route[-1] |
88
3b86597c5a88
major update, moved the map to an osm patch
Teemu Piippo <teemu@hecknology.net>
parents:
87
diff
changeset
|
48 | route_weights = {} |
22 | 49 | f = lambda i: i**-0.3 |
71
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
68
diff
changeset
|
50 | factor = 1 / max(f(i + 1) for i in range(len(route))) |
88
3b86597c5a88
major update, moved the map to an osm patch
Teemu Piippo <teemu@hecknology.net>
parents:
87
diff
changeset
|
51 | while priority(route[-1]) < 0: |
71
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
68
diff
changeset
|
52 | del route[-1] |
87 | 53 | if not route: |
54 | return '' | |
71
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
68
diff
changeset
|
55 | destination = route[-1] |
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
68
diff
changeset
|
56 | for i, stop in enumerate(route): |
5 | 57 | # muunna indeksi siten että myöhemmät alueet korostuvat |
24
e6bdb9c54096
Yhtenäistetty ohjelmakoodin kieli englanniksi
Teemu Piippo <teemu@hecknology.net>
parents:
22
diff
changeset
|
58 | i = f(i + 1) * factor |
5 | 59 | # ota prioriteetti huomioon, jotkin alueet ovat tärkeämpiä kyltissä kuin toiset |
88
3b86597c5a88
major update, moved the map to an osm patch
Teemu Piippo <teemu@hecknology.net>
parents:
87
diff
changeset
|
60 | i *= priority(stop) |
3b86597c5a88
major update, moved the map to an osm patch
Teemu Piippo <teemu@hecknology.net>
parents:
87
diff
changeset
|
61 | route_weights[stop] = i |
5 | 62 | # nollaa lähtöpaikan arvo ettei se mitenkään tule kylttiin |
88
3b86597c5a88
major update, moved the map to an osm patch
Teemu Piippo <teemu@hecknology.net>
parents:
87
diff
changeset
|
63 | if from_place in route_weights: |
3b86597c5a88
major update, moved the map to an osm patch
Teemu Piippo <teemu@hecknology.net>
parents:
87
diff
changeset
|
64 | route_weights[from_place] = 0 |
24
e6bdb9c54096
Yhtenäistetty ohjelmakoodin kieli englanniksi
Teemu Piippo <teemu@hecknology.net>
parents:
22
diff
changeset
|
65 | # varmista että destination tulee kylttiin |
88
3b86597c5a88
major update, moved the map to an osm patch
Teemu Piippo <teemu@hecknology.net>
parents:
87
diff
changeset
|
66 | route_weights[destination] = 1e10 |
24
e6bdb9c54096
Yhtenäistetty ohjelmakoodin kieli englanniksi
Teemu Piippo <teemu@hecknology.net>
parents:
22
diff
changeset
|
67 | # muodosta sign-tiedot järjestettynä reittiarvon mukaan |
e6bdb9c54096
Yhtenäistetty ohjelmakoodin kieli englanniksi
Teemu Piippo <teemu@hecknology.net>
parents:
22
diff
changeset
|
68 | weights = sorted([ |
88
3b86597c5a88
major update, moved the map to an osm patch
Teemu Piippo <teemu@hecknology.net>
parents:
87
diff
changeset
|
69 | (stop, route_weights[stop], i) \ |
71
d2e19670b772
Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents:
68
diff
changeset
|
70 | for i, stop in enumerate(route) \ |
88
3b86597c5a88
major update, moved the map to an osm patch
Teemu Piippo <teemu@hecknology.net>
parents:
87
diff
changeset
|
71 | if route_weights[stop] >= 1 |
24
e6bdb9c54096
Yhtenäistetty ohjelmakoodin kieli englanniksi
Teemu Piippo <teemu@hecknology.net>
parents:
22
diff
changeset
|
72 | ], key = lambda stop: -stop[1]) |
87 | 73 | if format == 'long': |
68 | 74 | weights = weights[:4] |
87 | 75 | elif format == 'short': |
76 | weights = weights[:2] | |
77 | # repeat for the second sign value | |
78 | try: | |
79 | if weights[1][0] != destination and weights[1][1] < (500 / length ** 1.15): | |
80 | del weights[1] | |
81 | except IndexError: | |
82 | pass | |
68 | 83 | else: |
84 | # enintään neljä tulee kylttiin | |
85 | weights = weights[:3] | |
87 | 86 | # if the third sign value is not significant enough, drop it |
68 | 87 | try: |
88 | if weights[2][0] != destination and weights[2][1] < (725 / length ** 0.8): | |
89 | del weights[2] | |
90 | except IndexError: | |
91 | pass | |
87 | 92 | # repeat for the second sign value |
68 | 93 | try: |
94 | if weights[1][0] != destination and weights[1][1] < (500 / length ** 1.15): | |
95 | del weights[1] | |
96 | except IndexError: | |
97 | pass | |
87 | 98 | # reorder to get the sign the right way around |
52 | 99 | weights = sorted(weights, key = lambda weight_data: weight_data[2]) |
87 | 100 | # form the sign.. |
24
e6bdb9c54096
Yhtenäistetty ohjelmakoodin kieli englanniksi
Teemu Piippo <teemu@hecknology.net>
parents:
22
diff
changeset
|
101 | sign = [paino[0] for paino in weights] |
52 | 102 | old_sign = sign.copy() |
103 | sign = [] | |
104 | for place in old_sign: | |
105 | if place not in sign: | |
106 | sign.append(place) | |
24
e6bdb9c54096
Yhtenäistetty ohjelmakoodin kieli englanniksi
Teemu Piippo <teemu@hecknology.net>
parents:
22
diff
changeset
|
107 | if whole: |
52 | 108 | sign = [from_place] + sign |
109 | if not sign: | |
110 | sign = [destination] | |
111 | return sign |