How to Calculate Various Spectral Indices in Google Earth Engine Quickly

What is Spectral Index?

In remote sensing we frequently use combinations of various bands to enhance visibility of an object so it's easier to differentiate with others. The combination of two or several bands in a certain mathematical formula to feature an object is commonly called spectral indices.

One of the famous spectral indices is the Normalized Difference Vegetation Index(NDVI) which use Red(R) and Near InfraRed(NIR) band and has the formula as in equation 1. Applying the equation we will get an image as shown in figure 1. Vegetation will have higher value that is visualized in the red color, and non-vegetation will have low value in the black or other specified color.

\[NDVI=\frac{NIR-R}{NIR+R}\]equation 1. NDVI Formula

NDVI Index
Figure 1. NDVI Index

Calculate Spectral Indices in Google Earth Engine

In Google Earth Engine, we can calculate a spectral index using expression or normalizedDifference method in the image class. The code below shows how to calculate NDVI using both methods.
 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
//AREA OF INTERES POLYGON
var AOI = ee.Geometry.Polygon(
[[[23.88443106702291,12.621167961627908],
[24.91989249280416,12.621167961627908],
[24.91989249280416,13.407914064700716],
[23.88443106702291,13.407914064700716],
[23.88443106702291,12.621167961627908]]]);

//SENTINEL-2 DATASET
var dataset = ee.ImageCollection('COPERNICUS/S2_SR')
                  .filterDate('2021-06-01', '2021-12-30')
                  // Pre-filter to get less cloudy granules.
                  .filter(ee.Filter.lt('CLOUDY_PIXEL_PERCENTAGE',20))
                  .filter(ee.Filter.bounds(AOI))
                  .select('B.*');

//GET MEDIAN IMAGE AND CLIP TO AREA OF INTEREST
var image=dataset.median().clip(AOI);

//CALCULATE SPETRAL INDICES WITH normalizedDifference METHOD
var ndvi = image.normalizedDifference(['B8', 'B4']);

//CALCULATE SPETRAL INDICES WITH expression METHOD
var ndvi_ex=image.expression('(NIR-RED)/(NIR+RED)',{
  'NIR':image.select('B8'),
  'RED':image.select('B4')
})

//ADD TO MAP
Map.addLayer(ndvi,{min:0, max:1,palette:['black','red']},'ndvi')
Map.addLayer(ndvi_ex,{min:0, max:1,palette:['black','red']},'ndvi_ex')
Map.centerObject(AOI);

So far so good. We can calculate a spectral index in Google Earth Engine using expression or normalizedDifference method. But there are hundreds of spectral indices out there such as: Bare Soil Index (BI), Normalized Difference Water Index (NDWI), Normalized Built-up Area Index (NBAI), Enhanced Modified Bare Soil Index(EMBI),Burned Area Index(BAI), Enhanced Modified Bareness Index (EMBI) and many more. So how to calculate them quickly in Google Earth Engine?

Awesome Spectral Indices Library

We can calculate many spectral indices in Google Earth Engine using Awesome Spectral Indices. What is it? Awesome Spectral Indices is a curated list of spectral indices that are ready to use in GEE. What makes it awesome is that besides tracking hundreds of spectral indices, it also gives a scientific reference to an index, so we can learn more about it.

To use the awesome spectral indices library, we have to make a variable and link to the awesome spectral indices GEE asset with the following code.

var spectral = require("users/dmlmont/spectral:spectral");

Then the computation of spectral indices can be done with the computeIndex method. In it we have to define the name of spectral indices and specify all the required bands. The name of spectral indices and the required bands can be found in the documentation.

The code below is an example of how to calculate spectral indices using the awesome spectral indices library. In the code some spectral indices are calculated there are: NDVI, BI, NDWI and EMBI.

 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
//AREA OF INTERES POLYGON
var AOI = ee.Geometry.Polygon(
[[[-15.921106414005651,11.999679419588576],
[-15.802145079777135,11.999679419588576],
[-15.802145079777135,12.086139355465773],
[-15.921106414005651,12.086139355465773],
[-15.921106414005651,11.99967941958857]]]);

//SENTINEL-2 DATASET
var dataset = ee.ImageCollection('COPERNICUS/S2_SR')
                  .filterDate('2021-06-01', '2021-12-30')
                  // Pre-filter to get less cloudy granules.
                  .filter(ee.Filter.lt('CLOUDY_PIXEL_PERCENTAGE',20))
                  .filter(ee.Filter.bounds(AOI))
                  .select('B.*');
                  
//GET MEDIAN IMAGE AND CLIP TO AREA OF INTEREST
var image=dataset.median().clip(AOI);

//AWESOME SPECTRAL INDICES LIBRARY
var spectral = require("users/dmlmont/spectral:spectral");

//COMPUTE SPECTRAL INDICES
var spectral_indices=spectral.computeIndex(image,["NDVI","BI",'NDWI','EMBI'],
  {"N":image.select('B8'),
    "R":image.select('B4'),
    "G":image.select('B3'),
    "B":image.select('B2'),
    "S1":image.select('B11'),
    "S2":image.select("B12")
  });
  
//ADD TO MAP
Map.addLayer(spectral_indices.select('NDVI'),{min:0, max:1,palette: ['white', 'green']}, 'NDVI');
Map.addLayer(spectral_indices.select('BI'),{min:0, max:1,palette: ['white', 'brown']}, 'BI');
Map.addLayer(spectral_indices.select('NDWI'),{min:0, max:1,palette: ['white', 'cyan']}, 'NDWI');
Map.addLayer(spectral_indices.select('EMBI'),{min:0, max:1,palette: ['white', 'red']}, 'EMBI');
Map.centerObject(AOI);

After running the code, we will get all spectral indices as shown in the following figure.

Spectral Indices Result
Figure 2. Spectral Indices Result

That's all for this tutorial on how to calculate various spectral indices fast and quick in Google Earth Engine platform using Awesome Spectral Indices library. Now you can compute many spectral indices without bothering about the formula to be used in an image expression and you can also learn more about a spectral index with the provided reference. Anyway if you're new to Google Earth Engine, take a look this post to get first insight about GEE. Thanks for reading!

Related Posts

Disqus Comments