-1

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
?

1 Answer 1

0

To know your NDWI in each pond you don't necessarily have to clip your images, and usually clipping is not recommended except in specific circumstances like before exporting.

Instead, I'd recommend using reduceRegions which will return a mean value for each polygon in your AOI (make sure each polygon is a separate feature). Keep in mind reduceRegions works works only on individual images, not imageCollections, so you will have to decide if you want to reduce your imageCollection first (using .mean() for example) or if you want to map over each image. Below is an example:

var addNDWIReduced = addNDWI.mean().reduceRegions({
  collection: finalAOI,
  reducer: ee.Reducer.mean(),
  scale: 100
})
print('addNDWIReduced: ',addNDWIReduced)

If you decide nonetheless that you want to clip each image in the collection, you can use a map:

var addNDWIClipped = addNDWI.map(function(image){
  return image.clip(AOI)
})
print('addNDWIClipped: ',addNDWIClipped)

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.