Atlaslog
Atlaslog / Examples / deck.gl / Business scenarios / Patrol Routes and Surveillance Points
deck.gl · Business scenario

Patrol Routes and Surveillance Points

Eight closed patrol loops of 60 points each, 120 surveillance points underneath, and eight patrol cars moving at constant speed along cumulative arc length, switchable between 1x and 2x.

Open standalone → Chinese notes All business examples
/demos/deckgl-biz-city-patrol.htmlLIVE
single HTML file · CDN dependencies onlyinterface labels are in Chinese

Business context

Police, city management and campus security use patrol route maps to assign shifts and find coverage gaps, and to check whether a route was actually walked. Data comes from vehicle GPS tracks or checkpoint sequences plus a fixed camera point table. A substation usually has dozens of routes and a hundred or more cameras; second-level tracks reach hundreds of thousands of points per day and are thinned to a route skeleton for display.

Tracks are thinned and smoothed before rendering, resampled by cumulative arc length in metres so that dense points from stopping at lights are removed. After normalising to WGS84, PathLayer expresses road scale with metre-based width and falls back to widthMinPixels at small scales. Vehicle position is found by binary search on cumulative arc length and then interpolated within the segment, never by rounding a point index, so speed stays uniform through curves and straights.

Line thickness is easily read as road class or traffic volume, when in fact the width is a fixed visual constant. Coverage claims have their own definition problem: passing a route is not the same as dwelling on it, so a dwell-time threshold is needed. Whether a camera's visible area is computed as a view sector or a radius circle changes the coverage conclusion substantially; counting camera points as covered area is not a coverage measurement.

How it works

Each route is generated from a centre and a base radius with 60 points, stepping the polar angle by 2π/60 and scaling the radius by 1 + 0.2·sin(3θ + phase) to produce an irregular loop, then copying the first point to the end so the loop closes and PathLayer can draw the last stroke back to the start. PathLayer uses widthUnits meters, a width of 16 metres and widthMinPixels 2.5, so lines do not thin out at regional zoom and thicken with scale when zoomed in. Cumulative arc length converts lng/lat differences to metres with 111320·cos(lat) and 110540, yielding the cum array and total length for interpolation.

Patrol car position is found by binary search on cum for the target arc length and then interpolated linearly within that segment; rounding to a point index instead makes a car jump a shorter geographic distance on tight curves than on straights and appear to speed up and slow down. The eight cars carry 1/8 phase offsets, so at any instant they sit at different progress along their own routes. Unchecking the trail leaves only surveillance points and cars, unchecking surveillance points shows the route skeleton alone, and the speed button moves the car from 0.05 to 0.1 laps per second, cutting a lap from about 20 seconds to 10.

ConfigurationWhat it does
60-point closed loopPolar generation with the first point copied to the end so the loop closes
width 16 mMetre-based width that scales with map scale
widthMinPixels 2.5Keeps a minimum visible width at regional zoom
Cumulative arc interpolationPosition by distance rather than point index, giving constant speed

Note. Change pointAt to round by point index and watch the curves: cars visibly stutter, which is the most direct check of arc-length interpolation.

Source

demos/deckgl-biz-city-patrol.html
<!--
  示例:巡逻路径与监控点
  要点:8 条闭合路线叠加监控点,巡逻车按累计弧长逐帧插值,速度可切 1x/2x
  依赖:deck.gl 9.0 · jsDelivr CDN
