Atlaslog
Atlaslog / Examples / OpenLayers / Cog Math
OpenLayers · Runnable Example

Cog Math

Runnable OpenLayers example: Cog Math. 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 OpenLayers examples
/demos/openlayers-cog-math.htmlLIVE
single HTML file · CDN dependencies onlyinterface may contain Chinese labels

Source

demos/openlayers-cog-math.html
<!--
  示例:栅格数学运算
  要点:ol.source.Raster 的 operation 拿到像元数组,逐点做阈值与伽马变换
  依赖:OpenLayers 10.3.1 · 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>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/ol@10.3.1/ol.css">
<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}
  .bar{flex:0 0 auto;padding:8px 12px;background:#f6f7f9;border-bottom:1px solid #e3e6ea;
       display:flex;gap:14px;align-items:center;font-size:12px;color:#58606e;flex-wrap:wrap}
  .bar input[type=range]{width:120px}
  #map{flex:1 1 auto;min-height:0;background:#eef0f3}
</style>
</head>
<body>
<div class="bar">
  <label for="g">伽马 <b id="gv">1.0</b></label>
  <input type="range" id="g" min="0.3" max="2.5" step="0.1" value="1">
  <label for="t">阈值 <b id="tv">0.55</b></label>
  <input type="range" id="t" min="0" max="1" step="0.05" value="0.55">
</div>
<div id="map"></div>
<script src="https://cdn.jsdelivr.net/npm/ol@10.3.1/dist/ol.js"></script>
<script>
(function () {
  'use strict';
  if (!window.ol) {
    document.getElementById('map').textContent = 'OpenLayers 加载失败,请检查 cdn.jsdelivr.net 是否可访问';
    return;
  }

  var SIZE = 256;
  var extent = ol.proj.transformExtent([116.20, 39.75, 116.62, 40.05], 'EPSG:4326', 'EPSG:3857');

  /* 单波段数据:城市热岛式的起伏,页面内直接算出来 */
  var field = (function () {
    var c = document.createElement('canvas');
    c.width = c.height = SIZE;
    var ctx = c.getContext('2d');
    var img = ctx.createImageData(SIZE, SIZE);
    for (var y = 0; y < SIZE; y++) {
      for (var x = 0; x < SIZE; x++) {
        var u = x / SIZE - 0.5, v = y / SIZE - 0.5;
        var d = Math.sqrt(u * u + v * v) * 2.6;
        var t = Math.max(0, 1 - d) * 0.9 + 0.12 * Math.sin(u * 30) * Math.cos(v * 26);
        var i = (y * SIZE + x) * 4;
        var g = Math.round(Math.max(0, Math.min(1, t)) * 255);
        img.data[i] = img.data[i + 1] = img.data[i + 2] = g;
        img.data[i + 3] = 255;
      }
    }
    ctx.putImageData(img, 0, 0);
    return c.toDataURL('image/png');
  })();

  var gamma = 1.0, threshold = 0.55;

  function build() {
    var raster = new ol.source.Raster({
      sources: [new ol.source.ImageStatic({
        url: field, imageExtent: extent, projection: 'EPSG:3857'
      })],
      operation: function (pixels) {
        var input = pixels[0];
        var out = new ImageData(input.width, input.height);
        for (var i = 0; i < input.data.length; i += 4) {
          var v = input.data[i] / 255;
          /* 伽马校正后按阈值二值化:高于阈值画红,低于画蓝 */
          var g = Math.pow(v, gamma);
          var rgb = g >= threshold
            ? [200 + 55 * g, 70 + 40 * (1 - g), 52]
            : [30, 70 + 90 * g, 180 + 60 * g];
          out.data[i] = rgb[0];
          out.data[i + 1] = rgb[1];
          out.data[i + 2] = rgb[2];
          out.data[i + 3] = 235;
        }
        return out;
      }
    });
    return new ol.layer.Image({ source: raster });
  }

  var map = new ol.Map({
    target: 'map',
    layers: [new ol.layer.Tile({ source: new ol.source.XYZ({url: 'https://server.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer/tile/{z}/{y}/{x}', attributions: 'Tiles © Esri'}) })],
    view: new ol.View({ center: ol.proj.fromLonLat([116.40, 39.90]), zoom: 10 }),
    controls: ol.control.defaults.defaults({ rotate: false })
  });

  var layer = build();
  map.addLayer(layer);

  function rebuild() {
    map.removeLayer(layer);
    layer = build();
    map.addLayer(layer);
  }

  document.getElementById('g').addEventListener('input', function () {
    gamma = Number(this.value);
    document.getElementById('gv').textContent = gamma.toFixed(1);
    rebuild();
  });

  document.getElementById('t').addEventListener('input', function () {
    threshold = Number(this.value);
    document.getElementById('tv').textContent = threshold.toFixed(2);
    rebuild();
  });
})();
</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 OpenLayers examples

OpenLayers · EXAMPLECog Math Multisource OpenLayers · EXAMPLECog Modeltransformation OpenLayers · EXAMPLECog Overviews