Edit

从两个 16 位 COG 计算 NDVI+NDWI

cog15 ndvi3 ndwi1 sentinel1 geotiff5

将 NDVI+NDWI 计算为绿色和蓝色值。

本示例中的 GeoTIFF 图层从两个云优化的 Sentinel 2 GeoTIFF 计算归一化植被指数 (NDVI) 和归一化水体指数 (NDWI):一个具有 10 米分辨率、红波段和近红外波段,另一个具有 60 米分辨率和短波红外通道。NDVI 显示为绿色,NDWI 显示为蓝色。第 4 个波段是 alpha 波段,当数据源配置了 nodata 值时会添加该波段。

main.js
import Map from 'ol/Map.js';
import TileLayer from 'ol/layer/WebGLTile.js';
import GeoTIFF from 'ol/source/GeoTIFF.js';

const source = new GeoTIFF({
  sources: [
    {
      url: 'https://s2downloads.eox.at/demo/Sentinel-2/3857/R10m.tif',
      bands: [3, 4],
      min: 0,
      nodata: 0,
      max: 65535,
    },
    {
      url: 'https://s2downloads.eox.at/demo/Sentinel-2/3857/R60m.tif',
      bands: [9],
      min: 0,
      nodata: 0,
      max: 65535,
    },
  ],
});
source.setAttributions(
  "<a href='https://s2maps.eu'>Sentinel-2 cloudless</a> by <a href='https://eox.at/'>EOX IT Services GmbH</a> (Contains modified Copernicus Sentinel data 2019)",
);

const ndvi = [
  '/',
  ['-', ['band', 2], ['band', 1]],
  ['+', ['band', 2], ['band', 1]],
];

const ndwi = [
  '/',
  ['-', ['band', 3], ['band', 1]],
  ['+', ['band', 3], ['band', 1]],
];

const map = new Map({
  target: 'map',
  layers: [
    new TileLayer({
      style: {
        color: [
          'color',
          // red: | NDVI - NDWI |
          ['*', 255, ['abs', ['-', ndvi, ndwi]]],
          // green: NDVI
          ['*', 255, ndvi],
          // blue: NDWI
          ['*', 255, ndwi],
          // alpha
          ['band', 4],
        ],
      },
      source,
    }),
  ],
  view: source.getView(),
});
index.html
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8">
    <title>NDVI+NDWI from two 16-bit COGs</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>

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