Skip to content

NDVI+NDWI 来自 two 16-bit COGs ​

NDVI+NDWI 来自 two 16-bit COGs示例。 COG NDVI NDWI sentinel GeoTIFF

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

main.js
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',
          // 红色:| NDVI - NDWI |
          ['*', 255, ['abs', ['-', ndvi, ndwi]]],
          // 绿色:NDVI
          ['*', 255, ndvi],
          // 蓝色:NDWI
          ['*', 255, ndwi],
          // alpha 通道
          ['band', 4],
        ],
      },
      source,
    }),
  ],
  view: source.getView(),
});
index.html
html
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8">
    <title>来自两个 16 位 COG 的 NDVI+NDWI</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
json
{
  "name": "cog-math-multisource",
  "dependencies": {
    "ol": "10.11.0"
  },
  "devDependencies": {
    "vite": "^8.1.5"
  },
  "scripts": {
    "start": "vite",
    "build": "vite build"
  }
}