Skip to content

NDVI 来自 a COG 与 a raster source ​

NDVI 来自 a COG 与 a raster source示例。 COG NDVI 栅格

本示例使用 ol/source/Raster 操作,根据两个 Sentinel 2 波段计算归一化植被指数(NDVI)。栅格数据源以 GeoTIFF 数据瓦片源作为输入,按其原生瓦片分辨率进行采样,并运行一个将 NDVI 映射到色带的像素操作。地图视图采用 Web Mercator 投影,因此该操作在数据源自带的投影中运行,其结果会被重投影到视图 —— OSM 底层显示了这种对齐效果。可以与 NDVI 来自 a Sentinel 2 COG 示例对比,后者使用的是 WebGL 图层样式。将鼠标指针移到地图上,即可从数据源的 getData() 查询中读取 NDVI 值。

main.js
js
import Map from 'ol/Map.js';
import ImageLayer from 'ol/layer/Image.js';
import GeoTIFF from 'ol/source/GeoTIFF.js';
import RasterSource from 'ol/source/Raster.js';

// `normalize: false` 保持原始的 16 位反射率数据为 Float32Array,
// 这样操作会在真实的波段值上进行 NDVI 计算。
const cog = new GeoTIFF({
  normalize: false,
  sources: [
    {
      // 可见光红波段
      url: 'https://sentinel-cogs.s3.us-west-2.amazonaws.com/sentinel-s2-l2a-cogs/36/Q/WD/2020/7/S2A_36QWD_20200701_0_L2A/B04.tif',
    },
    {
      // 近红外波段
      url: 'https://sentinel-cogs.s3.us-west-2.amazonaws.com/sentinel-s2-l2a-cogs/36/Q/WD/2020/7/S2A_36QWD_20200701_0_L2A/B08.tif',
    },
  ],
});

// NDVI 色带,通过 "beforeoperations" 事件传递给操作。
const ndviStops = [
  -0.2,
  [191, 191, 191],
  -0.1,
  [219, 219, 219],
  0,
  [255, 255, 224],
  0.025,
  [255, 250, 204],
  0.05,
  [237, 232, 181],
  0.075,
  [222, 217, 156],
  0.1,
  [204, 199, 130],
  0.125,
  [189, 184, 107],
  0.15,
  [176, 194, 97],
  0.175,
  [163, 204, 89],
  0.2,
  [145, 191, 82],
  0.25,
  [128, 179, 71],
  0.3,
  [112, 163, 64],
  0.35,
  [97, 150, 54],
  0.4,
  [79, 138, 46],
  0.45,
  [64, 125, 36],
  0.5,
  [48, 110, 28],
  0.55,
  [33, 97, 18],
  0.6,
  [15, 84, 10],
  0.65,
  [0, 69, 0],
];

/**
 * 使用 `value, [r, g, b]` 色带把一个值映射为 `[r, g, b, a]` 颜色。
 * 通过 `lib` 选项提供给操作,以便在 worker 中可用。
 * @param {number} value 要映射的值。
 * @param {Array<number|Array<number>>} stops 扁平的 `value, color` 色带节点。
 * @return {Array<number>} `[r, g, b, a]` 颜色。
 */
function colorFromRamp(value, stops) {
  if (value <= stops[0]) {
    const first = stops[1];
    return [first[0], first[1], first[2], 255];
  }
  for (let i = 2; i < stops.length; i += 2) {
    if (value <= stops[i]) {
      const lowValue = stops[i - 2];
      const lowColor = stops[i - 1];
      const highValue = stops[i];
      const highColor = stops[i + 1];
      const f = (value - lowValue) / (highValue - lowValue);
      return [
        lowColor[0] + f * (highColor[0] - lowColor[0]),
        lowColor[1] + f * (highColor[1] - lowColor[1]),
        lowColor[2] + f * (highColor[2] - lowColor[2]),
        255,
      ];
    }
  }
  const last = stops[stops.length - 1];
  return [last[0], last[1], last[2], 255];
}

/**
 * 由两个输入波段计算 NDVI,并通过色带映射为颜色。
 * @param {Array<Array<number>>} pixels 每个输入源一个像素;第一个(也是唯一的)
 *     输入包含红色和近红外波段。
 * @param {Object} data 在 "beforeoperations" 事件中填充的对象。
 * @return {Array<number>} 一个 `[r, g, b, a]` 像素。
 */
function ndviToColor(pixels, data) {
  const red = pixels[0][0];
  const nir = pixels[0][1];
  const sum = nir + red;
  if (!sum) {
    // 数据范围之外为透明
    return [0, 0, 0, 0];
  }
  return colorFromRamp((nir - red) / sum, data.stops);
}

const raster = new RasterSource({
  sources: [cog],
  operation: ndviToColor,
  // `lib` 对象中的函数可供 worker 中的操作使用。
  lib: {
    colorFromRamp: colorFromRamp,
  },
});

raster.on('beforeoperations', (event) => {
  event.data.stops = ndviStops;
});

const map = new Map({
  target: 'map',
  layers: [new ImageLayer({source: raster})],
  view: cog.getView(),
});

// getData() 返回某一坐标处的输入波段值,因此我们可以计算并显示
// 上方颜色所依据的 NDVI 值。
const output = document.getElementById('output');
map.on(['pointermove', 'click'], function (event) {
  const pixels = raster.getData(event.coordinate);
  if (!pixels) {
    output.textContent = ' ';
    return;
  }
  const [red, nir] = pixels[0];
  const sum = nir + red;
  output.textContent = sum ? ((nir - red) / sum).toFixed(2) : ' ';
});
index.html
html
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8">
    <title>使用栅格数据源从 COG 计算 NDVI</title>
    <link rel="stylesheet" href="node_modules/ol/ol.css">
    <style>
      .map {
        width: 100%;
        height: 400px;
      }
    </style>
  </head>
  <body>
    <div id="map" class="map"></div>
    <div>指针处的 NDVI: <span id="output">&nbsp;</span></div>

    <script type="module" src="main.js"></script>
  </body>
</html>
package.json
json
{
  "name": "cog-math-raster",
  "dependencies": {
    "ol": "10.11.0"
  },
  "devDependencies": {
    "vite": "^8.1.5"
  },
  "scripts": {
    "start": "vite",
    "build": "vite build"
  }
}