Google Earth Engine Sentinel-2 Masking – Using SCL Product for Masking

google-earth-enginegoogle-earth-engine-javascript-apimaskingsentinel-2

I need to create a Google Earth Engine (JavaScript API) function to mask the pixels corresponding to no_data, satured_or_defective, dark_area_pixels, cloud_shadows, unclassified, cloud_medium_probability, cloud_high_probability and thin_cirrus in Sentinel-2 L2A images according to the SCL product.

SCL classification.

My intuition tells me that the function would start like this:

function s2_clear_sky(image){
  // 1.Locate SCL product
  var scl = image.select('SCL');
  // 2.SCL classification
  var no_data = scl.eq(0);
  var satured_or_defective = scl.eq(1);
  var dark_area_pixels = scl.eq(2);
  var cloud_shadows = scl.eq(3);
  var vegetation = scl.eq(4);
  var not_vegetated = scl.eq(5);
  var water = scl.eq(6);
  var unclassified = scl.eq(7),
  var cloud_medium_probability = scl.eq(8);
  var cloud_high_probability = scl.eq(9);
  var thin_cirrus = scl.eq(10);
  var snow = scl.eq(11);
  // 3.Apply mask to the image
  ¿¿¿???
  return image.updateMask(¿¿¿???)
}

Finally this function would be applied to the entire ImageCollection using the .map() function:

var s2_masked = s2_collection.map(s2_clear_sky)

How can I implement this function?

Best Answer

You just need to indicate which values from the SCL band you are interested in and the apply that mask to each image. Although you can indicate the values one by one, it is easier to use and and or with lt (less than) or gt (greater than) operators to select the desired SCL values.

var s2_clear_sky = function(image){
  // 1.Locate SCL product
  var scl = image.select('SCL');
  
  // 2.Apply mask to the image
  // Select values: 1) greater than 3 and less than 7 or 2) equal to 11
  var wantedPixels = scl.gt(3).and(scl.lt(7))
                              .or(scl.eq(11));
  return image.updateMask(wantedPixels);
};