ตารางเวลาคำนวณได้ + หน้ารายละเอียดป้ายขับเคลื่อนด้วยข้อมูล + แผนที่ Leaflet
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 · นามสกุลรูป · ชิปสาย · หน้ารายละเอียดป้ายมาจากไหน
This commit is contained in:
1 parent
88311326e6
commit
1cfaeb1526
91 files changed
+7560
-844
No files matched your search
@@ -20,6 +20,11 @@ window.ATMap = (function () {
|
||||
'use strict';
|
||||
|
||||
var DEF_BASE = '../resource/data/';
|
||||
/* station_id 8 = "จุดขึ้นรถประจำทางอื่นๆ" (ใช้ร่วมหลายสาย) → ต้องจับคู่ route_id
|
||||
ในไฟล์ '8 other_stop.json' เพื่อเอาชื่อ/พิกัดจริงของสายนั้น (สายเดียวมีได้หลายจุด) */
|
||||
var OTHER_STOP_ID = 8;
|
||||
var OTHER_STOP_FILE = '8%20other_stop.json';
|
||||
var OFF_ROUTE_M = 2000; /* ห่างจากเส้นเกินนี้ → แจ้ง "นอกเส้นทาง" */
|
||||
var TILES = 'https://tile.openstreetmap.org/{z}/{x}/{y}.png';
|
||||
var ATTRIB = '© OpenStreetMap';
|
||||
var NEAR_M = 40; /* ป้ายอยู่ "บนเส้นทาง" ถ้าใกล้ ≤ 40 ม. */
|
||||
@@ -118,16 +123,97 @@ window.ATMap = (function () {
|
||||
}
|
||||
function stops(o) { return loadJson('busstop_list.json', 'stops', o).then(function (d) { return d || []; }); }
|
||||
function stationList(o) { return loadJson('stations.json', 'stations', o).then(function (d) { return (d || {}).stations || []; }); }
|
||||
function otherStopList(o) { return loadJson(OTHER_STOP_FILE, 'otherStops', o).then(function (d) { return (d || {}).other_stop || []; }); }
|
||||
function routeList(o) { return loadJson('routes.json', 'routes', o).then(function (d) { return (d || {}).routes || []; }); }
|
||||
|
||||
/* จุดจอดตามเส้นทาง = routes.json → station_ids (ตามลำดับของสาย)
|
||||
- station_id 8 → จับคู่ route_id ใน '8 other_stop.json' (ถ้าไม่มีคู่ จับคู่ไม่ได้ → ใช้สถานี 8 ตามเดิม)
|
||||
- กม. จากต้นทาง = วัดตามเส้น geometry โดยเลือกปลายที่ใกล้จุดจอดแรกที่สุดเป็น "ต้นทาง"
|
||||
- พิกัดที่ห่างจากเส้นเกิน OFF_ROUTE_M → ขึ้น "นอกเส้นทาง" แทนตัวเลข กม. (กันเลขหลอก) */
|
||||
function routeStopRows(route, stations, others, main, validIds) {
|
||||
var byId = {};
|
||||
stations.forEach(function (st) { byId[st.station_id] = st; });
|
||||
|
||||
var stops = [];
|
||||
(route.station_ids || []).forEach(function (sid) {
|
||||
if (sid === OTHER_STOP_ID) {
|
||||
/* จับคู่ route → ชื่อสถานที่จริง (รองรับรหัสเพี้ยน เช่น 1101 = สาย 1011) ผ่าน OtherStop */
|
||||
var hit = (window.OtherStop && OtherStop.forRouteAll)
|
||||
? OtherStop.forRouteAll(others, route.route_id, validIds)
|
||||
: (others || []).filter(function (o) { return String(o.route_id) === String(route.route_id); });
|
||||
if (hit.length) {
|
||||
hit.forEach(function (o) {
|
||||
/* แตกเป็น "จุดจริง" ทีละจุด — ชื่อ ↔ พิกัด จับคู่ตามลำดับ (จุดที่ไม่มีพิกัด = ไม่ปักหมุด) */
|
||||
var subs = (window.OtherStop && OtherStop.stopsOf) ? OtherStop.stopsOf(o) : [];
|
||||
if (!subs.length) subs = [{ name: o.station_name_th, hasCoord: false, lat: null, lng: null }];
|
||||
subs.forEach(function (sub) {
|
||||
stops.push({
|
||||
name: sub.name,
|
||||
pt: sub.hasCoord ? [sub.lng, sub.lat] : null,
|
||||
code: null,
|
||||
note: o.note || null
|
||||
});
|
||||
});
|
||||
});
|
||||
return; /* จับคู่ได้ → ไม่ใช้ชื่อสถานีรวม */
|
||||
}
|
||||
}
|
||||
var st = byId[sid];
|
||||
if (st) stops.push({ name: st.station_name_th, pt: pointOf(st), code: st.station_code });
|
||||
});
|
||||
|
||||
var total = lineLengthKm([main]);
|
||||
var first = stops.filter(function (s) { return s.pt; })[0];
|
||||
var fromEnd = first ? kmFromStart(main, first.pt).km > total / 2 : false;
|
||||
|
||||
stops.forEach(function (s) {
|
||||
if (!s.pt) { s.km = null; s.off = false; return; }
|
||||
var k = kmFromStart(main, s.pt);
|
||||
var km = fromEnd ? total - k.km : k.km;
|
||||
s.off = k.d > OFF_ROUTE_M;
|
||||
s.km = Math.max(0, km).toFixed(1);
|
||||
});
|
||||
return stops;
|
||||
}
|
||||
|
||||
/* ---------- องค์ประกอบแผนที่ ---------- */
|
||||
/* แผนที่ทั้งหมดที่สร้างในหน้านี้ (ไว้จัดการ resize + ตรวจสอบ) */
|
||||
var MAPS = [];
|
||||
|
||||
function mapOptions() {
|
||||
return {
|
||||
zoomControl: true,
|
||||
attributionControl: true,
|
||||
scrollWheelZoom: true, /* ล้อเมาส์: หมุนเข้า/ออก = ซูม */
|
||||
touchZoom: true, /* มือถือ: ใช้นิ้ว 2 นิ้วจีบ/กาง = ซูม */
|
||||
doubleClickZoom: true,
|
||||
boxZoom: true,
|
||||
keyboard: true,
|
||||
wheelDebounceTime: 40 /* หน่วงนิดเดียว ไม่ให้ซูมกระโดดตอนหมุนเร็ว */
|
||||
};
|
||||
}
|
||||
|
||||
function makeMap(el) {
|
||||
var offline = navigator.onLine === false;
|
||||
if (offline) el.classList.add('is-offline');
|
||||
var map = L.map(el, { zoomControl: true, attributionControl: true, scrollWheelZoom: false });
|
||||
var map = L.map(el, mapOptions());
|
||||
map.setView([14.588, 100.455], 11);
|
||||
if (!offline) L.tileLayer(TILES, { maxZoom: 18, minZoom: 8, attribution: ATTRIB }).addTo(map);
|
||||
MAPS.push({ map: map, el: el, bounds: null });
|
||||
return { map: map, offline: offline };
|
||||
}
|
||||
|
||||
/* ถ้าหน้าต่างเปลี่ยนขนาด (หรือหมุนมือถือ) → อัปเดตขนาด + ซูมให้พอดีเส้นทางอีกครั้ง */
|
||||
var resizeTimer = null;
|
||||
window.addEventListener('resize', function () {
|
||||
clearTimeout(resizeTimer);
|
||||
resizeTimer = setTimeout(function () {
|
||||
MAPS.forEach(function (rec) {
|
||||
rec.map.invalidateSize();
|
||||
if (rec.bounds) rec.map.fitBounds(rec.bounds, { padding: [24, 24], maxZoom: 15 });
|
||||
});
|
||||
}, 200);
|
||||
});
|
||||
function pinIcon(kind) {
|
||||
return L.divIcon({ className: 'at-pin-wrap', html: '<span class="at-pin ' + kind + '"></span>', iconSize: [16, 16], iconAnchor: [8, 8] });
|
||||
}
|
||||
@@ -152,13 +238,40 @@ window.ATMap = (function () {
|
||||
var all = [];
|
||||
latlngs.forEach(function (l) { all = all.concat(l); });
|
||||
if (!all.length) return;
|
||||
if (all.length === 1) { map.setView(all[0], 14); return; }
|
||||
map.fitBounds(L.latLngBounds(all), { padding: [24, 24] });
|
||||
var rec = mapsFor(map);
|
||||
if (all.length === 1) {
|
||||
if (rec) rec.bounds = L.latLngBounds(all);
|
||||
map.setView(all[0], 14);
|
||||
return;
|
||||
}
|
||||
var bounds = L.latLngBounds(all);
|
||||
if (rec) rec.bounds = bounds;
|
||||
map.invalidateSize(); /* ให้แน่ใจว่าขนาดถูกก่อนคำนวณซูม */
|
||||
map.fitBounds(bounds, { padding: [24, 24], maxZoom: 15 });
|
||||
}
|
||||
|
||||
function mapsFor(map) {
|
||||
for (var i = 0; i < MAPS.length; i++) if (MAPS[i].map === map) return MAPS[i];
|
||||
return null;
|
||||
}
|
||||
function mapsLink(lat, lng) {
|
||||
return 'https://www.google.com/maps/search/?api=1&query=' + lat + ',' + lng;
|
||||
}
|
||||
|
||||
/* วาดเส้นทาง: ขอบสีครีม (เห็นชัดบนพื้นแผนที่) แล้วทับด้วยเส้นสีจริง 5px ตาม DESIGN.md §5 */
|
||||
function drawLines(group, specs) {
|
||||
specs.forEach(function (sp) {
|
||||
L.polyline(sp.latlngs, {
|
||||
color: '#fffdf6', weight: LINE_W + 4, opacity: 0.95, lineJoin: 'round', lineCap: 'round', smoothFactor: 0.6
|
||||
}).addTo(group);
|
||||
});
|
||||
specs.forEach(function (sp) {
|
||||
L.polyline(sp.latlngs, {
|
||||
color: sp.color, weight: LINE_W, opacity: 1, lineJoin: 'round', lineCap: 'round', smoothFactor: 0.6
|
||||
}).addTo(group);
|
||||
});
|
||||
}
|
||||
|
||||
/* ป้ายหยุดรถ: hover = ข้อมูลย่อ · คลิก = popup (ประเภท/กม./ด้านทาง/สายที่ผ่าน + Google Maps) */
|
||||
function drawShelters(map, hits, group) {
|
||||
hits.forEach(function (h) {
|
||||
@@ -251,13 +364,16 @@ window.ATMap = (function () {
|
||||
if (!list.length) return;
|
||||
list.forEach(function (x) {
|
||||
var li = document.createElement('li');
|
||||
var a = document.createElement('a');
|
||||
a.href = x.href;
|
||||
a.className = 'at-km-row';
|
||||
a.innerHTML = '<span class="at-km-dot" aria-hidden="true"></span>' +
|
||||
var row = document.createElement(x.href ? 'a' : 'div');
|
||||
if (x.href) row.href = x.href;
|
||||
row.className = 'at-km-row' + (x.href ? '' : ' is-plain');
|
||||
if (x.note) row.title = x.note;
|
||||
var val = x.off ? '<span class="is-off">' + esc(t('map.offRoute')) + '</span>'
|
||||
: (x.km === null || x.km === '' ? '–' : x.km + ' ' + esc(t('map.km')));
|
||||
row.innerHTML = '<span class="at-km-dot" aria-hidden="true"></span>' +
|
||||
'<span class="at-km-name">' + esc(x.name) + '</span>' +
|
||||
'<span class="at-km-val">' + x.km + ' ' + esc(t('map.km')) + '</span>';
|
||||
li.appendChild(a);
|
||||
'<span class="at-km-val">' + val + '</span>';
|
||||
li.appendChild(row);
|
||||
host.appendChild(li);
|
||||
});
|
||||
var wrap = host.closest('.at-km-wrap');
|
||||
@@ -282,9 +398,7 @@ window.ATMap = (function () {
|
||||
var m = makeMap(el), map = m.map, group = L.layerGroup().addTo(map);
|
||||
var latlngs = toLatLngs(lines);
|
||||
|
||||
latlngs.forEach(function (l) {
|
||||
L.polyline(l, { color: LINE_MAIN, weight: LINE_W, opacity: 1, lineJoin: 'round', lineCap: 'round' }).addTo(group);
|
||||
});
|
||||
drawLines(group, latlngs.map(function (l) { return { latlngs: l, color: LINE_MAIN }; }));
|
||||
/* ต้นทาง/ปลายทาง */
|
||||
[[latlngs[0][0], t('map.origin')], [latlngs[latlngs.length - 1][latlngs[latlngs.length - 1].length - 1], t('map.destination')]]
|
||||
.forEach(function (e) {
|
||||
@@ -292,23 +406,36 @@ window.ATMap = (function () {
|
||||
.bindTooltip(esc(e[1]), { direction: 'top', offset: [0, -8] }).addTo(group);
|
||||
});
|
||||
|
||||
Promise.all([stopsNear(lines, opts), stationsNear(lines, STATION_NEAR_M, opts)]).then(function (res) {
|
||||
var shelters = res[0], sts = res[1];
|
||||
Promise.all([stopsNear(lines, opts), stationsNear(lines, STATION_NEAR_M, opts),
|
||||
stationList(opts), routeList(opts), otherStopList(opts)]).then(function (res) {
|
||||
var shelters = res[0], sts = res[1], stations = res[2], routesDoc = res[3], others = res[4];
|
||||
drawShelters(map, shelters, group);
|
||||
drawStations(map, sts, opts, group);
|
||||
fitAll(map, latlngs);
|
||||
|
||||
/* รายการสถานี + กม. จากต้นทาง (เรียงตามเส้นทาง) */
|
||||
/* จุดจอดตามเส้นทาง + กม. จากต้นทาง — ใช้ routes.json → station_ids เป็นหลัก
|
||||
(station 8 → จับคู่ route_id ใน 8 other_stop.json) ถ้าหา route ไม่ได้จึงใช้ระยะใกล้เส้นแบบเดิม */
|
||||
var main = longest(lines);
|
||||
var rows = sts.map(function (h) {
|
||||
var pt = pointOf(h.station);
|
||||
var k = kmFromStart(main, pt);
|
||||
return { name: h.station.station_name_th, km: k.km.toFixed(1), href: href(opts, h.station.station_code), _sort: k.km };
|
||||
}).sort(function (a, b) { return a._sort - b._sort; });
|
||||
var route = (routesDoc || []).filter(function (r) { return String(r.route_id) === String(routeId); })[0];
|
||||
var rows;
|
||||
if (route && (route.station_ids || []).length) {
|
||||
rows = routeStopRows(route, stations, others, main, (routesDoc || []).map(function (r) { return String(r.route_id); })).map(function (x) {
|
||||
return { name: x.name, km: x.km, off: x.off, note: x.note, href: x.code ? href(opts, x.code) : null };
|
||||
}).sort(function (a, b) { /* เรียงตาม กม. ตามเส้นทาง (อ่านเป็นเส้นทางเดินรถ) */
|
||||
if (a.km === null || a.km === '') return 1;
|
||||
if (b.km === null || b.km === '') return -1;
|
||||
return parseFloat(a.km) - parseFloat(b.km);
|
||||
});
|
||||
} else {
|
||||
rows = sts.map(function (h) {
|
||||
var k = kmFromStart(main, pointOf(h.station));
|
||||
return { name: h.station.station_name_th, km: k.km.toFixed(1), off: false, href: href(opts, h.station.station_code) };
|
||||
}).sort(function (a, b) { return parseFloat(a.km) - parseFloat(b.km); });
|
||||
}
|
||||
renderKmList('route-map-list', rows);
|
||||
|
||||
var parts = ['<b>' + esc(routeId) + '</b> · ' + esc(t('map.captionRoute', { km: lineLengthKm(lines).toFixed(1), n: shelters.length }))];
|
||||
if (sts.length) parts.push(esc(t('map.stations')) + ' <b>' + sts.length + '</b>');
|
||||
if (rows.length) parts.push(esc(t('map.stations')) + ' <b>' + rows.length + '</b>');
|
||||
if (m.offline) parts.push('<span class="at-offline">' + esc(t('map.offline')) + '</span>');
|
||||
caption(el, parts.join(' · '));
|
||||
});
|
||||
@@ -332,6 +459,9 @@ window.ATMap = (function () {
|
||||
var list = res[0], routes = (res[1] || {}).routes || [];
|
||||
var st = list.filter(function (s) { return s.station_code === code; })[0];
|
||||
var serving = st ? routes.filter(function (r) { return (r.station_ids || []).indexOf(st.station_id) !== -1; }) : [];
|
||||
if (opts.onlyRoutes && opts.onlyRoutes.length) {
|
||||
serving = serving.filter(function (r) { return opts.onlyRoutes.indexOf(String(r.route_id)) !== -1; });
|
||||
}
|
||||
var ids = serving.map(function (r) { return r.route_id; });
|
||||
|
||||
if (!st || !ids.length) {
|
||||
@@ -342,7 +472,7 @@ window.ATMap = (function () {
|
||||
|
||||
Promise.all(ids.map(function (id) { return loadGeo(id, opts); })).then(function (geos) {
|
||||
var m = makeMap(el), map = m.map, group = L.layerGroup().addTo(map);
|
||||
var all = [], allLines = [], legendRows = [], drawn = 0;
|
||||
var all = [], allLines = [], legendRows = [], drawn = 0, specs = [];
|
||||
|
||||
geos.forEach(function (g, idx) {
|
||||
var lines = flatten(g);
|
||||
@@ -351,7 +481,7 @@ window.ATMap = (function () {
|
||||
drawn++;
|
||||
allLines = allLines.concat(lines);
|
||||
toLatLngs(lines).forEach(function (l) {
|
||||
L.polyline(l, { color: color, weight: LINE_W, opacity: 1, lineJoin: 'round', lineCap: 'round' }).addTo(group);
|
||||
specs.push({ latlngs: l, color: color });
|
||||
all.push(l);
|
||||
});
|
||||
legendRows.push({ color: color, label: t('map.route') + ' ' + ids[idx] });
|
||||
@@ -362,13 +492,17 @@ window.ATMap = (function () {
|
||||
caption(el, '<span class="at-offline">' + esc(t('map.empty')) + '</span>');
|
||||
return;
|
||||
}
|
||||
drawLines(group, specs);
|
||||
|
||||
stopsNear(allLines, opts).then(function (shelters) {
|
||||
drawShelters(map, shelters, group);
|
||||
var stPt = pointOf(st);
|
||||
/* opts.focus = จุดจริงของสายที่เลือก (เช่น จุดขึ้นรถประจำทางอื่นๆ → วัดสีบัวทอง) */
|
||||
var stPt = opts.focus ? pointOf(opts.focus) : pointOf(st);
|
||||
var focusFirst = (opts.focus && window.OtherStop && OtherStop.firstName) ? OtherStop.firstName(opts.focus) : null;
|
||||
var pinName = focusFirst || (opts.focus && opts.focus.station_name_th) || st.station_name_th;
|
||||
if (stPt) {
|
||||
L.marker([stPt[1], stPt[0]], { icon: pinIcon('here'), zIndexOffset: 1000 })
|
||||
.bindTooltip('<b>' + esc(st.station_name_th) + '</b><br>' + esc(t('map.thisStop')), { direction: 'top', offset: [0, -11] })
|
||||
.bindTooltip('<b>' + esc(pinName) + '</b><br>' + esc(t('map.thisStop')), { direction: 'top', offset: [0, -11] })
|
||||
.addTo(group);
|
||||
all.push([[stPt[1], stPt[0]]]);
|
||||
}
|
||||
@@ -473,7 +607,7 @@ window.ATMap = (function () {
|
||||
|
||||
function drawNearMap(box, near, me) {
|
||||
var offline = navigator.onLine === false;
|
||||
var map = L.map(box, { zoomControl: true, attributionControl: true, scrollWheelZoom: false });
|
||||
var map = L.map(box, mapOptions());
|
||||
map.setView([me[1], me[0]], 13);
|
||||
if (!offline) L.tileLayer(TILES, { maxZoom: 18, minZoom: 8, attribution: ATTRIB }).addTo(map);
|
||||
else box.classList.add('is-offline');
|
||||
@@ -494,6 +628,7 @@ window.ATMap = (function () {
|
||||
|
||||
return {
|
||||
route: route, forStop: forStop, nearMe: nearMe,
|
||||
NEAR_M: NEAR_M, LINE_MAIN: LINE_MAIN
|
||||
NEAR_M: NEAR_M, LINE_MAIN: LINE_MAIN,
|
||||
maps: MAPS /* ไว้ตรวจสอบ/ทดสอบจากคอนโซล */
|
||||
};
|
||||
})();
|
||||
Reference in new issue
Block a user