-
Notifications
You must be signed in to change notification settings - Fork 1
/
ztm.py
executable file
·163 lines (126 loc) · 3.75 KB
/
ztm.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#!/usr/bin/env python3
import logging
logging.basicConfig(level=logging.INFO)
"""
Skrypt generujący dane dla skryptów aktualizujących
dane o liniach tramwajowych i autobusowych
db/ztm-linie.json:
"1": {
"petle": [
"Junikowo",
"Franowo"
],
"strefy": [
"A"
],
"przystanki": 31,
"czas": 41,
"agency": "ZTM MPK"
}
db/ztm-ulice.json:
"Żegrze": [
1,
4,
5,
13,
17,
55,
66,
201,
245
]
db/ztm-stops.lua:
local database = {
["LUGA01"] = { "54" },
["LUGA02"] = { "54", "62", "244" },
["PILS01"] = { "52", "55", "62", "65" },
["OSWI01"] = { "52", "55", "62", "65" },
["SZII02"] = { "52", "74", "84" },
}
return database
"""
import csv
import json
# czytaj ze źródeł
with open("sources/ztm-routes.json") as f:
routes = json.load(f)
with open("sources/ztm-operators.json") as f:
try:
operators = json.load(f)
except ValueError:
operators = {'lines': []}
# generuj dane o liniach i przystankach
lines = {}
stops = {}
for line in routes['lines']:
try:
# wspieraj numery liczbowe i ze znakami, np.: T12 / 1
line['name'] = str(int(line['name']))
except:
pass
# rejestruj linię
lines[line['name']] = {
"typ": line['typ'],
"petle": line['petle'],
"kolor1": '#' + line['color1'],
"kolor2": '#' + line['color2'],
# "przystanki": line['przystanki'],
}
logging.info("Line %s: %r %s" % (line['name'], line['petle'], line['typ']))
"""
# rejestruj linię zatrzymujące się na poszczególnych przystankach
for stop in line['przystankiSymbole'].split(','):
if stop not in stops:
stops[stop] = []
line_name = str(line['name'])
if line_name not in stops[stop]:
stops[stop].append(line_name)
"""
"""
# generuj dane o przystankach (format dla skryptów LUA)
with open("db/ztm-stops.lua", "w") as lua:
lua_lines = [' ["%s"] = { "%s" }' % (stop, '", "'.join(stops[stop])) for stop in sorted(stops.keys())]
lua.write('local database = {\n%s}\n\nreturn database\n' % ',\n'.join(lua_lines))
"""
# typ / operator linii + rozkład jazdy
for line in operators['lines']:
if line['name'] in lines:
if line['night'] is True:
lines[line['name']]['night'] = True
# operator
lines[line['name']]['agency'] = line['operator'].replace('ZTM_', '')
# link do rozkładu jazdy
lines[line['name']]['rozklad'] = \
'http://ztm.poznan.pl/komunikacja/rozklad/#/kierunki/%s/linia/%s' % (line['operator'], line['name'])
# TODO: inni operatorzy
# * Kombus - http://www.kombus.pl/komunikacja
# generuj plik JSON
with open("db/ztm-linie.json", "w") as out:
# https://docs.python.org/3/library/json.html#json.dump
json.dump(lines, out, indent=2, separators=(',', ':'), sort_keys=False)
"""
# generuj dane o ulicach
stops_to_street = {}
streets = {}
with open("sources/ztm-stops.tsv", "r") as f:
stops = csv.reader(f, delimiter="\t")
for stop in stops:
# tylko przystanki w Poznaniu
if stop[4] != 'Poznań':
continue
# ID przystanku -> ulica / lokalizacja
stops_to_street[stop[0]] = stop[5]
for line in routes['lines']:
entry = []
stops = line['przystankiSymbole'].split(',')
for stop in stops:
if stop in stops_to_street:
street = stops_to_street[stop]
if street not in streets:
streets[street] = set()
streets[street].add(line['name'])
for street in streets:
streets[street] = sorted(list(streets[street]))
with open("db/ztm-ulice.json", "w") as out:
json.dump(streets, out, indent=2, separators=(',', ': '), sort_keys=True)
"""