I am working with sentinel 2 images in Google earth engine and I have implemented the formula to calculate the EVI and I have added it as a band (in the collection). I know there is a way to implement a mask and show only those pixels with a certain value, but I'm interested to know if by implementing this mask (for example leaving only those pixels with EVI>0) there is a way to extrapolate it to the other bands . Specifically, I would like to calculate total reflectance (DN) in the image of all those pixels with EVI>0 in the NIR band. For this I would have to add the reflectance in the NIR band of all those pixels that have EVI>0 (for each image in the collection). It's possible?

 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);

var S2withEVI =S2.map(addEVI);

1 Answer 1


You can use the following function with 'where' method.

  var maskedNIR = S2withEVI.map(function (img) {

  var img_evi = img.select("EVI");
  var img_nir = img.select("B8");
  var new_nir = img_nir.where(img_evi.gte(0), img_nir)
                       .where(img_evi.lt(0), 0);
  new_nir = new_nir.updateMask(new_nir.neq(0));
  return new_nir.rename("new_NIR");

Complete code here.

After running link code in GEE code editor, I got result of following picture.

enter image description here

At the above picture, there are three layers: EVI, original B8 and masked B8 (new_NIR) bands. The original B8 band was unmarked for corroborating that new_NIR and EVI bands are both masked at the same areas. All B8 and masked B8 values are coincident in unmasked areas.

  • Thank you very much for your answer! That's to implement the mask, but I don't know how to work with the other bands yet. I want to calculate the total reflectance (sum of digital numbers) of all those pixels that are not masked (those with EVI>0)
    – Camila
    Commented Jan 30, 2023 at 19:41

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.