I calculated the NDWI with values greater than 0. On the other hand, I loaded a shape, like AOI, which contain diferents ponds if one with an id.
I would like to clip the NDWI imagecollection with the id of the AOI shape in order to know what NDWI is in each pond.
function maskS2clouds(image) {
var qa = image.select('QA60');
// Bits 10 and 11 are clouds and cirrus, respectively.
var cloudBitMask = 1 << 10;
var cirrusBitMask = 1 << 11;
// Both flags should be set to zero, indicating clear conditions.
var mask = qa.bitwiseAnd(cloudBitMask).eq(0)
.and(qa.bitwiseAnd(cirrusBitMask).eq(0));
return image.updateMask(mask).divide(10000);
}
// Image collection S2H.
var S2 = ee.ImageCollection('COPERNICUS/S2_HARMONIZED')
.filterDate('2025-08-04', '2025-08-04')
.filterBounds(AOI)
// Pre-filter to get less cloudy granules.
.filter(ee.Filter.lt('CLOUDY_PIXEL_PERCENTAGE', 20))
.map(maskS2clouds);
print('S2 ImageCollection', S2);
var Vis = {
min: 0.0,
max: 0.3,
bands: ['B4','B3', 'B2'],
};
Map.centerObject(AOI, 10);
Map.addLayer(S2.median(),Vis, 'RGB');
// --------------------------------------- //
// Calculate NDWI as a new ImageCollection (only values greater than 0)
// --------------------------------------- //
var addNDWI = S2.map(function(img) {
var NDWI = img.normalizedDifference(['B3', 'B8']).rename('NDWI')
var mask=NDWI.gt(0).selfMask()
return mask.copyProperties(img);
});
print(addNDWI, "NDWI");
//Visualize
Map.addLayer(addNDWI.first().clip(AOI),{palette:['blue']},'First Image NDWI Mask')
// Clip the image collection with the feature collection
?