1
    var mergedCollection = s2.map(function(image) {
    return image.select(['B8', 'B4'], ['NIR', 'red'])
    })


   var mergedAllFunction = function(image) {
   var ndvi = image.normalizedDifference(['NIR', 'red']).rename('NDVI');


   var thres1 = ndvi.gte(0).rename('thres1')
   var thres2=ndvi.gt(0.1) && ndvi.lt(0.3).rename('thres2')
   var thres3=ndvi.gt(0.4) && ndvi.lt(0.7).rename('thres3')
   var thres4=ndvi.gt(0.8) && ndvi.lte(1).rename('thres4')

   return image.addBands(ndvi).addBands(thres1,thres2,thres3,thres4);
   print(ndvi)
   }

How do I get area (count of pixels under each threshold)

2 Answers 2

2

There are a couple problems in your code. First, using && in that way will not work for reasons described here. Second, you can't addBands(arg1, arg2,...). Those args should be in a list. Here's a complete example that incorporates your code and a previous answer:

var mergedCollection = s2
    .filterDate('2018-01-01', '2018-03-01')
    .map(function(image) {
      return image.select(['B8', 'B4'], ['NIR', 'red'])
    })

var mergedAllFunction = function(image) {
 var ndvi = image.normalizedDifference(['NIR', 'red']).rename('NDVI');

 var thres1 = ndvi.gte(0).rename('thres1')
 var thres2=ndvi.gt(0.1).and(ndvi.lt(0.3)).rename('thres2')
 var thres3=ndvi.gt(0.4).and(ndvi.lt(0.7)).rename('thres3')
 var thres4=ndvi.gt(0.8).and(ndvi.lte(1)).rename('thres4')

 return image.addBands(ndvi).addBands([thres1,thres2,thres3,thres4]);
}

var median = mergedCollection.median();

var merged = mergedAllFunction(median);

var areas = merged
    .select(['thres1', 'thres2', 'thres3', 'thres4'])
    .multiply(ee.Image.pixelArea())
    .reduceRegion({
      reducer: ee.Reducer.sum(),
      geometry: geometry,  // a geometry
      scale: 10,   // scale = 10 for sentinel-2 'red' band
      maxPixels: 1e9  
    });
print(areas)
1
  • I tried this same code in Python API of Earth engine. It is showing the following error:EEException: Invalid argument for ee.Reducer(): ({'geometry': [[74.022029, 20.103245], [74.022029, 20.116564], [74.038048, 20.116564], [74.038048, 20.103245]], 'reducer': <ee.Reducer object at 0x03D06FF0>, 'scale': 10},). Must be a ComputedObject. ... Can you help me with this error. Feb 1, 2019 at 7:21
1

Calculate a dictionary of the summed area inside the per image function:

var areaThres1 = thres1.multiply(ee.Image.pixelArea()).reduceRegion({
    reducer: ee.Reducer.sum(),
    geometry: YOURAREAOFINTEREST,  // a geometry
    scale: 10,   // scale = 10 for sentinel-2 'red' band
    maxPixels: 1e9  
  });

Return the value as a property to each image:

return image.addBands(ndvi).addBands(thres1).addBands(thres2).addBands(thres3).addBands(thres4).set('area_sqm_th1', areaThres1);
5
  • I did not understand the per image function. Can you write a sample code in earth engine platform and share the link . Thanks for your help Jan 29, 2019 at 9:08
  • 1
  • Hi Kuik,thanks for prompt reply. I used your code bt I need the area calculated(according to threshold) to be printed in console. But when i use print option for the variable areathresh1. It is giving error.Please help in this regard Jan 29, 2019 at 10:02
  • I am getting this error: umber (Error) Failed to decode JSON. Error: Field 'value' of object '{"type":"ArgumentRef","value":null}' is missing or null. Object: {"type":"ArgumentRef","value":null}. Jan 29, 2019 at 10:04
  • 1
    You cannot print inside a mapped "server-side" function. Read the help pages to find out about that. use mergedAllFunction.first().get('area_sqm_th1') outside the mapped function.
    – Kuik
    Jan 29, 2019 at 12:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.