forked from CokeStudios/mtr-pathfinder
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmtr_pathfinder_v4.py
More file actions
1254 lines (1029 loc) · 42.3 KB
/
Copy pathmtr_pathfinder_v4.py
File metadata and controls
1254 lines (1029 loc) · 42.3 KB
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
'''
Find paths between two stations for Minecraft Transit Railway.
'''
from array import array
from datetime import datetime, timedelta, timezone
from difflib import SequenceMatcher
from enum import Enum
from io import BytesIO
from math import gcd, sqrt
from operator import itemgetter
from time import gmtime, strftime, time
from typing import Optional, Dict, Literal, Tuple, List, Union
import base64
import hashlib
import json
import mmap
import os
import pickle
import re
from fontTools.ttLib import TTFont
from opencc import OpenCC
from PIL import Image, ImageDraw, ImageFont
import requests
__version__ = '130'
MAX_INT = 2 ** 64 - 1
RUNNING_SPEED: int = 5.612 # 站内换乘速度,单位 block/s
TRANSFER_SPEED: int = 4.317 # 出站换乘速度,单位 block/s
WILD_WALKING_SPEED: int = 2.25 # 非出站换乘(越野)速度,单位 block/s
opencc1 = OpenCC('s2t')
opencc2 = OpenCC('t2jp')
opencc3 = OpenCC('t2s')
opencc4 = OpenCC('jp2t')
def get_close_matches(words, possibilities, cutoff=0.2):
result = [(-1, None)]
s = SequenceMatcher()
for word in words:
s.set_seq2(word)
for x, y in possibilities:
s.set_seq1(x)
if s.real_quick_ratio() >= cutoff and \
s.quick_ratio() >= cutoff:
ratio = s.ratio()
if ratio >= cutoff:
result.append((ratio, y))
return max(result)[1]
# From https://github.com/TrueMyst/PillowFontFallback/blob/main/fontfallback/writing.py
def load_fonts(*font_paths: str) -> Dict[str, TTFont]:
"""
Loads font files specified by paths into memory and returns a dictionary of font objects.
"""
fonts = {}
for path in font_paths:
font = TTFont(path)
fonts[path] = font
return fonts
# From https://github.com/TrueMyst/PillowFontFallback/blob/main/fontfallback/writing.py
def has_glyph(font: TTFont, glyph: str) -> bool:
"""
Checks if the given font contains a glyph for the specified character.
"""
for table in font["cmap"].tables:
if table.cmap.get(ord(glyph)):
return True
return False
# From https://github.com/TrueMyst/PillowFontFallback/blob/main/fontfallback/writing.py
def merge_chunks(text: str, fonts: Dict[str, TTFont]) -> List[List[str]]:
"""
Merges consecutive characters with the same font into clusters, optimizing font lookup.
"""
chunks = []
for char in text:
for font_path, font in fonts.items():
if has_glyph(font, char):
chunks.append([char, font_path])
break
cluster = chunks[:1]
for char, font_path in chunks[1:]:
if cluster[-1][1] == font_path:
cluster[-1][0] += char
else:
cluster.append([char, font_path])
return cluster
# From https://github.com/TrueMyst/PillowFontFallback/blob/main/fontfallback/writing.py
def draw_text_v2(
draw: ImageDraw.ImageDraw,
xy: Tuple[int, int],
text: str,
color: Tuple[int, int, int],
fonts: Dict[str, TTFont],
size: int,
anchor: Optional[str] = None,
align: Literal["left", "center", "right"] = "left",
direction: Literal["rtl", "ltr", "ttb"] = "ltr",
) -> None:
"""
Draws text on an image at given coordinates, using specified size, color, and fonts.
"""
y_offset = 0
sentence = merge_chunks(text, fonts)
for words in sentence:
xy_ = (xy[0] + y_offset, xy[1] - 6)
font = ImageFont.truetype(words[1], size)
draw.text(
xy=xy_,
text=words[0],
fill=color,
font=font,
anchor=anchor,
align=align,
direction=direction,
embedded_color=True,
)
# draw.text
box = font.getbbox(words[0])
y_offset += box[2] - box[0]
# From https://github.com/trainline-eu/csa-challenge/blob/2aa0fa55e466692d404d87aa2dcaf5b83bca5920/csa.py
# class Connection:
# def __init__(self, departure_station_id: str, arrival_station_id: str,
# departure_timestamp: int, arrival_timestamp: int,
# route_detail):
# self[0] = int('0x' + departure_station_id, 16)
# self[1] = int('0x' + arrival_station_id, 16)
# self[2] = departure_timestamp
# self[3] = arrival_timestamp
# self.riding_time = arrival_timestamp - departure_timestamp
# self[4] = route_detail
# self.station_count = 1
# def __str__(self):
# return ' '.join([str(x) for x in
# (self[4],
# self[0], self[1],
# self[2], self[3],
# self.station_count)])
# From https://github.com/trainline-eu/csa-challenge/blob/2aa0fa55e466692d404d87aa2dcaf5b83bca5920/csa.py and https://ljn.io/posts/connection-scan-algorithm-with-interchange-time
class CSA:
def __init__(self, max_stations, connections: list[tuple], timeout_min=2):
self.in_connection = array('L')
self.earliest_arrival = array('L')
self.max_stations = max_stations
self.connections: list[tuple] = connections
self.timeout_min = timeout_min
def main_loop(self, arrival_station):
earliest = MAX_INT
for i, c in enumerate(self.connections):
if c[2] >= self.earliest_arrival[c[0]] and c[3] < self.earliest_arrival[c[1]]:
self.earliest_arrival[c[1]] = c[3]
self.in_connection[c[1]] = i
if c[1] == arrival_station:
earliest = min(earliest, c[3])
elif c[2] >= earliest:
return
if i % 20000 == 0:
if time() > self.start_time + 60 * self.timeout_min:
raise TimeoutError('Pathfinding timeout')
def find_path(self, arrival_station):
route = []
if self.in_connection[arrival_station] != MAX_INT:
last_connection_index = self.in_connection[arrival_station]
while last_connection_index != MAX_INT:
connection = self.connections[last_connection_index]
route.append(connection)
last_connection_index = self.in_connection[connection[0]]
route.reverse()
return route
def compute(self, departure_station, arrival_station, departure_time) -> list[tuple]:
self.in_connection = array('Q', [MAX_INT for _ in range(self.max_stations)])
self.earliest_arrival = array('Q', [MAX_INT for _ in range(self.max_stations)])
self.earliest_arrival[departure_station] = departure_time
if departure_station <= self.max_stations and arrival_station <= self.max_stations:
self.start_time = time()
self.main_loop(arrival_station)
return self.find_path(arrival_station)
class RouteType(Enum):
'''
An Enum class to define the types of the route.
'''
IN_THEORY = 0
WAITING = 1
REAL_TIME = 2
class ImagePattern(Enum):
'''
An Enum class to define the patterns of the image.
Number -> x offset
THUMB -> need to -20
'''
OR = 0
FAKE_STATION = 1
TEXT = 40.2
STATION = 40 # 圆圈 + 黑体字 -> 车站
THUMB_TEXT = 60 # 路线种类图标 + 灰字 -> 路线名
THUMB_INTEND_TEXT = 80
GREY_TEXT = 40.1
GREY_INTEND_TEXT = 60.1
def round_ten(n: float) -> int:
'''
Round the number in ten.
'''
ans = round(n / 10) * 10
return ans if ans > 0 else 0
def atoi(text: str) -> Union[str, int]:
'''
Convert a string to a digit.
'''
return int(text) if text.isdigit() else text
def natural_keys(text: str) -> list:
'''
A sorting key in number order.
'''
return [atoi(c) for c in re.split(r'(\d+)', text)]
def lcm(a: int, b: int) -> int:
'''
Calculate LCM of two integers.
'''
return a * b // gcd(a, b)
def get_distance(a_dict: dict, b_dict: dict, square: bool = False) -> float:
'''
Get the distance of two stations.
'''
dist_square = (a_dict['x'] - b_dict['x']) ** 2 + \
(a_dict['z'] - b_dict['z']) ** 2
if square is True:
return dist_square
return sqrt(dist_square)
def fetch_data(link: str, LOCAL_FILE_PATH, MAX_WILD_BLOCKS) -> str:
'''
Fetch all the route data and station data.
'''
link = link.rstrip('/') + '/mtr/api/map/stations-and-routes?dimension=0'
data = requests.get(link).json()['data']
data_new = {'stations': {}, 'routes': {},
'station_coords': {}, 'station_routes': {},
'transfer_time': {}, 'transfer_dist': {}}
for d in data['routes']:
data_new['routes'][d['id']] = d
lengths = []
last_x = None
for x in d['stations']:
if x['id'] in data_new['station_routes']:
data_new['station_routes'][x['id']] += [d['id']]
else:
data_new['station_routes'][x['id']] = [d['id']]
if last_x is not None:
x1 = last_x['x']
y1 = last_x['y']
z1 = last_x['z']
x2 = x['x']
y2 = x['y']
z2 = x['z']
lengths.append(((x1 - x2) ** 2 + (y1 - y2) ** 2 +
(z1 - z2) ** 2) ** 0.5)
last_x = x
data_new['routes'][d['id']]['lengths'] = lengths
i = 0
for d in data['stations']:
if d['id'] not in data_new['station_routes']:
continue
d['station'] = hex(i)[2:]
data_new['stations'][d['id']] = d
i += 1
x_dict = {x['id']: [] for x in data['stations']}
y_dict = {x['id']: [] for x in data['stations']}
z_dict = {x['id']: [] for x in data['stations']}
for route in data['routes']:
for station in route['stations']:
x_dict[station['id']] += [station['x']]
y_dict[station['id']] += [station['y']]
z_dict[station['id']] += [station['z']]
for station in data['stations']:
x_list = x_dict[station['id']]
y_list = y_dict[station['id']]
z_list = z_dict[station['id']]
if len(x_list) == 0:
continue
data_new['station_coords'][station['id']] = \
{'x': sum(x_list) / len(x_list),
'y': sum(y_list) / len(y_list),
'z': sum(z_list) / len(z_list)}
for x, dict1 in data_new['station_coords'].items():
for y, dict2 in data_new['station_coords'].items():
if x == y:
continue
distance = get_distance(dict1, dict2)
if x == y:
speed = RUNNING_SPEED
elif x in data_new['stations'][y]['connections'] or \
y in data_new['stations'][x]['connections']:
speed = TRANSFER_SPEED
else:
speed = WILD_WALKING_SPEED
if distance > MAX_WILD_BLOCKS:
continue
if abs(dict1['x'] - dict2['x']) > MAX_WILD_BLOCKS or \
abs(dict1['z'] - dict2['z']) > MAX_WILD_BLOCKS:
continue
time = distance / speed
if x not in data_new['transfer_time']:
data_new['transfer_time'][x] = {}
data_new['transfer_time'][x][y] = time
if x not in data_new['transfer_dist']:
data_new['transfer_dist'][x] = {}
data_new['transfer_dist'][x][y] = distance
y = input(f'是否替换{LOCAL_FILE_PATH}文件? (Y/N) ').lower()
if y == 'y':
with open(LOCAL_FILE_PATH, 'w', encoding='utf-8') as f:
json.dump(data_new, f)
return data_new
def gen_departure(link: str, DEP_PATH) -> None:
'''
Download the departures.
'''
link = link.rstrip('/') + '/mtr/api/map/departures?dimension=0'
data = requests.get(link).json()['data']
departures = data['departures']
offset = data['cachedResponseTime']
dep_dict: dict[str, list[int]] = {}
for x in departures:
dep_list = set()
for y in x['departures']:
# deviation = y['deviation']
for z in y['departures']:
# dep = round((z + offset % 86400000 + deviation) / 1000)
dep = round((z + offset % 86400000) / 1000) % 86400
dep_list.add(dep)
dep_list = list(sorted(dep_list))
dep_dict[x['id']] = dep_list
y = input(f'是否替换{DEP_PATH}文件? (Y/N) ').lower()
if y == 'y':
with open(DEP_PATH, 'w', encoding='utf-8') as f:
json.dump(dep_dict, f)
def station_name_to_id(data: dict, sta: str, STATION_TABLE,
fuzzy_compare=True) -> str:
'''
Convert one station's name to its ID.
'''
sta = sta.lower()
if sta in STATION_TABLE:
sta = STATION_TABLE[sta]
tra1 = opencc1.convert(sta)
sta_try = [sta, tra1, opencc2.convert(tra1)]
all_names = []
stations = data['stations']
output = None
has_station = False
for station_id, station_dict in stations.items():
s_1 = station_dict['name']
all_names.append((s_1, station_id))
s_split = station_dict['name'].split('|')
s_2_2 = s_split[-1]
s_2 = s_2_2.split('/')[-1]
s_3 = s_split[0]
for st in sta_try:
if st in (s_1.lower(), s_2.lower(), s_2_2.lower(), s_3.lower()):
has_station = True
output = station_id
break
if has_station is False and fuzzy_compare is True:
return get_close_matches(sta_try, all_names)
return output
def station_num_to_name(data: dict, sta: str) -> str:
'''
Convert one station's code (str of base-10 int) to its name.
'''
sta = hex(int(sta))[2:]
for station in data['stations'].values():
if station['station'] == sta:
return station['name']
def sta_id(station: str) -> int:
return int('0x' + station, 16)
def check_route_name(route_data, IGNORED_LINES: list[str],
ONLY_LINES: list[str] = None):
if ONLY_LINES is None:
ONLY_LINES = []
if ONLY_LINES:
IGNORED_LINES = []
lines_to_check = [x.lower().strip()
for x in IGNORED_LINES + ONLY_LINES if x != '']
n: str = route_data['name']
number: str = route_data['number']
route_names = [n, n.split('|')[0], n.split('||')[0]]
if ('||' in n and n.count('|') > 2) or \
('||' not in n and n.count('|') > 0):
eng_name = n.split('|')[1].split('|')[0]
if eng_name != '':
route_names.append(eng_name)
if number not in ['', ' ']:
for tmp_name in route_names[1:]:
route_names.append(tmp_name + ' ' + number)
cont = False
for x in route_names:
x = x.lower().strip()
if x in lines_to_check:
cont = True
break
if x.isascii():
continue
simp1 = opencc3.convert(x)
if simp1 in lines_to_check:
cont = True
break
simp2 = opencc3.convert(opencc4.convert(x))
if simp2 in lines_to_check:
cont = True
break
if ONLY_LINES:
cont = not cont
return cont
def gen_timetable(data: dict, IGNORED_LINES: list[str], ONLY_LINES: list[str],
CALCULATE_HIGH_SPEED: bool, CALCULATE_BOAT: bool,
CALCULATE_WALKING_WILD: bool, ONLY_LRT: bool,
AVOID_STATIONS: list, route_type: RouteType,
original_ignored_lines: list[str], DEP_PATH: str,
version1: str, version2: str,
STATION_TABLE, WILD_ADDITION, TRANSFER_ADDITION
) -> list[tuple]:
'''
Generate the timetable of all routes.
'''
if not os.path.exists('mtr_pathfinder_temp'):
os.makedirs('mtr_pathfinder_temp')
with open(DEP_PATH, 'r', encoding='utf-8') as f:
dep_data: dict[str, list[int]] = json.load(f)
filename = ''
m = hashlib.md5()
if IGNORED_LINES == original_ignored_lines and \
CALCULATE_BOAT is True and ONLY_LRT is False and \
AVOID_STATIONS == [] and ONLY_LINES == [] and \
route_type == RouteType.REAL_TIME:
for s in original_ignored_lines:
m.update(s.encode('utf-8'))
filename = f'mtr_pathfinder_temp{os.sep}' + \
f'4{int(CALCULATE_HIGH_SPEED)}{int(CALCULATE_WALKING_WILD)}' + \
f'-{version1}-{version2}-{m.hexdigest()}-{__version__}.dat'
if os.path.exists(filename):
with open(filename, 'r+b') as f:
mmapped_file = mmap.mmap(f.fileno(), 0)
tt_dict = pickle.load(mmapped_file)
return tt_dict
avoid_ids = [station_name_to_id(data, x, STATION_TABLE)
for x in AVOID_STATIONS]
# 添加普通路线
tt_dict = {}
for route_id in dep_data.keys():
if route_id not in data['routes']:
continue
route = data['routes'][route_id]
# 禁路线
if check_route_name(route, IGNORED_LINES, ONLY_LINES) is True:
continue
if (not CALCULATE_HIGH_SPEED) and route['type'] == 'train_high_speed':
continue
if (not CALCULATE_BOAT) and 'boat' in route['type']:
continue
if ONLY_LRT and route['type'] != 'train_light_rail':
continue
durations = route['durations']
if durations == []:
continue
station_ids = [data['stations'][x['id']]['station']
for x in route['stations']]
if len(station_ids) - 1 < len(durations):
durations = durations[:len(station_ids) - 1]
if len(station_ids) - 1 > len(durations):
continue
real_ids = [x['id'] for x in route['stations']]
platforms = [x['name'] for x in route['stations']]
dwells = [x['dwellTime'] for x in route['stations']]
if len(dwells) > 0:
dep = -round(dwells[-1] / 1000)
else:
dep = 0
tt = []
for i in range(len(station_ids) - 1, 0, -1):
station1 = station_ids[i - 1]
station2 = station_ids[i]
_station1 = real_ids[i - 1]
_station2 = real_ids[i]
platform = platforms[i - 1]
dur = round(durations[i - 1] / 1000)
arr_time = dep
dep_time = dep - dur
dwell = round(dwells[i - 1] / 1000)
dep -= dur
dep -= dwell
if station1 == station2:
continue
if _station2 in avoid_ids:
continue
if _station1 not in avoid_ids and _station2 not in avoid_ids:
tt.append((sta_id(station1), sta_id(station2),
dep_time, arr_time,
[route_id, route['stations'][-1]['id'], platform]))
# 添加出站换乘
connections = data['stations'][_station2]['connections']
if _station2 in TRANSFER_ADDITION:
connections += data['stations'][TRANSFER_ADDITION[_station2]]
for con in connections:
if con in avoid_ids:
continue
if _station2 not in data['transfer_time']:
continue
if con not in data['transfer_time'][_station2]:
continue
t2 = round(data['transfer_time'][_station2][con])
dist = data['transfer_dist'][_station2][con]
con = data['stations'][con]['station']
tt.append((sta_id(station2), sta_id(con),
arr_time, arr_time + t2,
[f'出站换乘步行 Walk {round(dist, 2)}m', '', None]))
if CALCULATE_WALKING_WILD is True:
# 添加非出站换乘(越野)
connections = list(data['stations'].keys())
if _station2 in WILD_ADDITION:
connections += data['stations'][WILD_ADDITION[_station2]]
for con in connections:
if con in avoid_ids:
continue
if _station2 not in data['transfer_time']:
continue
if con not in data['transfer_time'][_station2]:
continue
t2 = round(data['transfer_time'][_station2][con])
dist = data['transfer_dist'][_station2][con]
con = data['stations'][con]['station']
tt.append((sta_id(station2), sta_id(con),
arr_time, arr_time + t2,
[f'步行 Walk {round(dist, 2)}m', '', None]))
tt_dict[route_id] = tt
if filename != '':
if not os.path.exists(filename):
with open(filename, 'wb') as f:
pickle.dump(tt_dict, f)
return tt_dict
def load_tt(tt_dict: dict[tuple], data, start, end, departure_time: int,
DEP_PATH, STATION_TABLE, TRANSFER_ADDITION,
CALCULATE_WALKING_WILD, WILD_ADDITION, MAX_HOUR):
with open(DEP_PATH, 'r', encoding='utf-8') as f:
dep_data: dict[str, list[int]] = json.load(f)
timetable: list[tuple] = []
start_station = station_name_to_id(data, start, STATION_TABLE)
end_station = station_name_to_id(data, end, STATION_TABLE)
if not (start_station and end_station):
return []
# 添加起点出站换乘
ss = data['stations'][start_station]['station']
connections = data['stations'][start_station]['connections']
if start in TRANSFER_ADDITION:
connections += data['stations'][TRANSFER_ADDITION[start]]
for con in connections:
if con not in data['transfer_time'][start_station]:
continue
t2 = round(data['transfer_time'][start_station][con])
dist = data['transfer_dist'][start_station][con]
con = data['stations'][con]['station']
timetable.append(
(sta_id(ss), sta_id(con),
departure_time, departure_time + t2,
[f'出站换乘步行 Walk {round(dist, 2)}m', '', None]))
if CALCULATE_WALKING_WILD is True:
# 添加起点非出站换乘(越野)
connections = list(data['stations'].keys())
if start in WILD_ADDITION:
connections += data['stations'][WILD_ADDITION[start]]
for con in connections:
if start_station not in data['transfer_time']:
continue
if con not in data['transfer_time'][start_station]:
continue
t2 = round(data['transfer_time'][start_station][con])
dist = data['transfer_dist'][start_station][con]
con = data['stations'][con]['station']
timetable.append(
(sta_id(ss), sta_id(con),
departure_time, departure_time + t2,
[f'步行 Walk {round(dist, 2)}m', '', None]))
max_time = departure_time + MAX_HOUR * 60 * 60
trips: dict[str, dict[str, int]] = {}
trip_no = 0
for route_id, departures in dep_data.items():
if route_id not in tt_dict:
continue
if max_time > 86400:
departures += [x + 86400 for x in departures
if x <= max_time - 86400]
tt = tt_dict[route_id]
for departure in departures:
if departure >= max_time:
break
trips[str(trip_no)] = {}
for t in tt:
_t = list(t)
_t[2] += departure
_t[3] += departure
if _t[2] < 0:
_t[2] += 86400
_t[3] += 86400
if max_time - 86400 < _t[2] < departure_time:
continue
if _t[4][1] != '': # Not walking
_t += [trip_no]
trips[str(trip_no)][str(_t[0])] = _t[2]
timetable.append(_t)
# if max_time > 86400 and departure <= max_time - 86400:
# _t[2] += 86400
# _t[3] += 86400
# if _t[4][1] != '': # Not walking
# trips[str(trip_no)][str(_t[0])] = _t[2]
# timetable.append(_t)
trip_no += 1
# IMPORTANT !!! Connections must be sorted by departure/arrival time.
timetable.sort(key=itemgetter(2))
return timetable, trips
def process_path(result: list[tuple], start: str, end: str,
trips: dict[str, dict[str, int]], data: dict, detail: bool,
STATION_TABLE) -> list[str, int, int, int, list]:
'''
Process the path, change it into human readable form.
'''
start_station = station_name_to_id(data, start, STATION_TABLE)
end_station = station_name_to_id(data, end, STATION_TABLE)
if not (start_station and end_station):
return None, None, None, None, None
if start_station == end_station:
return None, None, None, None, None
path: list[tuple] = []
last_detail: tuple = None
route_new = []
low_i = MAX_INT
for i in range(len(result) - 1, -1, -1):
new_leg = result[i]
if i >= low_i:
continue
if len(new_leg) < 6:
route_new.append(new_leg)
continue
trip = trips[str(new_leg[5])]
for j in range(i - 1, -1, -1):
old_leg = result[j]
trip_index = str(old_leg[0])
if trip_index not in trip:
continue
if trip[trip_index] >= old_leg[2]:
new_leg = [trip_index, new_leg[1], trip[trip_index],
new_leg[3], new_leg[4], new_leg[5]]
low_i = j
route_new.append(new_leg)
route_new.reverse()
for con in route_new:
if con[4][:2] != last_detail or detail is True:
path.append(con)
else:
last_con = path[-1]
last_con[3] = con[3]
last_con[1] = con[1]
last_detail = con[4]
stations = data['stations']
every_route_time = []
for x in path:
station_1 = x[0]
station_2 = x[1]
sta1_name = station_num_to_name(data, station_1)
sta1_id = station_name_to_id(data, sta1_name, {}, False)
sta1_name = sta1_name.replace('|', ' ')
sta2_name = station_num_to_name(data, station_2)
sta2_id = station_name_to_id(data, sta2_name, {}, False)
sta2_name = sta2_name.replace('|', ' ')
route_name = x[4][0]
if route_name in data['routes']:
z: dict[str, list] = data['routes'][route_name]
route_name = data['routes'][route_name]['name']
route = (z['number'] + ' ' + route_name.split('||')[0]).strip()
route = route.replace('|', ' ')
terminus_name: str = stations[x[4][1]]['name']
if terminus_name.count('|') == 0:
t1_name = t2_name = terminus_name
else:
t1_name = terminus_name.split('|')[0]
t2_name = terminus_name.split('|')[1].replace('|', ' ')
if z['circularState'] == 'CLOCKWISE':
t1_name = '(顺时针) ' + t1_name
t2_name += ' (Clockwise)'
elif z['circularState'] == 'ANTICLOCKWISE':
t1_name = '(逆时针) ' + t1_name
t2_name += ' (Anticlockwise)'
terminus = (t1_name, t2_name)
i2 = 0
sta_ids = [x['id'] for x in z['stations']]
while True:
try:
sta2_index = sta_ids.index(sta2_id, i2)
i2 = sta2_index + 1
try:
sta1_index = [i for (i, x) in enumerate(sta_ids[:i2])
if x == sta1_id][-1]
except ValueError:
pass
else:
platform = z['stations'][sta1_index]['name']
break
except ValueError:
platform = None
break
color = hex(z['color']).lstrip('0x').rjust(6, '0')
train_type = z['type']
else:
color = '000000'
route = route_name
terminus = (route_name.split(',用时')[0], 'Walk')
train_type = None
platform = None
color = '#' + color
r = (sta1_name, sta2_name, color, route, terminus,
x[2], x[3], train_type, platform, route_name)
every_route_time.append(r)
return every_route_time
def save_image(route_type: RouteType, every_route_time: list,
BASE_PATH, version1, version2,
PNG_PATH, departure_time,
show=False) -> tuple[Image.Image, str]:
'''
Save the image of the route.
'''
pattern = []
pattern.append(
(ImagePattern.TEXT,
str(strftime('%H:%M:%S', gmtime(departure_time))))) # 出发时间
time_img = Image.open(PNG_PATH + os.sep + 'time.png')
for route_data in every_route_time:
route_img = Image.open(PNG_PATH + os.sep + f'{route_data[7]}.png')
terminus = route_data[4][0] + '方向 To ' + route_data[4][1]
total_time = route_data[6] - route_data[5]
time1 = str(strftime('%H:%M:%S', gmtime(route_data[5])))
time2 = str(strftime('%H:%M:%S', gmtime(route_data[6])))
time3 = str(strftime('%H:%M:%S', gmtime(total_time)))
if int(time3.split(':', maxsplit=1)[0]) == 0:
time3 = ''.join(time3.split(':', maxsplit=1)[1:])
pattern.append((ImagePattern.STATION, route_data[0],
route_data[2])) # 车站
pattern.append((ImagePattern.TEXT, time1)) # 发车时间
pattern.append((ImagePattern.THUMB_TEXT, route_img,
route_data[3])) # 路线名
if route_data[7] is not None:
# 正常
pattern.append((ImagePattern.GREY_TEXT, terminus)) # 方向
colour = 'grey'
pattern.append((ImagePattern.THUMB_TEXT, time_img,
time3, colour)) # 用时
pattern.append((ImagePattern.TEXT, time2)) # 到站时间
pattern.append((ImagePattern.STATION, route_data[1], route_data[2]))
# full_time = every_route_time[-1][6] - every_route_time[0][5]
# 总时长从出发时间开始算,不从发车时间开始算
full_time = every_route_time[-1][6] - departure_time
return generate_image(pattern, route_type, BASE_PATH,
version1, version2, full_time, show)
def calculate_height_width(pattern: list[list[ImagePattern]],
route_type, final_str: str,
final_str_size: int, BASE_PATH) -> tuple[int]:
'''
Calculate the width and the height of the image.
'''
text_size = 20
font = ImageFont.truetype(BASE_PATH + os.sep + 'fonts' + os.sep +
'NotoSansKR-Regular.ttf',
size=text_size)
font2 = ImageFont.truetype(BASE_PATH + os.sep + 'fonts' + os.sep +
'NotoSansKR-Regular.ttf',
size=final_str_size)
route_len_list = [font.getlength(x[1]) + int(x[0].value) for x in pattern
if x[0] not in
[ImagePattern.FAKE_STATION, ImagePattern.OR,
ImagePattern.THUMB_TEXT,
ImagePattern.THUMB_INTEND_TEXT]]
route_len_list += [font.getlength(x[2]) + int(x[0].value) for x in pattern
if x[0] in [ImagePattern.THUMB_TEXT,
ImagePattern.THUMB_INTEND_TEXT]]
if route_type != RouteType.IN_THEORY:
len_final_str = font2.getlength(final_str) + 40
if max(route_len_list) > len_final_str:
width = round(max(route_len_list))
else:
width = round(len_final_str)
else:
width = round(max(route_len_list))
height = (len([x for x in pattern
if x[0] not in [ImagePattern.FAKE_STATION,
ImagePattern.OR]]) + 1) * 30 + 48 + 10
return (width + 10, height)
def draw_text(draw: ImageDraw.ImageDraw, xy: tuple[int, int], text: str,
color: tuple[int, int, int],
fonts: list[ImageFont.FreeTypeFont, dict[str, TTFont]],
size: int) -> None:
for x in text:
if ord(x) >= 128:
break
else:
draw.text((xy[0], xy[1] - 7), text, color, fonts[0]) # - 6
return
draw_text_v2(draw, xy, text, color, fonts[1], size)
def generate_image(pattern, route_type, BASE_PATH, version1, version2,
shortest_distance,
show: bool = False) -> tuple[Image.Image, str]:
'''
Generate the image with PIL.
'''
font_list = [BASE_PATH + x
for x in (
os.sep + 'fonts' + os.sep + "NotoSansSC-Regular.ttf",
os.sep + 'fonts' + os.sep + "NotoSansTC-Regular.ttf",
os.sep + 'fonts' + os.sep + "NotoSansHK-Regular.ttf",
os.sep + 'fonts' + os.sep + "NotoSansJP-Regular.ttf",
os.sep + 'fonts' + os.sep + "NotoSansKR-Regular.ttf",
os.sep + 'fonts' + os.sep + "NotoSansArabic-Regular.ttf",
os.sep + 'fonts' + os.sep +
"NotoSansThaiLooped-Regular.ttf",
)
]
font = ImageFont.FreeTypeFont(font_list[0], 20)
fonts = (font, load_fonts(*font_list))
gm_full = gmtime(shortest_distance)
full_time = str(strftime('%H:%M:%S', gm_full))
final_str = f'车站数据版本 Station data version: {version1}'