-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmap_pipeline.py
More file actions
71 lines (66 loc) · 3.07 KB
/
Copy pathmap_pipeline.py
File metadata and controls
71 lines (66 loc) · 3.07 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
"""地图前处理流水线。只消费解析后的 Transaction,可安全放入 Qt 后台线程。"""
from __future__ import annotations
import cn_network
import station_data
def resolve_transactions(transactions, station_db, cancelled=None):
cancelled = cancelled or (lambda: False)
rows = list(transactions)
jp = [t for t in rows if t.country == "日本" and t.in_station and t.out_station
and t.in_station != t.out_station]
# 同一批历史会反复出现相同站名/区间;单次任务内缓存解析结果。
plain = {}
if station_db:
for t in jp:
if cancelled():
return []
for name in (t.in_station, t.out_station):
if name not in plain:
plain[name] = station_db.resolve(name)
pre = [(plain.get(t.in_station), plain.get(t.out_station)) for t in jp]
pre = [(a, b) for a, b in pre if a and b]
anchor = None
if pre:
anchor = (sum(a[0] + b[0] for a, b in pre) / (2 * len(pre)),
sum(a[1] + b[1] for a, b in pre) / (2 * len(pre)))
anchored = {}
cn_routes = {}
trips = []
for t in rows:
if cancelled():
return []
if not (t.in_station and t.out_station and t.in_station != t.out_station):
continue
if t.country == "中国":
if t.in_lat is None or t.out_lat is None:
continue
route_key = (t.city, t.in_station, t.out_station)
if route_key not in cn_routes:
try:
cn_routes[route_key] = cn_network.get_router(t.city).route(
t.in_station, t.out_station)
except Exception:
cn_routes[route_key] = None
route = cn_routes[route_key]
if not route or len(route) < 2 or any("�" in (name or "") for name, _la, _lo in route):
continue
path = [{"name": name, "lat": lat, "lon": lon} for name, lat, lon in route]
trip = {"country": "中国", "in": route[0][0], "out": route[-1][0],
"ci": (route[0][1], route[0][2]), "co": (route[-1][1], route[-1][2]),
"path": path, "line": t.line, "city_code": t.city,
"lines_in": [], "lines_out": [],
"_place_key": "CN:" + (t.city or "0000")}
else:
if not station_db:
continue
for name in (t.in_station, t.out_station):
if name not in anchored:
anchored[name] = station_db.resolve(name, anchor)
a, b = anchored.get(t.in_station), anchored.get(t.out_station)
if not (a and b):
continue
trip = {"country": "日本", "in": t.in_station, "out": t.out_station,
"ci": (a[0], a[1]), "co": (b[0], b[1]), "path": None, "line": t.line,
"lines_in": station_db.lines_of(a), "lines_out": station_db.lines_of(b),
"_place_key": "JP:" + station_data.region_of_pref(a[2])}
trips.append(trip)
return trips