Calculating NDVI+NDWI as green and blue values.
The GeoTIFF layer in this example calculates the Normalized Difference Vegetation Index (NDVI) and Normalized Difference Water Index (NDWI) from two cloud-optimized Sentinel 2 GeoTIFFs: one with 10 m resolution and red and a near infrared bands, and one with 60 m resolution and a short wave infrared channel. The NDVI is shown as green, the NDWI as blue. The 4th band is the alpha band, which gets added when a source has a nodata
value configured.
import 'ol/ol.css';
import GeoTIFF from 'ol/source/GeoTIFF';
import Map from 'ol/Map';
import TileLayer from 'ol/layer/WebGLTile';
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(),
});
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>NDVI+NDWI from two 16-bit COGs</title>
<!-- Pointer events polyfill for old browsers, see https://caniuse.com/#feat=pointer -->
<script src="https://unpkg.com/elm-pep"></script>
<!-- The lines below are only needed for old environments like Internet Explorer and Android 4.x -->
<script src="https://cdn.polyfill.io/v3/polyfill.min.js?features=fetch,requestAnimationFrame,Element.prototype.classList,TextDecoder"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/core-js/3.18.3/minified.js"></script>
<style>
.map {
width: 100%;
height:400px;
}
</style>
</head>
<body>
<div id="map" class="map"></div>
<script src="main.js"></script>
</body>
</html>
{
"name": "cog-math-multisource",
"dependencies": {
"ol": "6.12.0"
},
"devDependencies": {
"parcel": "^2.0.0"
},
"scripts": {
"start": "parcel index.html",
"build": "parcel build --public-url . index.html"
}
}