Schedules — กติกา "จุดนี้ = เวลาจุดอ้างอิง ± n นาที"
- ฟิลด์ derive { from_station_id, offset_min } คำนวณที่ ui/partials/schedules.js (ท่อเดียว) รองรับอ้างจุดข้ามทิศ
- จุดย่อยของ "8 other_stop" ใช้ offsets_min จับคู่ตามลำดับ (เช่น ตลาดสามโก้ = ตลาดท่าช้าง +45 นาที)
- เครื่องมือ resource/tools/derive-times.py + ตาราง resource/data/derive-times.csv (--check / --audit ชี้จุดที่ควรตั้งกฎ)
- แยกตารางเป็นรายสาย: scheduleslist/schedules<route_id>.json และ schedules.json เป็นสารบัญ (โหลดแบบ lazy + cache)
- ตรวจจับวันทำงาน/วันหยุด + schema 3 (weekday/holiday → directions) · ไฟล์ข้อมูลเป็น CRLF ทั้งชุด
- ล้างค่าที่ไม่ใช่ HH:MM ที่ฝังใน times (ทำให้ KPI เพี้ยน) + กันค่าหลอกในตัวสรุปเวลา
UI
- หน้ารายละเอียดป้าย/ตารางเวลา ขับเคลื่อนด้วย JSON (stopDetail.init.js) · จุดขึ้นรถประจำทางอื่นๆ รองรับหลายจุดต่อสาย
- #/stops: แสดง 6 ป้ายแรก + ปุ่ม "แสดงป้ายทั้งหมด" (โหลดรูปเฉพาะที่แสดง) · ชิปสาย → #/routes/<route_id>
- หน้าแรก: ตัดตัวเลือก "ทุกสถานีในจังหวัดนี้" · ตัวกรองจังหวัด/สถานี + ปุ่มแชร์ (share URL)
- การ์ดติดต่อ: ไอคอน/ชื่อ/สีเปลี่ยนตามชนิด URL (Facebook / LINE / เว็บไซต์) + ปุ่มเปิดลิงก์จาก web_url
- รูปรองรับหลายนามสกุล (.jpg → .jpeg → .webp → .png) · ป้าย "ประมาณ" + หมายเหตุสำหรับตารางที่คำนวณ
- หน้าเครดิตเหลือ 3 หมวด · แก้ข้อความหมวดแผนที่ให้ตรงกับข้อมูลจริง
- แผนที่ Leaflet (vendor ในโปรเจกต์ ไม่ใช้ CDN): เส้นทาง + หมุด + ป๊อปอัป · แก้ CSS ที่ทำให้ SVG ยุบ 0x0
เครื่องมือและเอกสาร
- qgis-to-web.py อ่าน .shp ตรง → geojson-web (สร้างใหม่ 10 ไฟล์) + audit-geojson.py ตรวจ 3 ส่วน
- stop-km.py วัดระยะ กม. ตามเส้นจริง → stop-km.json · schedules-editor.html (แก้ทีละไฟล์ · มุมมองตารางแบบ Excel)
- AGENTS.md: กติกา derive · offsets_min · นามสกุลรูป · ชิปสาย · หน้ารายละเอียดป้ายมาจากไหน
180 lines
8.9 KiB
Python
180 lines
8.9 KiB
Python
#!/usr/bin/env python3
|
|
# -*- coding: utf-8 -*-
|
|
"""derive-times.py — เติมกฎ "จุดนี้ = เวลาจุดอ้างอิง ± n นาที" ให้ไฟล์สาย (resource/data/scheduleslist/)
|
|
|
|
แนวคิด: เขียนกฎครั้งเดียวเป็นตาราง (resource/data/derive-times.csv) แล้วรันสคริปต์นี้
|
|
→ เติมบล็อก "derive" ให้จุดเป้าหมาย + ลบเวลาที่คัดลอกมาทิ้ง (กันข้อมูลไม่ตรงกัน)
|
|
ไม่ต้องจดจำไวยากรณ์ JSON และไม่ต้องแก้ทีละสายด้วยมือ
|
|
|
|
ตาราง (CSV, คั่นด้วยจุลภาค — บรรทัดขึ้นต้น # = ความเห็น):
|
|
route_id, station_id, from_station_id, offset_min, note
|
|
902, 15, 6, 30, รอรถที่ 7-11 หน้ากินซ่า
|
|
|
|
วิธีใช้:
|
|
python resource/tools/derive-times.py --check # ดูว่าจะเปลี่ยนอะไร (ไม่เขียนไฟล์)
|
|
python resource/tools/derive-times.py # เขียนจริง
|
|
python resource/tools/derive-times.py --audit # ตรวจกฎ vs ไฟล์ (ไม่เขียน)
|
|
"""
|
|
import argparse
|
|
import csv
|
|
import json
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
ROOT = Path(__file__).resolve().parents[2]
|
|
DATA = ROOT / "resource" / "data"
|
|
SUB_DIR = DATA / "scheduleslist"
|
|
TABLE = DATA / "derive-times.csv"
|
|
|
|
|
|
def read_table(path=TABLE):
|
|
"""อ่านตารางกฎ → [{route_id, station_id, from_station_id, offset_min, note}]"""
|
|
if not path.exists():
|
|
return []
|
|
rows = []
|
|
with path.open(encoding="utf-8-sig") as f:
|
|
for line in f:
|
|
line = line.strip()
|
|
if not line or line.startswith("#"):
|
|
continue
|
|
parts = [x.strip() for x in line.split(",")]
|
|
if parts[0].lower() == "route_id": # หัวตาราง
|
|
continue
|
|
if len(parts) < 4:
|
|
continue
|
|
rows.append({
|
|
"route_id": str(parts[0]),
|
|
"station_id": int(parts[1]),
|
|
"from_station_id": int(parts[2]),
|
|
"offset_min": int(parts[3]),
|
|
"note": (parts[4] if len(parts) > 4 else "").strip() or None,
|
|
})
|
|
return rows
|
|
|
|
|
|
def sub_path(rid):
|
|
return SUB_DIR / f"schedules{rid}.json"
|
|
|
|
|
|
def load_sub(rid):
|
|
"""คืน (ข้อมูล, ชนิดขึ้นบรรทัด) — อ่านเป็น bytes เพราะ Path.read_text() แปลง CRLF→LF ให้อัตโนมัติ
|
|
(ถ้าอ่านด้วย read_text() จะตรวจชนิดขึ้นบรรทัดผิด แล้วเขียนไฟล์เป็น LF ทั้งที่เดิม CRLF)"""
|
|
f = sub_path(rid)
|
|
if not f.exists():
|
|
return None, None
|
|
raw = f.read_bytes()
|
|
eol = "\r\n" if b"\r\n" in raw else "\n"
|
|
return json.loads(raw.decode("utf-8")), eol
|
|
|
|
|
|
def all_times(sub, station_id):
|
|
"""เวลาจริงของจุดนั้น (จากทุกทิศ)"""
|
|
for d in sub.get("directions", []):
|
|
for s in d.get("stops", []):
|
|
if int(s["station_id"]) == int(station_id) and (s.get("times") or []):
|
|
return s["times"], d.get("dir_id")
|
|
return [], None
|
|
|
|
|
|
def find_stop(sub, station_id):
|
|
"""คืน (ทิศ, จุด) ของจุดเป้าหมาย"""
|
|
for d in sub.get("directions", []):
|
|
for s in d.get("stops", []):
|
|
if int(s["station_id"]) == int(station_id):
|
|
return d, s
|
|
return None, None
|
|
|
|
|
|
def main():
|
|
ap = argparse.ArgumentParser()
|
|
ap.add_argument("--check", action="store_true", help="dry run — ไม่เขียนไฟล์")
|
|
ap.add_argument("--audit", action="store_true", help="ตรวจกฎกับไฟล์ (ไม่เขียน)")
|
|
args = ap.parse_args()
|
|
|
|
rules = read_table()
|
|
if not rules:
|
|
print(f"ไม่พบกฎในตาราง {TABLE.relative_to(ROOT)} (หรือไฟล์ว่าง)")
|
|
return 0
|
|
|
|
print(f"กฎทั้งหมด: {len(rules)} ข้อ" + (" [--check = ไม่เขียนไฟล์]" if args.check else ""))
|
|
problems = 0
|
|
for r in rules:
|
|
rid = r["route_id"]
|
|
sub, eol = load_sub(rid)
|
|
if sub is None:
|
|
print(f" ✗ {rid}: ไม่พบไฟล์ {sub_path(rid).name}")
|
|
problems += 1
|
|
continue
|
|
src_times, src_dir = all_times(sub, r["from_station_id"])
|
|
d, stop = find_stop(sub, r["station_id"])
|
|
if stop is None:
|
|
print(f" ✗ {rid}: ไม่พบจุด {r['station_id']} ในไฟล์ (ต้องมีจุดก่อนจึงจะตั้งกฎได้)")
|
|
problems += 1
|
|
continue
|
|
if not src_times:
|
|
print(f" ✗ {rid}: จุดอ้างอิง {r['from_station_id']} ยังไม่มีเวลาจริง — คำนวณไม่ได้")
|
|
problems += 1
|
|
continue
|
|
|
|
had = len(stop.get("times") or [])
|
|
want = {"from_station_id": r["from_station_id"], "offset_min": r["offset_min"]}
|
|
if r["note"]:
|
|
want["note"] = r["note"]
|
|
already = stop.get("derive") == want and not had
|
|
if args.audit:
|
|
ok = already
|
|
print(f" {'✓' if ok else '✗'} {rid} จุด {r['station_id']} ← จุด {r['from_station_id']} "
|
|
f"{r['offset_min']:+d} นาที (ต้นทางมี {len(src_times)} เที่ยว"
|
|
+ (f" · ทิศ {src_dir}" if src_dir else "") + ")"
|
|
+ ("" if ok else f" — ไฟล์ยังไม่ตรง (มี times {had} · derive={stop.get('derive')})"))
|
|
if not ok:
|
|
problems += 1
|
|
continue
|
|
|
|
if already:
|
|
print(f" = {rid} จุด {r['station_id']}: ตั้งกฎไว้แล้ว ({r['offset_min']:+d} นาที) — ข้าม")
|
|
continue
|
|
|
|
print(f" → {rid} จุด {r['station_id']} ← จุด {r['from_station_id']} {r['offset_min']:+d} นาที"
|
|
+ (f" · ลบเวลาที่คัดลอกมา {had} เที่ยว" if had else ""))
|
|
if not args.check:
|
|
stop["derive"] = want
|
|
if had:
|
|
stop.pop("times", None) # ไม่เก็บเวลาซ้ำ — ให้เว็บคำนวณเสมอ
|
|
stop["times"] = []
|
|
txt = json.dumps(sub, ensure_ascii=False, indent=2)
|
|
if eol == "\r\n":
|
|
txt = txt.replace("\n", "\r\n")
|
|
sub_path(rid).write_text(txt + eol, encoding="utf-8", newline="")
|
|
|
|
if args.audit:
|
|
print(f"\nผลตรวจ: {len(rules) - problems}/{len(rules)} ตรงกัน" + (f" · ไม่ตรง {problems}" if problems else " ✓"))
|
|
print("\n— จุดที่ 'เวลาซ้ำกับอีกจุดในสายเดียวกัน' แต่ยังไม่มีกฎ (น่าจะต้องตั้ง derive) —")
|
|
cands = 0
|
|
for f in sorted(SUB_DIR.glob("schedules*.json")):
|
|
sub2, _ = load_sub(f.stem.replace("schedules", ""))
|
|
if not sub2:
|
|
continue
|
|
sig = {}
|
|
for d in sub2.get("directions", []):
|
|
for st in d.get("stops", []):
|
|
t = tuple(st.get("times") or [])
|
|
if t:
|
|
sig.setdefault(t, []).append((d.get("dir_id"), st["station_id"], st.get("derive")))
|
|
for times, who in sig.items():
|
|
if len(who) < 2:
|
|
continue
|
|
if any(w[2] for w in who): # มีกฎแล้วบางจุด → ถือว่าจัดการแล้ว
|
|
continue
|
|
cands += 1
|
|
print(f" · {sub2.get('route_id')}: " + " = ".join(f"จุด {w[1]} (ทิศ {w[0]})" for w in who)
|
|
+ f" · {len(times)} เที่ยวเท่ากันเป๊ะ → เลือกจุดอ้างอิง + ใส่ offset")
|
|
print(" (ไม่มี)" if not cands else f" รวม {cands} กลุ่ม")
|
|
else:
|
|
print(f"\n{'ตรวจแล้ว (ไม่เขียนไฟล์)' if args.check else 'เขียนไฟล์แล้ว'} · ปัญหา {problems} รายการ")
|
|
return 1 if problems else 0
|
|
|
|
|
|
if __name__ == "__main__":
|
|
sys.exit(main())
|