Atlaslog
Atlaslog / Examples / Turf.js / Dissolve
Turf.js · Runnable Example

Dissolve

Runnable Turf.js example: Dissolve. Open the live demo, read the complete source, and copy the file into your project. No build step or API key.

Open standalone → Chinese notes All Turf.js examples
/demos/turf-dissolve.htmlLIVE
single HTML file · CDN dependencies onlyinterface may contain Chinese labels

Source

demos/turf-dissolve.html
<!--
  示例:多面融合
  核心逻辑:dissolve 按分组字段融合相邻小区,输出轮廓并渲染
  依赖:Turf.js 7 · 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}
  body{display:flex;flex-direction:column;overflow:hidden;
       font:13px/1.6 -apple-system,"Segoe UI","PingFang SC",sans-serif;color:#16181d;background:#fff}
  .bar{flex:0 0 auto;display:flex;gap:12px;align-items:center;padding:8px 12px;
       background:#f6f7f9;border-bottom:1px solid #e3e6ea;font-size:12px;color:#58606e;flex-wrap:wrap}
  .bar button{font:inherit;font-size:12px;padding:4px 10px;border-radius:4px;cursor:pointer;
       border:1px solid #ccd1d9;background:#fff;color:#16181d}
  .bar button:hover{background:#eef0f3}
  .bar input[type=range]{width:150px}
  .bar b{color:#1f5fd0;font-weight:600}
  .hint{color:#8a929f}
  svg{flex:1 1 auto;min-height:0;width:100%;display:block;background:#fbfcfd}
  .out{flex:0 0 auto;display:flex;gap:20px;flex-wrap:wrap;padding:8px 12px;
       border-top:1px solid #e3e6ea;
       font-family:ui-monospace,Consolas,monospace;font-size:11.5px;color:#3a4250}
  .out b{font-weight:600;color:#1f5fd0}
</style>
</head>
<body>
<div class="bar"><span>八个街区按两个片区融合</span><span class="hint">细线为原始街区边界</span></div>
<svg id="stage" viewBox="0 0 640 320" preserveAspectRatio="xMidYMid meet"></svg>
<div class="out">
  <span>原始街区 <b id="n0">—</b></span>
  <span>融合后 <b id="n1">—</b></span>
  <span>片区 A <b id="a">—</b></span>
  <span>片区 B <b id="b">—</b></span>
</div>
<script src="https://cdn.jsdelivr.net/npm/@turf/turf@7/turf.min.js"></script>
<script>
(function () {
  'use strict';

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

  function makeProjection(BOX, W, H, PAD) {
    var kx = Math.cos(((BOX[1] + BOX[3]) / 2) * Math.PI / 180);
    var x0 = BOX[0] * kx, x1 = BOX[2] * kx;
    var y0 = BOX[1], y1 = BOX[3];
    var s = Math.min((W - PAD * 2) / (x1 - x0), (H - PAD * 2) / (y1 - y0));
    var ox = (W - (x1 - x0) * s) / 2;
    var oy = (H - (y1 - y0) * s) / 2;
    var to = function (c) {
      return [(c[0] * kx - x0) * s + ox, (y1 - c[1]) * s + oy];
    };
    to.inverse = function (px, py) {
      return [((px - ox) / s + x0) / kx, y1 - (py - oy) / s];
    };
    return to;
  }

  function linePath(pr, coords) {
    var d = '';
    for (var i = 0; i < coords.length; i++) {
      var p = pr(coords[i]);
      d += (i ? 'L' : 'M') + p[0].toFixed(1) + ' ' + p[1].toFixed(1);
    }
    return d;
  }

  function ringPath(pr, ring) { return linePath(pr, ring) + 'Z'; }

  function polyPath(pr, rings) {
    var d = '';
    for (var i = 0; i < rings.length; i++) d += ringPath(pr, rings[i]);
    return d;
  }
  var BOX = [116.30, 39.83, 116.50, 39.95];
  var pr = makeProjection(BOX, 640, 320, 26);
  var stage = document.getElementById('stage');

  var cells = [];
  var w = 0.035, h = 0.04;
  for (var x = 0; x < 4; x++) {
    for (var y = 0; y < 2; y++) {
      var x0 = 116.32 + x * w;
      var y0 = 39.86 + y * h;
      cells.push(turf.polygon([[
        [x0, y0], [x0 + w, y0], [x0 + w, y0 + h], [x0, y0 + h], [x0, y0]
      ]], { group: x < 2 ? 'A' : 'B', name: '街区 ' + (x * 2 + y + 1) }));
    }
  }

  var fc = turf.featureCollection(cells);
  var merged = turf.dissolve(fc, { propertyName: 'group' });

  var parts = ['<rect x="0" y="0" width="640" height="320" fill="none" stroke="#e3e6ea"/>'];

  merged.features.forEach(function (f) {
    var fill = f.properties.group === 'A' ? 'rgba(31,95,208,0.18)' : 'rgba(15,138,122,0.18)';
    var stroke = f.properties.group === 'A' ? '#1f5fd0' : '#0f8a7a';
    var rings = f.geometry.type === 'Polygon'
      ? f.geometry.coordinates
      : f.geometry.coordinates[0];
    parts.push('<path d="' + polyPath(pr, rings) + '" fill="' + fill +
               '" fill-rule="evenodd" stroke="' + stroke + '" stroke-width="2.4"/>');
  });

  cells.forEach(function (c) {
    parts.push('<path d="' + polyPath(pr, c.geometry.coordinates) +
               '" fill="none" stroke="#8a929f" stroke-width="0.8" stroke-dasharray="3 3"/>');
  });

  stage.innerHTML = parts.join('');

  document.getElementById('n0').textContent = cells.length;
  document.getElementById('n1').textContent = merged.features.length;

  merged.features.forEach(function (f) {
    var area = turf.area(f) / 1e6;
    if (f.properties.group === 'A') {
      document.getElementById('a').textContent = area.toFixed(2) + ' km²';
    } else {
      document.getElementById('b').textContent = area.toFixed(2) + ' km²';
    }
  });
})();
</script>
</body>
</html>
Language: the code and this page are in English; the full tutorial and API notes for this example are in Chinese. The demo itself is the authoritative version — it runs exactly as shown.

More Turf.js examples

Turf.js · EXAMPLEDistance Turf.js · EXAMPLEExplode Turf.js · EXAMPLEGeoJSON Transform