原文链接及内容

运行界面

这个例子计算了归一化植被指数(NDVI)和归一化水指数(NDWI),使用了2个来自cloud-optimized Sentinel 2的GeoTIFF数据: 一个10米分辨率,具有红色和近红外波段,另一个60米分辨率,具有短波红外通道。NDVI显示为绿色,NDWI显示为蓝色。第四个波段是alpha波段,当数据源配置了nodata值时会添加这个波段。

main.js代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import GeoTIFF from 'ol/source/GeoTIFF.js';
import Map from 'ol/Map.js';
import TileLayer from 'ol/layer/WebGLTile.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代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<!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>
<!-- Pointer events polyfill for old browsers, see https://caniuse.com/#feat=pointer -->
<script src="https://cdn.jsdelivr.net/npm/elm-pep@1.0.6/dist/elm-pep.js"></script>
<script type="module" src="main.js"></script>
</body>
</html>