Monday, 18 November 2019

python - I have NDVI layer in Earth Engine. I want to assign and calculate area of good and bad ndvi area


    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)



Answer



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)

No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...