Upload app.py
Browse files
app.py
CHANGED
|
@@ -461,45 +461,38 @@ PAGE = r'''<!DOCTYPE html>
|
|
| 461 |
<script src="https://unpkg.com/leaflet@1.9.4/dist/leaflet.js"></script>
|
| 462 |
<script src="https://unpkg.com/leaflet-draw@1.0.4/dist/leaflet.draw.js"></script>
|
| 463 |
<script>
|
| 464 |
-
// =====
|
| 465 |
const drop = document.getElementById('drop'), file = document.getElementById('file');
|
| 466 |
drop.onclick = () => file.click();
|
| 467 |
file.onchange = e => e.target.files[0] && showShot(e.target.files[0]);
|
| 468 |
-
['dragenter','dragover'].forEach(t => drop.addEventListener(t, e => {
|
| 469 |
-
|
| 470 |
-
['dragleave','drop'].forEach(t => drop.addEventListener(t, e => {
|
| 471 |
-
e.preventDefault(); drop.classList.remove('hot'); }));
|
| 472 |
drop.addEventListener('drop', e => {
|
| 473 |
const f = e.dataTransfer.files[0];
|
| 474 |
-
if (f && f.type.startsWith('image/')) showShot(f);
|
| 475 |
-
else msg('That file is not an image.', 'err');
|
| 476 |
});
|
| 477 |
-
function showShot(f){
|
| 478 |
-
const img = document.createElement('img');
|
| 479 |
-
img.src = URL.createObjectURL(f);
|
| 480 |
-
drop.innerHTML = ''; drop.appendChild(img);
|
| 481 |
-
}
|
| 482 |
|
| 483 |
-
// =====
|
| 484 |
const map = L.map('map').setView([39.83, -98.58], 4);
|
| 485 |
-
const streets = L.tileLayer(
|
| 486 |
-
'https://{s}.basemaps.cartocdn.com/rastertiles/voyager/{z}/{x}/{y}{r}.png',
|
| 487 |
{maxZoom:19, attribution:'© OpenStreetMap, © CARTO'});
|
| 488 |
-
const satellite = L.tileLayer(
|
| 489 |
-
'https://server.arcgisonline.com/ArcGIS/rest/services/World_Imagery/MapServer/tile/{z}/{y}/{x}',
|
| 490 |
{maxZoom:19, maxNativeZoom:19, attribution:'Imagery © Esri'});
|
| 491 |
-
const ESRI
|
| 492 |
-
const roadLabels
|
| 493 |
-
const placeLabels
|
| 494 |
-
const satHybrid
|
| 495 |
streets.addTo(map);
|
| 496 |
-
L.control.layers({'Street':streets,
|
| 497 |
map.createPane('routePane'); map.getPane('routePane').style.zIndex = 450;
|
| 498 |
|
| 499 |
const drawn = new L.FeatureGroup().addTo(map);
|
| 500 |
-
|
|
|
|
|
|
|
| 501 |
|
| 502 |
-
const SHAPE_STYLE
|
| 503 |
const boxTool = new L.Draw.Rectangle(map, {shapeOptions:SHAPE_STYLE});
|
| 504 |
const polyTool = new L.Draw.Polygon(map, {shapeOptions:SHAPE_STYLE, allowIntersection:false,
|
| 505 |
showArea:false, drawError:{color:'#d7263d', message:'Edges cannot cross'}});
|
|
@@ -530,8 +523,8 @@ map.on('click', e => {
|
|
| 530 |
|
| 531 |
document.getElementById('clear').onclick = () => {
|
| 532 |
stop(); drawn.clearLayers();
|
| 533 |
-
[
|
| 534 |
-
|
| 535 |
document.getElementById('solve').disabled = true;
|
| 536 |
document.getElementById('map').classList.remove('routed');
|
| 537 |
document.getElementById('play').classList.remove('on');
|
|
@@ -541,7 +534,7 @@ document.getElementById('clear').onclick = () => {
|
|
| 541 |
msg('');
|
| 542 |
};
|
| 543 |
|
| 544 |
-
// =====
|
| 545 |
const q = document.getElementById('q');
|
| 546 |
document.getElementById('find').onclick = find;
|
| 547 |
q.onkeydown = e => { if (e.key === 'Enter') find(); };
|
|
@@ -558,7 +551,7 @@ async function find(){
|
|
| 558 |
} catch (err) { msg('Lookup failed: ' + err, 'err'); }
|
| 559 |
}
|
| 560 |
|
| 561 |
-
// =====
|
| 562 |
const solveBtn = document.getElementById('solve');
|
| 563 |
solveBtn.onclick = async () => {
|
| 564 |
if (!shape) return;
|
|
@@ -577,77 +570,68 @@ solveBtn.onclick = async () => {
|
|
| 577 |
} catch (err) { msg('Solve failed: ' + err, 'err'); }
|
| 578 |
finally { solveBtn.disabled = false; solveBtn.innerHTML = orig; }
|
| 579 |
};
|
| 580 |
-
// =====
|
| 581 |
let PATH=[], TOTAL_M=0, DRIVE_MIN=0, STEPS=[];
|
| 582 |
-
|
| 583 |
-
|
| 584 |
-
const
|
| 585 |
-
const C_DONE = '#22c55e'; // traveled = green
|
| 586 |
-
const C_DEAD = '#ef4444'; // traveled deadhead (repeat) = red
|
| 587 |
|
| 588 |
function bearing(a, b){
|
| 589 |
-
const r
|
| 590 |
-
const y
|
| 591 |
-
const x
|
| 592 |
-
return (Math.atan2(y,
|
| 593 |
}
|
| 594 |
|
| 595 |
function render(d){
|
| 596 |
stop();
|
| 597 |
[baseLayer, doneLayer, partialLine, arrowLayer, truck, startPin].forEach(l => l && map.removeLayer(l));
|
| 598 |
-
|
| 599 |
const feats = d.geojson.features;
|
| 600 |
-
PATH
|
| 601 |
feats.forEach(f => {
|
| 602 |
-
const c
|
| 603 |
-
const a
|
| 604 |
-
const len
|
| 605 |
-
PATH.push({a,
|
| 606 |
-
|
| 607 |
-
TOTAL_M += len;
|
| 608 |
});
|
| 609 |
-
DRIVE_MIN
|
| 610 |
|
| 611 |
-
|
| 612 |
-
const line = [PATH[0].a]; PATH.forEach(s => line.push(s.b));
|
| 613 |
baseLayer = L.layerGroup([
|
| 614 |
-
L.polyline(line,
|
| 615 |
-
L.polyline(line,
|
| 616 |
]).addTo(map);
|
| 617 |
|
| 618 |
-
// layer that fills in GREEN/RED behind the truck as it drives
|
| 619 |
doneLayer = L.layerGroup([], {pane:'routePane'}).addTo(map);
|
| 620 |
-
partialLine = L.polyline([], {pane:'routePane',
|
| 621 |
|
| 622 |
buildArrows();
|
| 623 |
-
|
| 624 |
-
startPin = L.marker(PATH[0].a, {zIndexOffset:1100, icon: pinIcon('START / END', '#3b82f6')}).addTo(map);
|
| 625 |
truck = L.marker(PATH[0].a, {icon: truckIcon(0), zIndexOffset:1200, interactive:false}).addTo(map);
|
| 626 |
|
| 627 |
-
const bb
|
| 628 |
map.fitBounds(bb, {padding:[30,30]});
|
| 629 |
buildLegend();
|
| 630 |
document.getElementById('map').classList.add('routed');
|
| 631 |
document.getElementById('play').classList.add('on');
|
| 632 |
document.getElementById('hud').classList.add('on');
|
| 633 |
renderStats(d.stats);
|
| 634 |
-
resetTravel();
|
| 635 |
-
seek(0);
|
| 636 |
}
|
| 637 |
|
| 638 |
-
// small dark arrowheads so you can read direction on the yellow line
|
| 639 |
function buildArrows(){
|
| 640 |
if (arrowLayer) map.removeLayer(arrowLayer);
|
| 641 |
arrowLayer = L.layerGroup();
|
| 642 |
-
const stride = Math.max(1, Math.ceil(PATH.length
|
| 643 |
-
PATH.forEach((s,
|
| 644 |
-
if (i
|
| 645 |
-
if (s.len
|
| 646 |
-
const mid
|
| 647 |
-
L.marker(mid,
|
| 648 |
-
html:'<div class="arrow" style="transform:rotate('
|
| 649 |
-
'<svg width="16" height="16" viewBox="0 0 16 16">'
|
| 650 |
-
'<path d="M8 2 L12 12 L8 9.6 L4 12 Z" fill="#111" stroke="#fff" stroke-width="1"/>'
|
| 651 |
'</svg></div>'})}).addTo(arrowLayer);
|
| 652 |
});
|
| 653 |
if (document.getElementById('arrowsOn').checked) arrowLayer.addTo(map);
|
|
@@ -657,155 +641,129 @@ document.getElementById('arrowsOn').onchange = e => {
|
|
| 657 |
e.target.checked ? arrowLayer.addTo(map) : map.removeLayer(arrowLayer);
|
| 658 |
};
|
| 659 |
|
| 660 |
-
function pinIcon(label,
|
| 661 |
-
return L.divIcon({className:'',
|
| 662 |
-
'<div class="pin"><svg width="26" height="30" viewBox="0 0 26 30">'
|
| 663 |
-
'<path d="M13 29 C13 29 24 17 24 11 A11 11 0 1 0 2 11 C2 17 13 29 13 29 Z" ' +
|
| 664 |
-
'
|
| 665 |
-
'<
|
| 666 |
-
'<div style="margin-top:-2px">' + label + '</div></div>'});
|
| 667 |
}
|
| 668 |
|
| 669 |
function truckIcon(deg){
|
| 670 |
-
return L.divIcon({className:'',
|
| 671 |
-
html:'<div class="truck" style="transform:rotate('
|
| 672 |
-
'<svg width="34" height="34" viewBox="0 0 34 34">'
|
| 673 |
-
'<circle cx="17" cy="17" r="15" fill="#05070c" opacity=".6"/>'
|
| 674 |
-
'<g transform="translate(17,17)">'
|
| 675 |
-
'<rect x="-7" y="-9" width="14" height="18" rx="2.5" fill="#ffffff"/>'
|
| 676 |
-
'<rect x="-7" y="-9" width="14" height="6.5" rx="2" fill="#16a34a"/>'
|
| 677 |
-
'<rect x="-8.5" y="-2" width="3" height="6.5" rx="1" fill="#1b2130"/>'
|
| 678 |
-
'<rect x="5.5" y="-2" width="3" height="6.5" rx="1" fill="#1b2130"/>'
|
| 679 |
-
'<path d="M0 -14 L5 -9 L-5 -9 Z" fill="#ffd21e"/>'
|
| 680 |
'</g></svg></div>'});
|
| 681 |
}
|
| 682 |
|
| 683 |
function buildLegend(){
|
| 684 |
-
|
| 685 |
-
|
| 686 |
-
'<div class="
|
| 687 |
-
'<div class="lg"><span class="sw" style="background:'
|
| 688 |
-
'<div class="lg"><span class="sw" style="background:'
|
| 689 |
-
'<div class="lg"><span class="sw" style="background:' + C_DEAD + '"></span>driven deadhead (repeat)</div>' +
|
| 690 |
'<div class="lg" style="margin-top:5px"><span class="sw" style="background:#3b82f6"></span>start / end (loop)</div>';
|
| 691 |
-
|
| 692 |
}
|
| 693 |
|
| 694 |
-
// =====
|
| 695 |
let pos=0, playing=false, raf=null, last=0, drawnUpto=0;
|
| 696 |
const pp=document.getElementById('pp'), scrub=document.getElementById('scrub'),
|
| 697 |
speedSel=document.getElementById('speed'), followCk=document.getElementById('follow');
|
| 698 |
pp.onclick = () => playing ? stop() : play();
|
| 699 |
document.getElementById('restart').onclick = () => { seek(0); play(); };
|
| 700 |
-
scrub.oninput = () => { stop(); seek(TOTAL_M
|
| 701 |
|
| 702 |
-
|
| 703 |
-
function resetTravel(){
|
| 704 |
-
if (doneLayer) doneLayer.clearLayers();
|
| 705 |
-
drawnUpto = 0;
|
| 706 |
-
}
|
| 707 |
|
| 708 |
function play(){
|
| 709 |
if (!PATH.length) return;
|
| 710 |
-
if (pos >= TOTAL_M
|
| 711 |
-
playing
|
| 712 |
-
last
|
| 713 |
}
|
| 714 |
-
function stop(){ playing
|
| 715 |
-
|
| 716 |
function tick(now){
|
| 717 |
if (!playing) return;
|
| 718 |
-
const dt
|
| 719 |
-
pos += (TOTAL_M
|
| 720 |
-
if (pos
|
| 721 |
-
seek(pos); raf
|
| 722 |
}
|
| 723 |
|
| 724 |
function seek(m){
|
| 725 |
if (!PATH.length) return;
|
| 726 |
-
pos
|
| 727 |
-
|
| 728 |
-
|
| 729 |
-
|
| 730 |
-
|
| 731 |
-
|
| 732 |
-
|
| 733 |
-
const s
|
| 734 |
-
|
| 735 |
-
|
| 736 |
-
|
| 737 |
-
|
| 738 |
-
if (i < drawnUpto) resetTravel();
|
| 739 |
-
// paint every segment the truck has fully passed (green, or red if it's a deadhead)
|
| 740 |
-
while (drawnUpto < i){
|
| 741 |
-
const p = PATH[drawnUpto];
|
| 742 |
-
L.polyline([p.a, p.b], {pane:'routePane', color: p.deadhead ? C_DEAD : C_DONE,
|
| 743 |
-
weight:6, opacity:1, lineCap:'round'}).addTo(doneLayer);
|
| 744 |
drawnUpto++;
|
| 745 |
}
|
| 746 |
-
// the segment the truck is on: paint the part already covered
|
| 747 |
partialLine.setLatLngs([s.a, here]);
|
| 748 |
-
partialLine.setStyle({color: s.deadhead
|
| 749 |
-
|
| 750 |
-
truck.setLatLng(here); truck.setIcon(truckIcon(bearing(s.a, s.b)));
|
| 751 |
-
|
| 752 |
-
// camera follows the truck like a chase cam
|
| 753 |
if (followCk.checked && playing && !map.getBounds().pad(-0.28).contains(here))
|
| 754 |
map.panTo(here, {animate:true, duration:.5});
|
| 755 |
-
|
| 756 |
-
scrub.value = Math.round((pos/TOTAL_M)*1000);
|
| 757 |
updateHud(s);
|
| 758 |
}
|
| 759 |
|
| 760 |
function updateHud(s){
|
| 761 |
-
const pct
|
| 762 |
-
const leg
|
| 763 |
-
leg.textContent
|
| 764 |
-
leg.style.background
|
| 765 |
-
document.getElementById('hudStreet').innerHTML =
|
| 766 |
-
|
| 767 |
-
|
| 768 |
-
|
| 769 |
-
|
| 770 |
-
|
| 771 |
-
|
| 772 |
-
|
| 773 |
-
|
| 774 |
-
document.querySelectorAll('#out .street.now').forEach(r => r.classList.remove('now'));
|
| 775 |
-
const row = document.querySelector('#out .street[data-step="' + (idx+1) + '"]');
|
| 776 |
if (row){ row.classList.add('now'); if (playing) row.scrollIntoView({block:'nearest'}); }
|
| 777 |
}
|
| 778 |
|
| 779 |
-
// =====
|
| 780 |
function renderStats(s){
|
| 781 |
-
const row
|
| 782 |
-
let h
|
| 783 |
-
h
|
| 784 |
-
h
|
| 785 |
-
h
|
| 786 |
-
h
|
| 787 |
-
h
|
| 788 |
-
h
|
| 789 |
-
h
|
| 790 |
-
h
|
| 791 |
-
h
|
| 792 |
-
if (s.excluded_nodes)
|
| 793 |
-
|
| 794 |
-
|
| 795 |
-
|
| 796 |
-
|
| 797 |
-
|
| 798 |
-
h += '<div class="street" data-step="' + x.step + '" style="border-left-color:' + col + '">' +
|
| 799 |
-
'<span>' + x.step + '. ' + (x.deadhead ? '<i style="color:' + C_DEAD + '">(deadhead)</i> ' : '') +
|
| 800 |
-
x.name + '</span><b>' + x.mi.toFixed(2) + '</b></div>';
|
| 801 |
});
|
| 802 |
-
document.getElementById('out').innerHTML
|
| 803 |
}
|
| 804 |
|
| 805 |
-
function msg(t, cls){
|
| 806 |
-
const m = document.getElementById('msg');
|
| 807 |
-
m.innerHTML = t || ''; m.className = 'msg' + (t ? ' ' + cls : '');
|
| 808 |
-
}
|
| 809 |
</script>
|
| 810 |
</body>
|
| 811 |
</html>
|
|
|
|
| 461 |
<script src="https://unpkg.com/leaflet@1.9.4/dist/leaflet.js"></script>
|
| 462 |
<script src="https://unpkg.com/leaflet-draw@1.0.4/dist/leaflet.draw.js"></script>
|
| 463 |
<script>
|
| 464 |
+
// ===== screenshot drop (reference only; never uploaded) =====
|
| 465 |
const drop = document.getElementById('drop'), file = document.getElementById('file');
|
| 466 |
drop.onclick = () => file.click();
|
| 467 |
file.onchange = e => e.target.files[0] && showShot(e.target.files[0]);
|
| 468 |
+
['dragenter','dragover'].forEach(t => drop.addEventListener(t, e => { e.preventDefault(); drop.classList.add('hot'); }));
|
| 469 |
+
['dragleave','drop'].forEach(t => drop.addEventListener(t, e => { e.preventDefault(); drop.classList.remove('hot'); }));
|
|
|
|
|
|
|
| 470 |
drop.addEventListener('drop', e => {
|
| 471 |
const f = e.dataTransfer.files[0];
|
| 472 |
+
if (f && f.type.startsWith('image/')) showShot(f); else msg('That file is not an image.', 'err');
|
|
|
|
| 473 |
});
|
| 474 |
+
function showShot(f){ const img=document.createElement('img'); img.src=URL.createObjectURL(f); drop.innerHTML=''; drop.appendChild(img); }
|
|
|
|
|
|
|
|
|
|
|
|
|
| 475 |
|
| 476 |
+
// ===== map + basemaps =====
|
| 477 |
const map = L.map('map').setView([39.83, -98.58], 4);
|
| 478 |
+
const streets = L.tileLayer('https://{s}.basemaps.cartocdn.com/rastertiles/voyager/{z}/{x}/{y}{r}.png',
|
|
|
|
| 479 |
{maxZoom:19, attribution:'© OpenStreetMap, © CARTO'});
|
| 480 |
+
const satellite = L.tileLayer('https://server.arcgisonline.com/ArcGIS/rest/services/World_Imagery/MapServer/tile/{z}/{y}/{x}',
|
|
|
|
| 481 |
{maxZoom:19, maxNativeZoom:19, attribution:'Imagery © Esri'});
|
| 482 |
+
const ESRI='https://server.arcgisonline.com/ArcGIS/rest/services/Reference/';
|
| 483 |
+
const roadLabels=L.tileLayer(ESRI+'World_Transportation/MapServer/tile/{z}/{y}/{x}',{maxZoom:19});
|
| 484 |
+
const placeLabels=L.tileLayer(ESRI+'World_Boundaries_and_Places/MapServer/tile/{z}/{y}/{x}',{maxZoom:19});
|
| 485 |
+
const satHybrid=L.layerGroup([satellite, roadLabels, placeLabels]);
|
| 486 |
streets.addTo(map);
|
| 487 |
+
L.control.layers({'Street':streets,'Satellite':satHybrid},{},{position:'bottomleft',collapsed:false}).addTo(map);
|
| 488 |
map.createPane('routePane'); map.getPane('routePane').style.zIndex = 450;
|
| 489 |
|
| 490 |
const drawn = new L.FeatureGroup().addTo(map);
|
| 491 |
+
// all map-layer handles declared ONCE, here
|
| 492 |
+
let arrowLayer=null, truck=null, startMarker=null, shape=null,
|
| 493 |
+
baseLayer=null, doneLayer=null, partialLine=null, startPin=null;
|
| 494 |
|
| 495 |
+
const SHAPE_STYLE={color:'#4f8cff', weight:2, fillOpacity:.06};
|
| 496 |
const boxTool = new L.Draw.Rectangle(map, {shapeOptions:SHAPE_STYLE});
|
| 497 |
const polyTool = new L.Draw.Polygon(map, {shapeOptions:SHAPE_STYLE, allowIntersection:false,
|
| 498 |
showArea:false, drawError:{color:'#d7263d', message:'Edges cannot cross'}});
|
|
|
|
| 523 |
|
| 524 |
document.getElementById('clear').onclick = () => {
|
| 525 |
stop(); drawn.clearLayers();
|
| 526 |
+
[baseLayer, doneLayer, partialLine, arrowLayer, truck, startMarker, startPin].forEach(l => l && map.removeLayer(l));
|
| 527 |
+
baseLayer = doneLayer = partialLine = arrowLayer = truck = startMarker = startPin = shape = null;
|
| 528 |
document.getElementById('solve').disabled = true;
|
| 529 |
document.getElementById('map').classList.remove('routed');
|
| 530 |
document.getElementById('play').classList.remove('on');
|
|
|
|
| 534 |
msg('');
|
| 535 |
};
|
| 536 |
|
| 537 |
+
// ===== geocode =====
|
| 538 |
const q = document.getElementById('q');
|
| 539 |
document.getElementById('find').onclick = find;
|
| 540 |
q.onkeydown = e => { if (e.key === 'Enter') find(); };
|
|
|
|
| 551 |
} catch (err) { msg('Lookup failed: ' + err, 'err'); }
|
| 552 |
}
|
| 553 |
|
| 554 |
+
// ===== solve =====
|
| 555 |
const solveBtn = document.getElementById('solve');
|
| 556 |
solveBtn.onclick = async () => {
|
| 557 |
if (!shape) return;
|
|
|
|
| 570 |
} catch (err) { msg('Solve failed: ' + err, 'err'); }
|
| 571 |
finally { solveBtn.disabled = false; solveBtn.innerHTML = orig; }
|
| 572 |
};
|
| 573 |
+
// ===== route data =====
|
| 574 |
let PATH=[], TOTAL_M=0, DRIVE_MIN=0, STEPS=[];
|
| 575 |
+
const C_TODO='#ffd21e'; // not driven yet = yellow
|
| 576 |
+
const C_DONE='#22c55e'; // driven = green
|
| 577 |
+
const C_DEAD='#ef4444'; // driven deadhead (repeat) = red
|
|
|
|
|
|
|
| 578 |
|
| 579 |
function bearing(a, b){
|
| 580 |
+
const r=Math.PI/180;
|
| 581 |
+
const y=Math.sin((b.lng-a.lng)*r)*Math.cos(b.lat*r);
|
| 582 |
+
const x=Math.cos(a.lat*r)*Math.sin(b.lat*r)-Math.sin(a.lat*r)*Math.cos(b.lat*r)*Math.cos((b.lng-a.lng)*r);
|
| 583 |
+
return (Math.atan2(y,x)*180/Math.PI+360)%360;
|
| 584 |
}
|
| 585 |
|
| 586 |
function render(d){
|
| 587 |
stop();
|
| 588 |
[baseLayer, doneLayer, partialLine, arrowLayer, truck, startPin].forEach(l => l && map.removeLayer(l));
|
|
|
|
| 589 |
const feats = d.geojson.features;
|
| 590 |
+
PATH=[]; TOTAL_M=0;
|
| 591 |
feats.forEach(f => {
|
| 592 |
+
const c=f.geometry.coordinates;
|
| 593 |
+
const a=L.latLng(c[0][1],c[0][0]), b=L.latLng(c[1][1],c[1][0]);
|
| 594 |
+
const len=map.distance(a,b);
|
| 595 |
+
PATH.push({a,b,len,cum:TOTAL_M,name:f.properties.name,deadhead:f.properties.deadhead,order:f.properties.order});
|
| 596 |
+
TOTAL_M+=len;
|
|
|
|
| 597 |
});
|
| 598 |
+
DRIVE_MIN=d.stats.drive_min; STEPS=d.directions;
|
| 599 |
|
| 600 |
+
const line=[PATH[0].a]; PATH.forEach(s=>line.push(s.b));
|
|
|
|
| 601 |
baseLayer = L.layerGroup([
|
| 602 |
+
L.polyline(line,{pane:'routePane',color:'#05070c',weight:9,opacity:.6,lineCap:'round',lineJoin:'round'}),
|
| 603 |
+
L.polyline(line,{pane:'routePane',color:C_TODO,weight:5,opacity:.95,lineCap:'round',lineJoin:'round'})
|
| 604 |
]).addTo(map);
|
| 605 |
|
|
|
|
| 606 |
doneLayer = L.layerGroup([], {pane:'routePane'}).addTo(map);
|
| 607 |
+
partialLine = L.polyline([], {pane:'routePane',color:C_DONE,weight:6,opacity:1,lineCap:'round'}).addTo(map);
|
| 608 |
|
| 609 |
buildArrows();
|
| 610 |
+
startPin = L.marker(PATH[0].a, {zIndexOffset:1100, icon: pinIcon('START / END','#3b82f6')}).addTo(map);
|
|
|
|
| 611 |
truck = L.marker(PATH[0].a, {icon: truckIcon(0), zIndexOffset:1200, interactive:false}).addTo(map);
|
| 612 |
|
| 613 |
+
const bb=L.latLngBounds([]); line.forEach(p=>bb.extend(p));
|
| 614 |
map.fitBounds(bb, {padding:[30,30]});
|
| 615 |
buildLegend();
|
| 616 |
document.getElementById('map').classList.add('routed');
|
| 617 |
document.getElementById('play').classList.add('on');
|
| 618 |
document.getElementById('hud').classList.add('on');
|
| 619 |
renderStats(d.stats);
|
| 620 |
+
resetTravel(); seek(0);
|
|
|
|
| 621 |
}
|
| 622 |
|
|
|
|
| 623 |
function buildArrows(){
|
| 624 |
if (arrowLayer) map.removeLayer(arrowLayer);
|
| 625 |
arrowLayer = L.layerGroup();
|
| 626 |
+
const stride = Math.max(1, Math.ceil(PATH.length/80));
|
| 627 |
+
PATH.forEach((s,i)=>{
|
| 628 |
+
if (i%stride) return;
|
| 629 |
+
if (s.len<28) return;
|
| 630 |
+
const mid=L.latLng((s.a.lat+s.b.lat)/2,(s.a.lng+s.b.lng)/2);
|
| 631 |
+
L.marker(mid,{interactive:false, icon:L.divIcon({className:'',iconSize:[16,16],iconAnchor:[8,8],
|
| 632 |
+
html:'<div class="arrow" style="transform:rotate('+bearing(s.a,s.b)+'deg)">'+
|
| 633 |
+
'<svg width="16" height="16" viewBox="0 0 16 16">'+
|
| 634 |
+
'<path d="M8 2 L12 12 L8 9.6 L4 12 Z" fill="#111" stroke="#fff" stroke-width="1"/>'+
|
| 635 |
'</svg></div>'})}).addTo(arrowLayer);
|
| 636 |
});
|
| 637 |
if (document.getElementById('arrowsOn').checked) arrowLayer.addTo(map);
|
|
|
|
| 641 |
e.target.checked ? arrowLayer.addTo(map) : map.removeLayer(arrowLayer);
|
| 642 |
};
|
| 643 |
|
| 644 |
+
function pinIcon(label,color){
|
| 645 |
+
return L.divIcon({className:'',iconSize:[70,34],iconAnchor:[35,32],html:
|
| 646 |
+
'<div class="pin"><svg width="26" height="30" viewBox="0 0 26 30">'+
|
| 647 |
+
'<path d="M13 29 C13 29 24 17 24 11 A11 11 0 1 0 2 11 C2 17 13 29 13 29 Z" fill="'+color+'" stroke="#05070c" stroke-width="1.5"/>'+
|
| 648 |
+
'<circle cx="13" cy="11" r="4.5" fill="#0f1115"/></svg>'+
|
| 649 |
+
'<div style="margin-top:-2px">'+label+'</div></div>'});
|
|
|
|
| 650 |
}
|
| 651 |
|
| 652 |
function truckIcon(deg){
|
| 653 |
+
return L.divIcon({className:'',iconSize:[34,34],iconAnchor:[17,17],
|
| 654 |
+
html:'<div class="truck" style="transform:rotate('+deg+'deg)">'+
|
| 655 |
+
'<svg width="34" height="34" viewBox="0 0 34 34">'+
|
| 656 |
+
'<circle cx="17" cy="17" r="15" fill="#05070c" opacity=".6"/>'+
|
| 657 |
+
'<g transform="translate(17,17)">'+
|
| 658 |
+
'<rect x="-7" y="-9" width="14" height="18" rx="2.5" fill="#ffffff"/>'+
|
| 659 |
+
'<rect x="-7" y="-9" width="14" height="6.5" rx="2" fill="#16a34a"/>'+
|
| 660 |
+
'<rect x="-8.5" y="-2" width="3" height="6.5" rx="1" fill="#1b2130"/>'+
|
| 661 |
+
'<rect x="5.5" y="-2" width="3" height="6.5" rx="1" fill="#1b2130"/>'+
|
| 662 |
+
'<path d="M0 -14 L5 -9 L-5 -9 Z" fill="#ffd21e"/>'+
|
| 663 |
'</g></svg></div>'});
|
| 664 |
}
|
| 665 |
|
| 666 |
function buildLegend(){
|
| 667 |
+
document.getElementById('legend').innerHTML =
|
| 668 |
+
'<div class="ttl">Route</div>'+
|
| 669 |
+
'<div class="lg"><span class="sw" style="background:'+C_TODO+'"></span>not driven yet</div>'+
|
| 670 |
+
'<div class="lg"><span class="sw" style="background:'+C_DONE+'"></span>driven</div>'+
|
| 671 |
+
'<div class="lg"><span class="sw" style="background:'+C_DEAD+'"></span>driven deadhead (repeat)</div>'+
|
|
|
|
| 672 |
'<div class="lg" style="margin-top:5px"><span class="sw" style="background:#3b82f6"></span>start / end (loop)</div>';
|
| 673 |
+
document.getElementById('legend').style.display='block';
|
| 674 |
}
|
| 675 |
|
| 676 |
+
// ===== animation (video of the truck driving the route) =====
|
| 677 |
let pos=0, playing=false, raf=null, last=0, drawnUpto=0;
|
| 678 |
const pp=document.getElementById('pp'), scrub=document.getElementById('scrub'),
|
| 679 |
speedSel=document.getElementById('speed'), followCk=document.getElementById('follow');
|
| 680 |
pp.onclick = () => playing ? stop() : play();
|
| 681 |
document.getElementById('restart').onclick = () => { seek(0); play(); };
|
| 682 |
+
scrub.oninput = () => { stop(); seek(TOTAL_M*(scrub.value/1000)); };
|
| 683 |
|
| 684 |
+
function resetTravel(){ if (doneLayer) doneLayer.clearLayers(); drawnUpto=0; }
|
|
|
|
|
|
|
|
|
|
|
|
|
| 685 |
|
| 686 |
function play(){
|
| 687 |
if (!PATH.length) return;
|
| 688 |
+
if (pos >= TOTAL_M-0.5){ pos=0; resetTravel(); }
|
| 689 |
+
playing=true; pp.innerHTML='❙❙ Pause';
|
| 690 |
+
last=performance.now(); raf=requestAnimationFrame(tick);
|
| 691 |
}
|
| 692 |
+
function stop(){ playing=false; pp.innerHTML='▶ Play'; if (raf) cancelAnimationFrame(raf); raf=null; }
|
|
|
|
| 693 |
function tick(now){
|
| 694 |
if (!playing) return;
|
| 695 |
+
const dt=Math.min((now-last)/1000,0.1); last=now;
|
| 696 |
+
pos += (TOTAL_M/75)*parseFloat(speedSel.value)*dt;
|
| 697 |
+
if (pos>=TOTAL_M){ seek(TOTAL_M); stop(); return; }
|
| 698 |
+
seek(pos); raf=requestAnimationFrame(tick);
|
| 699 |
}
|
| 700 |
|
| 701 |
function seek(m){
|
| 702 |
if (!PATH.length) return;
|
| 703 |
+
pos=Math.max(0,Math.min(m,TOTAL_M));
|
| 704 |
+
let lo=0,hi=PATH.length-1,i=0;
|
| 705 |
+
while (lo<=hi){ const mid=(lo+hi)>>1,s=PATH[mid];
|
| 706 |
+
if (pos<s.cum) hi=mid-1; else if (pos>s.cum+s.len) lo=mid+1; else { i=mid; break; }
|
| 707 |
+
i=Math.min(lo,PATH.length-1); }
|
| 708 |
+
const s=PATH[i];
|
| 709 |
+
const f=s.len?Math.max(0,Math.min((pos-s.cum)/s.len,1)):0;
|
| 710 |
+
const here=L.latLng(s.a.lat+(s.b.lat-s.a.lat)*f, s.a.lng+(s.b.lng-s.a.lng)*f);
|
| 711 |
+
if (i<drawnUpto) resetTravel();
|
| 712 |
+
while (drawnUpto<i){
|
| 713 |
+
const p=PATH[drawnUpto];
|
| 714 |
+
L.polyline([p.a,p.b],{pane:'routePane',color:p.deadhead?C_DEAD:C_DONE,weight:6,opacity:1,lineCap:'round'}).addTo(doneLayer);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 715 |
drawnUpto++;
|
| 716 |
}
|
|
|
|
| 717 |
partialLine.setLatLngs([s.a, here]);
|
| 718 |
+
partialLine.setStyle({color: s.deadhead?C_DEAD:C_DONE});
|
| 719 |
+
truck.setLatLng(here); truck.setIcon(truckIcon(bearing(s.a,s.b)));
|
|
|
|
|
|
|
|
|
|
| 720 |
if (followCk.checked && playing && !map.getBounds().pad(-0.28).contains(here))
|
| 721 |
map.panTo(here, {animate:true, duration:.5});
|
| 722 |
+
scrub.value=Math.round((pos/TOTAL_M)*1000);
|
|
|
|
| 723 |
updateHud(s);
|
| 724 |
}
|
| 725 |
|
| 726 |
function updateHud(s){
|
| 727 |
+
const pct=pos/TOTAL_M;
|
| 728 |
+
const leg=document.getElementById('hudLeg');
|
| 729 |
+
leg.textContent=Math.round(pct*100)+'% driven';
|
| 730 |
+
leg.style.background=s.deadhead?C_DEAD:C_DONE;
|
| 731 |
+
document.getElementById('hudStreet').innerHTML=(s.deadhead?'<span style="color:'+C_DEAD+'">↷ </span>':'')+s.name;
|
| 732 |
+
document.getElementById('hudMeta').textContent=
|
| 733 |
+
(pos/1609.344).toFixed(2)+' / '+(TOTAL_M/1609.344).toFixed(2)+' mi · ~'+Math.round(DRIVE_MIN*pct)+' min in';
|
| 734 |
+
document.getElementById('hudBar').style.width=(pct*100)+'%';
|
| 735 |
+
document.getElementById('hudBar').style.background=C_DONE;
|
| 736 |
+
let acc=0,idx=0;
|
| 737 |
+
for (let k=0;k<STEPS.length;k++){ acc+=STEPS[k].mi*1609.344; idx=k; if (pos<=acc) break; }
|
| 738 |
+
document.querySelectorAll('#out .street.now').forEach(r=>r.classList.remove('now'));
|
| 739 |
+
const row=document.querySelector('#out .street[data-step="'+(idx+1)+'"]');
|
|
|
|
|
|
|
| 740 |
if (row){ row.classList.add('now'); if (playing) row.scrollIntoView({block:'nearest'}); }
|
| 741 |
}
|
| 742 |
|
| 743 |
+
// ===== stats panel =====
|
| 744 |
function renderStats(s){
|
| 745 |
+
const row=(l,v,c)=>'<div class="stat '+(c||'')+'"><span>'+l+'</span><b>'+v+'</b></div>';
|
| 746 |
+
let h='';
|
| 747 |
+
h+=row('Total route', s.total_mi.toFixed(2)+' mi','big');
|
| 748 |
+
h+=row('Delivering', s.min_mi.toFixed(2)+' mi','good');
|
| 749 |
+
h+=row('Deadhead (repeats)', s.deadhead_mi.toFixed(2)+' mi','warn');
|
| 750 |
+
h+=row('Efficiency', s.efficiency_pct.toFixed(1)+'%');
|
| 751 |
+
h+=row('Overhead vs. minimum', '+'+s.overhead_pct.toFixed(1)+'%');
|
| 752 |
+
h+=row('Streets covered', s.streets.toLocaleString());
|
| 753 |
+
h+=row('Passes (both sides)', s.required_passes.toLocaleString());
|
| 754 |
+
h+=row('Est. driving time', Math.round(s.drive_min)+' min');
|
| 755 |
+
h+=row('Est. walking time', Math.round(s.walk_min)+' min');
|
| 756 |
+
if (s.excluded_nodes) h+='<div class="msg info" style="display:block">'+s.excluded_nodes+' intersection(s) were unreachable and left out.</div>';
|
| 757 |
+
h+='<div class="hd" style="margin-top:8px">Turn-by-turn (in drive order)</div>';
|
| 758 |
+
STEPS.forEach(x=>{
|
| 759 |
+
const col=x.deadhead?C_DEAD:C_DONE;
|
| 760 |
+
h+='<div class="street" data-step="'+x.step+'" style="border-left-color:'+col+'"><span>'+x.step+'. '+
|
| 761 |
+
(x.deadhead?'<i style="color:'+C_DEAD+'">(deadhead)</i> ':'')+x.name+'</span><b>'+x.mi.toFixed(2)+'</b></div>';
|
|
|
|
|
|
|
|
|
|
| 762 |
});
|
| 763 |
+
document.getElementById('out').innerHTML=h;
|
| 764 |
}
|
| 765 |
|
| 766 |
+
function msg(t, cls){ const m=document.getElementById('msg'); m.innerHTML=t||''; m.className='msg'+(t?' '+cls:''); }
|
|
|
|
|
|
|
|
|
|
| 767 |
</script>
|
| 768 |
</body>
|
| 769 |
</html>
|