-->
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta name="robots" content="noindex,follow">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>巡逻路径与监控点</title>
<style>
  html,body{height:100%;margin:0;background:#0d1117}
  body{display:flex;flex-direction:column;overflow:hidden;
       font:13px/1.6 -apple-system,"Segoe UI","PingFang SC",sans-serif;color:#e6e8ec}
  .bar{position:relative;z-index:5;flex:0 0 auto;padding:8px 12px;background:#161b22;border-bottom:1px solid #252c36;
       display:flex;gap:12px;align-items:center;font-size:12px;color:#8b93a1;flex-wrap:wrap}
  .bar b{color:#58a6ff;font-weight:600}
  .bar button{font:inherit;font-size:12px;padding:3px 10px;border-radius:5px;cursor:pointer;
              border:1px solid #303845;background:#1d2531;color:#c8d2e0}
  .bar button:hover{background:#253040}
  .bar button.on{background:#2c4a7a;border-color:#4f8ad6;color:#dfe9ff}
  .bar .sep{width:1px;height:18px;background:#2a313c;margin:0 4px}
  .bar label{display:flex;align-items:center;gap:5px;cursor:pointer}
  .bar input[type=checkbox]{accent-color:#4f9ef8;margin:0}
  #cv{flex:1 1 auto;min-height:0;width:100%;display:block}
</style>
</head>
<body>
<div class="bar">
  <span>路线 <b>8</b> · 监控点 <b>120</b></span>
  <span class="sep"></span>
  <label><input id="tr" type="checkbox" checked> 显示轨迹</label>
  <label><input id="mo" type="checkbox" checked> 显示监控点</label>
  <span class="sep"></span>
  <button id="spd">1x</button>
  <span>巡逻车按累计弧长插值</span>
</div>
<canvas id="cv"></canvas>
<script src="https://cdn.jsdelivr.net/npm/deck.gl@9.0.35/dist.min.js"></script>
<script>
(function () {
  'use strict';

  if (typeof deck === 'undefined') {
    document.body.innerHTML =
      '<div style="padding:24px;color:#ff8c74">deck.gl 加载失败,请检查能否访问 cdn.jsdelivr.net</div>';
    return;
  }

  function rng(seed) {
    var a = seed >>> 0;
    return function () {
      a = (a + 0x6D2B79F5) >>> 0;
      var t = Math.imul(a ^ (a >>> 15), 1 | a);
      t = (t + Math.imul(t ^ (t >>> 7), 61 | t)) ^ t;
      return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
    };
  }

  var rand = rng(918273);
  var palette = [[64,154,255],[86,214,168],[246,186,78],[240,104,72],[176,132,255],[64,206,226],[255,160,120],[130,200,255]];

  function distM(a, b) {
    var dx = (b[0] - a[0]) * 111320 * Math.cos((a[1] + b[1]) / 2 * Math.PI / 180);
    var dy = (b[1] - a[1]) * 110540;
    return Math.sqrt(dx * dx + dy * dy);
  }

  var routes = [];
  for (var r = 0; r < 8; r++) {
    var cx = 116.10 + (r % 4) * 0.17;
    var cy = 39.80 + Math.floor(r / 4) * 0.20;
    var R = 0.040 + (r % 2) * 0.010;
    var path = [];
    for (var s = 0; s < 60; s++) {
      var a = s / 60 * Math.PI * 2;
      var wob = 1 + 0.2 * Math.sin(a * 3 + r * 1.7);
      path.push([cx + Math.cos(a) * R * wob, cy + Math.sin(a) * R * 0.78 * wob]);
    }
    path.push([path[0][0], path[0][1]]);

    // 累计弧长:插值按距离查段,不按点序号
    var cum = [0];
    for (var k = 1; k < path.length; k++) {
      cum.push(cum[k - 1] + distM(path[k - 1], path[k]));
    }
    routes.push({ path: path, cum: cum, total: cum[cum.length - 1],
      color: palette[r], phase: r / 8 });
  }

  var monitors = [];
  for (var m = 0; m < 120; m++) {
    var v = routes[m % 8].path[(m * 7) % 60];
    monitors.push([v[0] + (rand() - 0.5) * 0.012, v[1] + (rand() - 0.5) * 0.010]);
  }

  var routeLayer = new deck.PathLayer({
    id: 'patrol-routes',
    data: routes,
    getPath: function (d) { return d.path; },
    getWidth: 16,
    widthUnits: 'meters',
    widthMinPixels: 2.5,
    getColor: function (d) { return [d.color[0], d.color[1], d.color[2], 115]; },
    jointRounded: true,
    capRounded: true
  });

  var monitorLayer = new deck.ScatterplotLayer({
    id: 'patrol-monitors',
    data: monitors,
    getPosition: function (d) { return d; },
    getRadius: 3,
    radiusUnits: 'pixels',
    getFillColor: [130, 208, 255, 210],
    stroked: true,
    getLineColor: [214, 240, 255, 180],
    lineWidthUnits: 'pixels',
    getLineWidth: 1
  });

  function pointAt(route, t) {
    var s = ((t % 1) + 1) % 1 * route.total;
    var cum = route.cum;
    var lo = 1;
    var hi = cum.length - 1;
    while (lo < hi) {
      var mid = (lo + hi) >> 1;
      if (cum[mid] < s) lo = mid + 1;
      else hi = mid;
    }
    var i = lo;
    var seg = cum[i] - cum[i - 1];
    var frac = seg > 0 ? (s - cum[i - 1]) / seg : 0;
    var a = route.path[i - 1];
    var b = route.path[i];
    return [a[0] + (b[0] - a[0]) * frac, a[1] + (b[1] - a[1]) * frac];
  }

  function makeCar(t) {
    var data = [];
    for (var i = 0; i < routes.length; i++) {
      data.push(pointAt(routes[i], routes[i].phase + t));
    }
    return new deck.ScatterplotLayer({
      id: 'patrol-cars',
      data: data,
      getPosition: function (p) { return p; },
      getRadius: 7,
      radiusUnits: 'pixels',
      getFillColor: [255, 232, 150, 255],
      stroked: true,
      getLineColor: [24, 30, 40, 255],
      lineWidthUnits: 'pixels',
      getLineWidth: 2
    });
  }

  var showTrace = true;
  var showMonitors = true;
  var speed = 1, clock = 0, last = performance.now();
  var carLayer = makeCar(0);

  function compose() {
    var layers = [];
    if (showTrace) layers.push(routeLayer);
    if (showMonitors) layers.push(monitorLayer);
    layers.push(carLayer);
    return layers;
  }

  var deckgl = new deck.Deck({
    canvas: 'cv',
    width: '100%',
    height: '100%',
    initialViewState: {
      longitude: 116.36,
      latitude: 39.90,
      zoom: 10.4,
      pitch: 0,
      bearing: 0
    },
    controller: true,
    layers: compose()
  });

  function frame(now) {
    var dt = Math.min(0.05, (now - last) / 1000);
    last = now;
    clock += dt * 0.05 * speed;
    carLayer = makeCar(clock);
    deckgl.setProps({ layers: compose() });
    requestAnimationFrame(frame);
  }
  requestAnimationFrame(frame);

  document.getElementById('tr').addEventListener('change', function (e) {
    showTrace = e.currentTarget.checked;
    deckgl.setProps({ layers: compose() });
  });

  document.getElementById('mo').addEventListener('change', function (e) {
    showMonitors = e.currentTarget.checked;
    deckgl.setProps({ layers: compose() });
  });

  document.getElementById('spd').addEventListener('click', function (e) {
    speed = speed === 1 ? 2 : 1;
    e.currentTarget.textContent = speed + 'x';
    e.currentTarget.classList.toggle('on', speed === 2);
  });

  window.__deck = deckgl;
})();
</script>
</body>
</html>
Language: this page is in English; the demo keeps Chinese interface labels and code comments. The Chinese page carries the full notes and the same runnable file.

More business examples

deck.gl · APIPathLayer deck.gl · APIScatterplotLayer deck.gl · APITripsLayer