Springe direkt zu Inhalt

NDVI - Classification and Area Calculation

As a final step for our monitoring analysis, we will want to classify our NDVI-Image and calculate the area, cover percentage and pixel count for each class.

In case the area calculations produce an error, simply run the script again to fix it. The supporting video is cut to skip processing times during area calculation.

Classification

The GEE allows to remap the NDVI-values into classes, so that we receive an Image with Integer-values ranging from 0 - n (n being the number of classes). This is done by setting threshold values and summing up all the values that are either less or greater than those thresholds. For the NDVI, we will remap our Image to 7 classes: values smaller than -0.2, 0, 0.1, 0.2, 0.3, 0.4 and 1.

var thresholds = ee.Image([-0.2, 0, 0.1, 0.2, 0.3, 0.4, 1]); //Define the thresholds
var classified = pre_event.gt(thresholds).reduce('sum').toInt(); //Create the classified Image
print(classified, 'Classified');

//Define new visualization parameters for the classification: The values are now ranging from 0 to 7, one for each class
var classifiedParams = {min: 0, max: 7, palette: ['blue', 'white', 'brown', 'burlywood', 'LimeGreen', 'ForestGreen', 'DarkGreen']};
Map.addLayer(classified.clip(extent_aoi), classifiedParams, 'Classified');

//Have a look at the classification in the map panel!

Area calculation per class

Now that we have classified our NDVI-Image, let us have a look at area statistics for the single classes.
The following approach is adapted from the UN Spider Burn Severity Mapping Recommended Practices. Make sure to have a look at it, as this is highly informative and really well written!
https://un-spider.org/advisory-support/recommended-practices/recommended-practice-burn-severity/burn-severity-earth-engine

Depending on the size of your AOI, the GEE might return the error 'Invalid JSON:' when trying to examine to calculated area statistics. Simply run the code again, and it should work fine.

// First, we want to count the number of pixels in the entire layer for future reference.
var allpix =  classified.updateMask(classified);  // mask the entire layer
var pixstats = allpix.reduceRegion({
  reducer: ee.Reducer.count(),               // count all pixels in a single class
  geometry: extent_aoi,
  scale: 30,
  maxPixels: 1e10
  });
var allpixels = ee.Number(pixstats.get('sum')); // extract pixel count as a number


// Then, we want to create an empty list to store the area values we will calculate in
var arealist = [];

// Now, we can create a function to derive the extent of one NDVI class
// The arguments are class number (cnr) and class name (name)
var areacount = function(cnr, name) {
 var singleMask =  classified.updateMask(classified.eq(cnr));  // mask a single class
 var stats = singleMask.reduceRegion({
  reducer: ee.Reducer.count(),               // count pixels in a single class
  geometry: extent_aoi,
  scale: 30,
  maxPixels: 1e10
  });
var pix =  ee.Number(stats.get('sum'));
var hect = pix.multiply(900).divide(10000);                // Landsat pixel = 30m x 30m --> 900 sqm
var perc = pix.divide(allpixels).multiply(10000).round().divide(100);   // get area percent by class and round to 2 decimals
arealist.push({Class: name, Pixels: pix, Hectares: hect, Percentage: perc});
};

// Create a list that contains the NDVI class names (7 classes, ranging from [-0.2, 0, 0.1, 0.2, 0.3, 0.4, 1])
var names2 = ['Water', 'No Vegetation', 'Very Low Vegetation',
'Low Vegetation', 'Moderate Vegetation','Moderate-high Vegetation', 'High Vegetation'];

// execute function for each class
for (var i = 0; i < 7; i++) {
  areacount(i, names2[i]);
  }

//Print the results to the Console and examine it.
print('Vegetated Area by NDVI Class', arealist, '--> click list objects for individual classes');