1

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

AnesG
  • 275
  • 2
  • 17

1 Answers1

1

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

Nishanta Khanal
  • 316
  • 2
  • 4
  • Would you mind explaining a lite more how the decision tree works?, in case I want to change the intervals. – AnesG Jul 17 '19 at 19:39
  • 1
    You can learn about the decision tree itself [here](https://www.datacamp.com/community/tutorials/decision-trees-R). As for the string itself, I can't find a good source to explain that so i'll try my best to explain that here. To start with the decision tree string has multiple lines and on each line a decision node is specified. Each line has 5 sections for eg. *1)*, *root* , *9999*,*9999*,*9999*. Here *1)* is the node number and it connects forward to 2n, 2n+1 th node. For eg 1 goes to 2 and 3, 3 goes to 6 and 7 etc. *root* or *nd<=0.2* here is the condition to check. – Nishanta Khanal Jul 18 '19 at 03:25
  • 1
    In case of our images it should be the band name as it checks for value in that band. *9999* are placeholders and the one at end specifies the value to assign. Like 1. The asterisk means no further propagation from that node. To change thresholds you can just change the values in the condition and its similar to add a line but you have to consider the line/node number while doing so. – Nishanta Khanal Jul 18 '19 at 03:27