I computed the NDWI of the whole image collection and I expect to output an image that shows water body whenever it has been flooded , even once , throughout the filtered date.
Somehow the output image only show the result of the last image in the image collection.
This is what I currently have :
// Function to clip image collection
var clipimgcol = function(image){ return image.clip(Border)} ;
// ImageCollection
var l8collection = ee.ImageCollection('LANDSAT/LC08/C01/T1_SR')
.filterBounds(Border)
.filterDate('2014-02-05', '2018-02-05')
.sort('system:time_start')
.map(clipimgcol);
// Mask pixels with clouds and cloud shadows
// Surface reflectance products come with a 'pixel_qa' band based on CFMask
// Create function leaving Water and Clear
var maskCloudWater = function(image){
var pixel_qa = image.select('pixel_qa');
return image.updateMask(pixel_qa.eq(322).or(pixel_qa.eq(386)).or(pixel_qa.eq(834))
.or(pixel_qa.eq(898)).or(pixel_qa.eq(1346)) //pixel_qa = Clear
.or(pixel_qa.eq(324)).or(pixel_qa.eq(388)).or(pixel_qa.eq(836))
.or(pixel_qa.eq(900)).or(pixel_qa.eq(1348))); //pixel_qa = Water
};
// use "map" to apply the function to each image in the collection
var l8masked = l8collection.map(maskCloudWater);
// Compute the Normalized Difference Water Index (NDWI).
var getNDWI = function(img){
return img.addBands(img.normalizedDifference(['B3','B5']).rename('NDWI'));
};
// map over image collection
var l8ndwi = l8masked.map(getNDWI);
var ndwi = l8ndwi.select('NDWI');
print(ndwi);
I use a threshold of 0.3 for NDWI and tried to iterate the image collection so once the pixel's NDWI is gte 0.3 it returns 1 and the output will be the way I expected, but resulted differently :
var ini = ee.Image(ndwi.first());
function it (current , previousresult){
var step1 = current.gte(0.3);
var step2 = ee.Image(previousresult).select('NDWI');
var laststep = ee.Image.cat([step2 , step1]);
return laststep;
}
var ndwiit = ee.Image(ndwi.iterate(it , ini)).select('NDWI');
var ndwiViz = {min: -1, max: 1, palette: ['green', 'white','blue']};
Map.addLayer(ndwiit , ndwiViz, 'NDWIIT', false);
I also came up with the idea of remap function, to update the pixel's NDWI value once it is gte 0.3 but i didn't know how to extract band value of every pixel.