0

I'm using Google Earth Engine's JavaScript API to process Sentinel-2 images. I've calculated the EVI from the Sentinel ImageCollection, and would like to apply a mask that removes all the values outside the range of -1 to 1. However, I don't know how to create a mask for an ImageCollection rather than a single image.

My code so far is:

//make EVI expression into a function
var addEVI=function(image){
var EVI = image.expression(
      '2.5 * ((NIR - RED) / (NIR + 6 * RED - 7.5 * BLUE + 1))', {
      'NIR' : image.select('B8').divide(10000),
      'RED' : image.select('B4').divide(10000),
      'BLUE': image.select('B2').divide(10000)}).rename('EVI');
      return image.addBands(EVI);
};

//get full dataset
var collectionfull= ee.ImageCollection('COPERNICUS/S2')
            .filterBounds(studyarea)
                  // Pre-filter to get less cloudy granules.
                  .filter(ee.Filter.lt('CLOUDY_PIXEL_PERCENTAGE',50));

//map EVI across full dataset
var calculatedfull=collectionfull.map(addEVI);

//mapping selecting only EVI band across full dataset
var evifull =calculatedfull.select('EVI');

//applying mask (focus of question)
var maskposi = evifull.select('EVI').gt(1);
print('Mask positive', maskposi);
Map.addLayer(maskposi, 'mask positive');

This code, when run, produces the error evifull.select(...).gt is not a function.

How do I make a mask that will work for an ImageCollection?

1 Answer 1

1

You also map over the image collection. Just like with the addEVI function you have.

Basically if you know how to do the operation for a single Image, most of the time it is very easy to write a mapped function for an entire image collection.

For a single image you can apply a mask something like this, calling .mask() on the image and supplying it with a binary classification:

var test_image = ee.Image("COPERNICUS/S2_SR/20200102T111349_20200102T111435_T30UXB")

// Let's mask everything that is a cloud
var masked = test_image.mask(test_image.select("SCL").eq(9))

Now if we want to mask every Image in an Image Collection we use the same code but generalize it and put it into the function to map over:

var s2 = ee.ImageCollection("COPERNICUS/S2_SR").filterBounds(ee.Geometry.Point([-0.8, 50.9])).limit(10)

var s2_masked = s2.map(function(image){
  // Here comes the same code as for a single Image, 
  // but only replacing the specific variable `test_image` from before with the
  // general parameter `image` that we just specified as the parameter of
  // this function
  return image.mask(image.select("SCL").eq(9))
})

Map.addLayer(s2_masked)

Mapping a function over an image collection basically means "Apply those commands to every image in this collection"

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.