3

I am trying to filter out the least cloudy Sentinal 2 images and display them on as per the AOI boundary. I am working on the following code, but it is not filtering out the least cloudy images or any method to remove the cloud from images. I had gone through several posts but not found any solution yet.

var shp=ee.FeatureCollection('users/Desktop/AOI');
Map.addLayer(shp);
var sentinel=ee.ImageCollection('COPERNICUS/S2_SR').filterDate('2019-12-10','2019-12-30')
.select(['B8','B4','B3','B2']).filterBounds(shp).sort('CLOUDY_PIXEL_PERCENTAGE', false);
var s2a=sentinel.mosaic().clip(shp);
print(sentinel);
//var clipped = sentinel.map(cloudmask).map(clipper);
//var leastCloudy = ee.Image(sentinel.sort('CLOUDY_PIXEL_PERCENTAGE').first());
Map.addLayer(s2a,{min:0, max:4000}, 'Output');

1 Answer 1

3

I don't have your ROI, but I made another to make your code reproducible. Rather than using the sort() function, try instead setting a threshold percent cloud cover that you're happy with and remove all imagery that has too much cloud cover:

var Sample = ee.Geometry.Polygon([
  [[-119.22, 38.06], [-119.23, 37.97], [-119.01, 37.91], 
  [-118.89, 37.93], [-118.85, 38.02], [-119.01, 38.09],
  [-119.22, 38.06]]
]);

// Generate Region of Interest
var shp = ee.FeatureCollection(Sample);

Map.addLayer(shp);
var sentinel = ee.ImageCollection('COPERNICUS/S2_SR')
  .filterDate('2019-12-10','2019-12-30')
  .select(['B8','B4','B3','B2'])
  .filterBounds(shp)
  .filter(ee.Filter.lt('CLOUDY_PIXEL_PERCENTAGE', 20));

var s2a = sentinel.mosaic().clip(shp);
print(sentinel);
//var clipped = sentinel.map(cloudmask).map(clipper);
//var leastCloudy = ee.Image(sentinel.sort('CLOUDY_PIXEL_PERCENTAGE').first());
Map.addLayer(s2a,{min:0, max:4000}, 'Output');
1
  • Thank you. Perfectly working
    – SWAT
    Commented May 27, 2020 at 16:38

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.