colab Open in SageMaker Studio Lab Open in Planetary Computer

Computing Spectral Indices in Landsat 8

Tutorial created by **David Montero Loaiza**: GitHub | Twitter

Let’s start!

If required, please uncomment:

[1]:
#!pip install eemont
#!pip install geemap

Import the required packages.

[2]:
import ee, eemont, datetime, geemap

Authenticate and Initialize Earth Engine and geemap.

[3]:
Map = geemap.Map()

Point of interest.

[4]:
point = ee.Geometry.Point([-74.0592,11.3172])

Get, filter, mask clouds and scale the image collection.

[5]:
L8 = (ee.ImageCollection('LANDSAT/LC08/C01/T1_SR')
      .filterBounds(point)
      .sort('CLOUD_COVER')
      .first()
      .maskClouds()
      .scaleAndOffset())

Spectral Indices

Spectral indices can be computed for Sentinel-2 and Landsat Products using the spectralIndices() method.

[6]:
L8 = L8.spectralIndices()

By default, the NDVI is computed and added as a new band.

[7]:
L8.bandNames().getInfo()
[7]:
['B1',
 'B2',
 'B3',
 'B4',
 'B5',
 'B6',
 'B7',
 'B10',
 'B11',
 'sr_aerosol',
 'pixel_qa',
 'radsat_qa',
 'NDVI']

If required, another index can be computed:

[8]:
L8 = L8.spectralIndices('BAI')

This new index is now added as another new band.

[9]:
L8.bandNames().getInfo()
[9]:
['B1',
 'B2',
 'B3',
 'B4',
 'B5',
 'B6',
 'B7',
 'B10',
 'B11',
 'sr_aerosol',
 'pixel_qa',
 'radsat_qa',
 'NDVI',
 'BAI']

Multiple indices can also be computed.

[10]:
L8 = L8.spectralIndices(['EVI','GNDVI','RVI'])

These indices are now added as new bands:

[11]:
L8.bandNames().getInfo()
[11]:
['B1',
 'B2',
 'B3',
 'B4',
 'B5',
 'B6',
 'B7',
 'B10',
 'B11',
 'sr_aerosol',
 'pixel_qa',
 'radsat_qa',
 'NDVI',
 'BAI',
 'EVI',
 'GNDVI',
 'RVI']

Group of indices can also be added by the group name. The available options are: vegetation, water, burn, snow or all (compute all spectral indices):

[12]:
L8 = L8.spectralIndices('water') # Computes NDWI and MNDWI

The computed indices are now added as new bands:

[13]:
L8.bandNames().getInfo()
[13]:
['B1',
 'B2',
 'B3',
 'B4',
 'B5',
 'B6',
 'B7',
 'B10',
 'B11',
 'sr_aerosol',
 'pixel_qa',
 'radsat_qa',
 'NDVI',
 'BAI',
 'EVI',
 'GNDVI',
 'RVI',
 'AWEInsh',
 'AWEIsh',
 'MNDWI',
 'NDWI',
 'WI1',
 'WI2']

Visualization

Now, let’s plot two of the computed indices: GNDVI and NDWI.

Let’s define the visualization parameters for the GNDVI:

[14]:
visParamsGNDVI = {
  'min': 0.0,
  'max': 1.0,
  'palette': [
    'FFFFFF', 'CE7E45', 'DF923D', 'F1B555', 'FCD163', '99B718', '74A901',
    '66A000', '529400', '3E8601', '207401', '056201', '004C00', '023B01',
    '012E01', '011D01', '011301'
  ],
}

Let’s define the visualization parameters for the NDWI:

[15]:
visParamsNDWI = {
  'min': 0.0,
  'max': 1.0,
  'palette': ['0000ff', '00ffff', 'ffff00', 'ff0000', 'ffffff'],
}

Use geemap to display results:

[16]:
Map.centerObject(point,9)
Map.addLayer(L8.select('GNDVI'),visParamsGNDVI,'GNDVI')
Map.addLayer(L8.select('NDWI'),visParamsNDWI,'NDWI')
Map.add_colorbar(visParamsGNDVI['palette'], caption = 'GNDVI')
Map.add_colorbar(visParamsNDWI['palette'], caption = 'NDWI')
Map