busroute.py

Wed, 26 Sep 2018 13:12:11 +0300

author
Teemu Piippo <teemu@hecknology.net>
date
Wed, 26 Sep 2018 13:12:11 +0300
changeset 128
7a55abeab5fd
parent 98
c07a77c8a070
permissions
-rw-r--r--

refactor

5
a65b680f1774 Paljon asioita
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
1 #!/usr/bin/env python3
27
f89504285945 Lisää aluejuttuja
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
2
72
65408ed066b3 Refactor
Teemu Piippo <teemu@hecknology.net>
parents: 71
diff changeset
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
cab8d38fe5c6 Kuntauudistus
Teemu Piippo <teemu@hecknology.net>
parents: 46
diff changeset
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
f89504285945 Lisää aluejuttuja
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
11
94
e27c18f080d1 added an interesting trip portal
Teemu Piippo <teemu@hecknology.net>
parents: 92
diff changeset
12 def simplify_name(region_name, replace = False):
88
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:
98
c07a77c8a070 fixes, updates, new regions + priorities
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
15 if replace and 'replacement' in region:
94
e27c18f080d1 added an interesting trip portal
Teemu Piippo <teemu@hecknology.net>
parents: 92
diff changeset
16 return simplify_name(region['replacement'])
88
3b86597c5a88 major update, moved the map to an osm patch
Teemu Piippo <teemu@hecknology.net>
parents: 87
diff changeset
17 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
18 else:
3b86597c5a88 major update, moved the map to an osm patch
Teemu Piippo <teemu@hecknology.net>
parents: 87
diff changeset
19 return region_name
87
9139a94e540c updates
Teemu Piippo <teemu@hecknology.net>
parents: 75
diff changeset
20
91
209f5930d038 show variants in week schedules
Teemu Piippo <teemu@hecknology.net>
parents: 88
diff changeset
21 def greatly_simplify_name(region_name):
209f5930d038 show variants in week schedules
Teemu Piippo <teemu@hecknology.net>
parents: 88
diff changeset
22 region = regions.get(region_name)
209f5930d038 show variants in week schedules
Teemu Piippo <teemu@hecknology.net>
parents: 88
diff changeset
23 if region:
92
16a5c37e4e67 stop_week now shows rare variants with less emphasis
Teemu Piippo <teemu@hecknology.net>
parents: 91
diff changeset
24 if 'greater_replacement' in region:
91
209f5930d038 show variants in week schedules
Teemu Piippo <teemu@hecknology.net>
parents: 88
diff changeset
25 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
26 if 'replacement' in region:
91
209f5930d038 show variants in week schedules
Teemu Piippo <teemu@hecknology.net>
parents: 88
diff changeset
27 return simplify_name(region['replacement'])
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 simplify_name(region_name)
209f5930d038 show variants in week schedules
Teemu Piippo <teemu@hecknology.net>
parents: 88
diff changeset
30 else:
209f5930d038 show variants in week schedules
Teemu Piippo <teemu@hecknology.net>
parents: 88
diff changeset
31 return region_name
209f5930d038 show variants in week schedules
Teemu Piippo <teemu@hecknology.net>
parents: 88
diff changeset
32
87
9139a94e540c updates
Teemu Piippo <teemu@hecknology.net>
parents: 75
diff changeset
33 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
34 length = ((trip_length / 600) * 3 + len(route) * 2) / 5
87
9139a94e540c updates
Teemu Piippo <teemu@hecknology.net>
parents: 75
diff changeset
35 have_already = set()
9139a94e540c updates
Teemu Piippo <teemu@hecknology.net>
parents: 75
diff changeset
36 i = 0
71
d2e19670b772 Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents: 68
diff changeset
37 if not route:
5
a65b680f1774 Paljon asioita
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
38 return ''
71
d2e19670b772 Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents: 68
diff changeset
39 while i < len(route):
88
3b86597c5a88 major update, moved the map to an osm patch
Teemu Piippo <teemu@hecknology.net>
parents: 87
diff changeset
40 region = regions.get(route[i])
3b86597c5a88 major update, moved the map to an osm patch
Teemu Piippo <teemu@hecknology.net>
parents: 87
diff changeset
41 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
42 route[i] = region['replacement']
3b86597c5a88 major update, moved the map to an osm patch
Teemu Piippo <teemu@hecknology.net>
parents: 87
diff changeset
43 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
44 del route[i]
5
a65b680f1774 Paljon asioita
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
45 else:
71
d2e19670b772 Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents: 68
diff changeset
46 have_already.add(route[i])
5
a65b680f1774 Paljon asioita
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
47 i += 1
71
d2e19670b772 Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents: 68
diff changeset
48 from_place = route[0]
d2e19670b772 Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents: 68
diff changeset
49 destination = route[-1]
88
3b86597c5a88 major update, moved the map to an osm patch
Teemu Piippo <teemu@hecknology.net>
parents: 87
diff changeset
50 route_weights = {}
22
3d094a804af8 lisää muutoksia
Teemu Piippo <teemu@hecknology.net>
parents: 21
diff changeset
51 f = lambda i: i**-0.3
71
d2e19670b772 Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents: 68
diff changeset
52 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
53 while priority(route[-1]) < 0:
71
d2e19670b772 Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents: 68
diff changeset
54 del route[-1]
87
9139a94e540c updates
Teemu Piippo <teemu@hecknology.net>
parents: 75
diff changeset
55 if not route:
9139a94e540c updates
Teemu Piippo <teemu@hecknology.net>
parents: 75
diff changeset
56 return ''
71
d2e19670b772 Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents: 68
diff changeset
57 destination = route[-1]
d2e19670b772 Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents: 68
diff changeset
58 for i, stop in enumerate(route):
5
a65b680f1774 Paljon asioita
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
59 # 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
60 i = f(i + 1) * factor
5
a65b680f1774 Paljon asioita
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
61 # 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
62 i *= priority(stop)
3b86597c5a88 major update, moved the map to an osm patch
Teemu Piippo <teemu@hecknology.net>
parents: 87
diff changeset
63 route_weights[stop] = i
5
a65b680f1774 Paljon asioita
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
64 # 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
65 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
66 route_weights[from_place] = 0
24
e6bdb9c54096 Yhtenäistetty ohjelmakoodin kieli englanniksi
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
67 # 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
68 route_weights[destination] = 1e10
24
e6bdb9c54096 Yhtenäistetty ohjelmakoodin kieli englanniksi
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
69 # muodosta sign-tiedot järjestettynä reittiarvon mukaan
e6bdb9c54096 Yhtenäistetty ohjelmakoodin kieli englanniksi
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
70 weights = sorted([
88
3b86597c5a88 major update, moved the map to an osm patch
Teemu Piippo <teemu@hecknology.net>
parents: 87
diff changeset
71 (stop, route_weights[stop], i) \
71
d2e19670b772 Remove assumptions and added some api stuff
Teemu Piippo <teemu@hecknology.net>
parents: 68
diff changeset
72 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
73 if route_weights[stop] >= 1
24
e6bdb9c54096 Yhtenäistetty ohjelmakoodin kieli englanniksi
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
74 ], key = lambda stop: -stop[1])
87
9139a94e540c updates
Teemu Piippo <teemu@hecknology.net>
parents: 75
diff changeset
75 if format == 'long':
68
382dd85b83a2 Asioita
Teemu Piippo <teemu@hecknology.net>
parents: 60
diff changeset
76 weights = weights[:4]
87
9139a94e540c updates
Teemu Piippo <teemu@hecknology.net>
parents: 75
diff changeset
77 elif format == 'short':
9139a94e540c updates
Teemu Piippo <teemu@hecknology.net>
parents: 75
diff changeset
78 weights = weights[:2]
9139a94e540c updates
Teemu Piippo <teemu@hecknology.net>
parents: 75
diff changeset
79 # repeat for the second sign value
9139a94e540c updates
Teemu Piippo <teemu@hecknology.net>
parents: 75
diff changeset
80 try:
9139a94e540c updates
Teemu Piippo <teemu@hecknology.net>
parents: 75
diff changeset
81 if weights[1][0] != destination and weights[1][1] < (500 / length ** 1.15):
9139a94e540c updates
Teemu Piippo <teemu@hecknology.net>
parents: 75
diff changeset
82 del weights[1]
9139a94e540c updates
Teemu Piippo <teemu@hecknology.net>
parents: 75
diff changeset
83 except IndexError:
9139a94e540c updates
Teemu Piippo <teemu@hecknology.net>
parents: 75
diff changeset
84 pass
68
382dd85b83a2 Asioita
Teemu Piippo <teemu@hecknology.net>
parents: 60
diff changeset
85 else:
382dd85b83a2 Asioita
Teemu Piippo <teemu@hecknology.net>
parents: 60
diff changeset
86 # enintään neljä tulee kylttiin
382dd85b83a2 Asioita
Teemu Piippo <teemu@hecknology.net>
parents: 60
diff changeset
87 weights = weights[:3]
87
9139a94e540c updates
Teemu Piippo <teemu@hecknology.net>
parents: 75
diff changeset
88 # if the third sign value is not significant enough, drop it
68
382dd85b83a2 Asioita
Teemu Piippo <teemu@hecknology.net>
parents: 60
diff changeset
89 try:
382dd85b83a2 Asioita
Teemu Piippo <teemu@hecknology.net>
parents: 60
diff changeset
90 if weights[2][0] != destination and weights[2][1] < (725 / length ** 0.8):
382dd85b83a2 Asioita
Teemu Piippo <teemu@hecknology.net>
parents: 60
diff changeset
91 del weights[2]
382dd85b83a2 Asioita
Teemu Piippo <teemu@hecknology.net>
parents: 60
diff changeset
92 except IndexError:
382dd85b83a2 Asioita
Teemu Piippo <teemu@hecknology.net>
parents: 60
diff changeset
93 pass
87
9139a94e540c updates
Teemu Piippo <teemu@hecknology.net>
parents: 75
diff changeset
94 # repeat for the second sign value
68
382dd85b83a2 Asioita
Teemu Piippo <teemu@hecknology.net>
parents: 60
diff changeset
95 try:
382dd85b83a2 Asioita
Teemu Piippo <teemu@hecknology.net>
parents: 60
diff changeset
96 if weights[1][0] != destination and weights[1][1] < (500 / length ** 1.15):
382dd85b83a2 Asioita
Teemu Piippo <teemu@hecknology.net>
parents: 60
diff changeset
97 del weights[1]
382dd85b83a2 Asioita
Teemu Piippo <teemu@hecknology.net>
parents: 60
diff changeset
98 except IndexError:
382dd85b83a2 Asioita
Teemu Piippo <teemu@hecknology.net>
parents: 60
diff changeset
99 pass
87
9139a94e540c updates
Teemu Piippo <teemu@hecknology.net>
parents: 75
diff changeset
100 # reorder to get the sign the right way around
52
cab8d38fe5c6 Kuntauudistus
Teemu Piippo <teemu@hecknology.net>
parents: 46
diff changeset
101 weights = sorted(weights, key = lambda weight_data: weight_data[2])
87
9139a94e540c updates
Teemu Piippo <teemu@hecknology.net>
parents: 75
diff changeset
102 # form the sign..
24
e6bdb9c54096 Yhtenäistetty ohjelmakoodin kieli englanniksi
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
103 sign = [paino[0] for paino in weights]
52
cab8d38fe5c6 Kuntauudistus
Teemu Piippo <teemu@hecknology.net>
parents: 46
diff changeset
104 old_sign = sign.copy()
cab8d38fe5c6 Kuntauudistus
Teemu Piippo <teemu@hecknology.net>
parents: 46
diff changeset
105 sign = []
cab8d38fe5c6 Kuntauudistus
Teemu Piippo <teemu@hecknology.net>
parents: 46
diff changeset
106 for place in old_sign:
cab8d38fe5c6 Kuntauudistus
Teemu Piippo <teemu@hecknology.net>
parents: 46
diff changeset
107 if place not in sign:
cab8d38fe5c6 Kuntauudistus
Teemu Piippo <teemu@hecknology.net>
parents: 46
diff changeset
108 sign.append(place)
24
e6bdb9c54096 Yhtenäistetty ohjelmakoodin kieli englanniksi
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
109 if whole:
52
cab8d38fe5c6 Kuntauudistus
Teemu Piippo <teemu@hecknology.net>
parents: 46
diff changeset
110 sign = [from_place] + sign
cab8d38fe5c6 Kuntauudistus
Teemu Piippo <teemu@hecknology.net>
parents: 46
diff changeset
111 if not sign:
cab8d38fe5c6 Kuntauudistus
Teemu Piippo <teemu@hecknology.net>
parents: 46
diff changeset
112 sign = [destination]
cab8d38fe5c6 Kuntauudistus
Teemu Piippo <teemu@hecknology.net>
parents: 46
diff changeset
113 return sign

mercurial