Skip to content
Advertisement

Reclassify NDVI raster in intervals on Google Earht Engine

Location:

var roi = /* color: #d63000 */ee.Geometry.Point([-71.97203347683796, -13.529827050320447]);

Collection:

var collection = ee.ImageCollection('COPERNICUS/S2') 
  .filter(ee.Filter.lt("CLOUDY_PIXEL_PERCENTAGE", 10)) 
  .filterDate('2018-01-1' ,'2018-12-31')
  .filterBounds(roi)

Calculte NDVI

function addNDVI(image) {
  var a = image.normalizedDifference(['B8', 'B4']);
  return image.addBands(a);
}
var ndvi2 = collection.map(addNDVI)
var ndvi2 = ndvi2.qualityMosaic('nd');

Now i want to reclass the NDVI raster in interal [-1-0.2], [0.2-0.4], [0.4-0.6], [0.6-0.8],[0.8-1],i try this code

var ndvireclass = ndvi2.select("nd").divide(10).ceil();
Map.addLayer(ndvireclass, {bands:'nd', min: 0, max: 1, gamma: 1.5}, 'NDVI reclass');

But the result image have only 2 clasess -1 and 1

Advertisement

Answer

There are multiple ways to do this, the way I prefer is to use a decision tree classifier. From your question, it seems like the ranges you want is less than 0.2, 02-0.4,0.4-0.6,0.6-0.8(missed in question maybe) and greater than 0.8. We need to construct a decision tree for this, use it to create a classifier and then apply it to the image.

var DTstring = ['1) root 9999 9999 9999',
'2) nd<=0.2 9999 9999 1 *',
'3) nd>0.2 9999 9999 9999',
'6) nd<=0.4 9999 9999 2 *',
'7) nd>0.4 9999 9999 9999',
'14) nd<=0.6 9999 9999 3 *',
'15) nd>0.6 9999 9999 9999',
'30) nd<=0.8 9999 9999 4 *',
'31) nd>0.8 9999 9999 5 *'].join("n");

var classifier = ee.Classifier.decisionTree(DTstring);
var reclassifiedImage = ndvi2.select('nd').classify(classifier);

You can see the working example here

OR You can also use logic operators to test the values in required range and then multiply by class numbers to get pixels for each class. for eg for class 2

var nd = ndvi2.select('nd');
var c2 = nd.gt(0.2).and(nd.lte(0.4)).multiply(2);

if you do similar for c1, c3, c4, c5 you should have rasters that only have pixels with values 0 and the class number. If you add all these layers you should get what you want

Advertisement