3

I want to do cloud masking for sentinel surface reflectance images but I cannot apply for multi-images,It was done for first image.

How could I do that using the Google Earth Engine JavaScript API?

var cld = require('users/fitoprincipe/geetools:cloud_masks')

var s2SR = ee.ImageCollection('COPERNICUS/S2_SR')
              //filter start and end date
             .filter(ee.Filter.calendarRange(2013,2021,'year'))
             .filter(ee.Filter.calendarRange(1,12,'month'))
             //filter according to drawn boundary
             .filterBounds(geometry)
             .filterMetadata('CLOUD_COVERAGE_ASSESSMENT', 'less_than',50);
//var first = ee.Image.constant(0) // init image

//var first = ee.Image(s2SR.get(0))
var test_image = s2SR.first()

Map.addLayer(test_image, {bands:['B4', 'B3', 'B2'], min:0, max:8000}, 'test image')

// adding mNDWI function
var addMNDWI = function(image) {
  var mndwi = ee.Image(image).normalizedDifference(['B3', 'B11']).rename('MNDWI');
  return ee.Image(image).addBands(mndwi);
};
 var mndwı=ee.ImageCollection(test_image)
             .map(addMNDWI);
 Map.addLayer(mndwı, {bands:['B4', 'B3', 'B2'], min:0, max:400}, 'mndwı')


var masked = cld.sclMask(['cloud_low', 'cloud_medium', 'cloud_high', 'shadow'])(test_image)
Map.addLayer(masked, {bands:['B4', 'B3', 'B2'], min:0, max:3000}, 'masked')
3

You can do that by mapping entire collection with function (s2SRMasked) of following script. There it was used an arbitrary point geometry because you didn't include one.

var geometry = ee.Geometry.Point([-109.647021484375, 39.40057454539126]);

var cld = require('users/fitoprincipe/geetools:cloud_masks');

var s2SR = ee.ImageCollection('COPERNICUS/S2_SR')
              //filter start and end date
             .filter(ee.Filter.calendarRange(2013,2021,'year'))
             .filter(ee.Filter.calendarRange(1,12,'month'))
             //filter according to drawn boundary
             .filterBounds(geometry)
             .filterMetadata('CLOUD_COVERAGE_ASSESSMENT', 'less_than',50);
//var first = ee.Image.constant(0) // init image

print("s2SR", s2SR);

//var first = ee.Image(s2SR.get(0))
var test_image = s2SR.first();

Map.addLayer(test_image, {bands:['B4', 'B3', 'B2'], min:0, max:8000}, 'test image');

// adding mNDWI function
var addMNDWI = function(image) {
  var mndwi = ee.Image(image).normalizedDifference(['B3', 'B11']).rename('MNDWI');
  return ee.Image(image).addBands(mndwi);
};
 var mndwı=ee.ImageCollection(test_image)
             .map(addMNDWI);
 Map.addLayer(mndwı, {bands:['B4', 'B3', 'B2'], min:0, max:400}, 'mndwı');


var masked = cld.sclMask(['cloud_low', 'cloud_medium', 'cloud_high', 'shadow'])(test_image);

print("masked", masked);
Map.addLayer(masked, {bands:['B4', 'B3', 'B2'], min:0, max:3000}, 'masked');

var s2SR_list = s2SR.toList(s2SR.size());

var s2SRMasked = s2SR_list.map(function (img) {
  
  return cld.sclMask(['cloud_low', 'cloud_medium', 'cloud_high', 'shadow'])(img);
  
});

print('s2SRMasked', s2SRMasked);
3
  • Couldn't I use this in certain polygon area,should it necessarily be a point?
    – Canan
    Apr 30 at 22:21
  • No, you can use your polygon area. In my case, I used a point because it was easier to put one and testing my approach (you didn't include your polygon geometry).
    – xunilk
    Apr 30 at 23:45
  • I mean,I want to use this code for all images on the black sea border,but I cant. I want to see all of images
    – Canan
    May 1 at 17:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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