NDVI on L2A Vegetation and Natural Colours Script

// inspired by Custom script repository

// ndviColorMap taken from NDVI script description

// findColor function taken from ...

// B8A is used for NDVI computation as B8A spectral response is narrower than band B08

// works on L2A data with SCL layer vegetation class

// naturalColour combination B04, B03, B02 with gain adapted to L2A surface reflectance



var naturalColour = [3*B04, 3*B03, 3*B02];

let ndviColorMap = [
	[-1.0, 0x000000],
	[-0.2, 0xA50026],
	[0.0,  0xD73027],
	[0.1,  0xF46D43],
	[0.2,  0xFDAE61],
	[0.3,  0xFEE08B],
	[0.4,  0xFFFFBF],
	[0.5,  0xD9EF8B],
	[0.6,  0xA6D96A],
	[0.7,  0x66BD63],
	[0.8,  0x1A9850],
    [0.9,  0x006837]
];

function index(x, y) {
	return (x - y) / (x + y);
}

function toRGB(val) {
	return [val >>> 16, val >>> 8, val].map(x => (x & 0xFF) / 0xFF);
}

function findColor(colValPairs, val) {
	let n = colValPairs.length;
	for (let i = 1; i < n; i++) {
		if (val <= colValPairs[i][0]) {
			return toRGB(colValPairs[i-1][1]);
		}
	}
	return toRGB(colValPairs[n-1][1]);
}

return (SCL == 4)? 
  findColor(ndviColorMap, index(B8A, B04)): naturalColour;

Evaluate and Visualize

General description of the script

This script works on Sentinel-2 L2A products, using the Vegetation class from the “Scene Classification map”, to display a colour-coded normalized difference vegetation index, abbreviated NDVI, on pixels classified as vegetation and natural colours of surface reflectance (red = B04, green = B03, blue = B02) otherwise (water, clouds, snow, not-vegetated land pixels).

The normalized difference vegetation index, abbreviated NDVI, is defined as

        NDVI = (B8A - B04) / (B8A + B04).

B8A band at 20 m resolution is used for NDVI computation as B8A spectral response is narrower than band B08 and less impacted by water vapour content.

It is an indicator of live green vegetation as described in [1].

Details of the script

Due to visualization issue with the Scene Classification Map this script performs better when displaying data close to the native pixel size with respect to screen size, therefore when the zoom level on bottom right of your EO Browser web window indicates a scale of 2 km or 3 km.

It works better in vegetated area without too many clouds however the L2A vegetation class is pretty reliable. The natural colours visualisation is obtained with a gain of 3.0 optimized for the visualisation of surface reflectance:

        naturalColour = [3*B04, 3*B03, 3*B02]

Author of the script

Jérôme LOUIS

Description of representative images

1) The two images of forest of Compiègne acquired 6 months apart (early summer vs early winter) show the difference of forest NDVI depending on the season.

The early summer image

Compiègne, France - early summer

The early winter image

Compiègne, France - early winter

2) The borders of the Virunga Park lying across the boundaries of three states (DRC, Ugunda, Rwanda) are clearly visible as the NDVI values differ between the Park and the surrounded areas more influenced by human land-use.

Virunga Park

NDVI color legend

NDVI color legend

References

[1] Wikipedia, Normalized Difference Vegetation Index. Accessed on January 21th 2020.

Credits

This script is inspired by Custom script repository:

  • ndviColorMap values taken from NDVI script description
  • findColor function taken from Custom Processing Scripts documentation