I'm trying to reclassify a NDWI raster into 1 for values >0.4 and 0 for <0.4. I understand that its possible to use .remap in case of discrete values, but not sure how to reclassify NDWI raster using a threshold. Ideally, I need a function which will reclassify each image in a collection.
Attached is the earth engine code I used (and struck)
// Import MODIS 8 day reflectance collection.
var collection = ee.ImageCollection('MODIS/006/MYD09A1')
.filterBounds(roi);
//roi refers to an area of interest
//start data and end date
var iniDate = ee.Date.fromYMD(2016,1,1);
var endDate = ee.Date.fromYMD(2017,12,31);
print('MODIS', collection);
// MODIS bands
var modisBands =
['sur_refl_b03','sur_refl_b04','sur_refl_b01','sur_refl_b02',
'sur_refl_b06', 'sur_refl_b07'];
var lsBands = ['blue','green','red','nir','swir1','swir2'];
// helper function to extract the QA bits
function getQABits(image, start, end, newName) {
// Compute the bits we need to extract.
var pattern = 0;
for (var i = start; i <= end; i++) {
pattern += Math.pow(2, i);
}
// Return a single band image of the extracted QA bits, giving the band
// a new name.
return image.select([0], [newName])
.bitwiseAnd(pattern)
.rightShift(start);
}
// A function to mask out cloudy pixels.
function maskQuality(image) {
// Select the QA band.
var QA = image.select('StateQA');
// Get the internal_cloud_algorithm_flag bit.
var internalQuality = getQABits(QA,8, 13, 'internal_quality_flag');
// Return an image masking out cloudy areas.
return image.updateMask(internalQuality.eq(0));
}
// create cloud free composite
var noCloud = collection.filterDate(iniDate,endDate)
.map(maskQuality)
.select(modisBands,lsBands);
// Function to calculate and add an NDWI band
var addNDWI = function(image) {
return image.addBands(image.normalizedDifference(['green', 'swir1']));
};
// Add NDWI band to image collection
var noCloud = noCloud.map(addNDWI);
// create NDWI image collection
var NDWI = noCloud.select(['nd']);
// Reclassify each image in NDWI collection and calculate yearly composite
Answer
What about values equal to 0.4?
.gt()
: greater than
.gte()
: greater or equal than
Then:
// Reclassify each image in NDWI collection and calculate yearly composite
var reclassified = NDWI.map(function(img){
return img.gte(0.4) // or gt()
})
No comments:
Post a Comment