2

I am trying to create a cloud free-composite that covers my ENTIRE study area using Sentinel-2 data. But, everytime I run this code, I get a Computation Timed out error. I am simply loading an image which is a composite of 10 images or lesser.

Link to code:(features can be accessed easily) https://code.earthengine.google.com/0d9ffb91268960807edc86bec867ee65

And code below

    //Set dates
var year = 2015;
var start_date = '-07-01';
var end_date = '-08-30';
var end_year = year+1;
print (year.toString() + start_date,end_year.toString() + end_date);

// Bits 10 and 11 are clouds and cirrus, respectively.
var cloudBitMask = ee.Number(2).pow(10).int();
print (cloudBitMask); //1024
var cirrusBitMask = ee.Number(2).pow(11).int();
print (cirrusBitMask); //2048 

var cloudmask = function (image) {
  var qa = image.select('QA60');
 // Both flags should be set to zero, indicating clear conditions.
  var mask = qa.bitwiseAnd(cloudBitMask).eq(0).and(
             qa.bitwiseAnd(cirrusBitMask).eq(0));
  return image.updateMask(mask);
};


// Create clipping function
var clipper = function(image){
  return image.clip(WG);
};

//// Application
var clipped = sentinel2.map(clipper);
var filtered = clipped.filterDate('2015-07-01','2018-08-30')
                      .filter(ee.Filter.lte('CLOUDY_PIXEL_PERCENTAGE',5))
                      .map(cloudmask)
                      .sort('system:time_start',false)
                      .limit(10);

// Create RGB composite. I use the median of the 10 most recent images
// to enhance the changes that really no clouds are present and the 
// complete ROI is covered.
var median = filtered.reduce(ee.Reducer.median())
                   .select(['B4_median','B3_median','B2_median']);
Map.addLayer(median);

Any suggestions / solutions?

Before you berate my post, YES, I did take a look at the solution here and it did not help - How do I create a Sentinel-2 cloud free, cloud-shadow free composite or scene on Earth Engine (GEE)?

1

Your problem most probably lies with the size of what you are asking Earth Engine to do.

If you look at the order.

var filtered = clipped.filterDate('2015-07-01','2018-08-30')
                  .filter(ee.Filter.lte('CLOUDY_PIXEL_PERCENTAGE',5))
                  .map(cloudmask) // do this after you limit and it will be faster
                  .sort('system:time_start',false)
                  .limit(10);

You are asking it to map "cloudmask" onto every image in your collection. If you change the order of the application, you will only be mapping 10 images, and that should go quite fast. The order in which you do things to a collection matters.

The main issue has to do with the timestamp. system:time_start. First you have to turn that into a date.

change your code like this

//// Application
var clipped = sentinel2.map(clipper);
var filtered = clipped.filterDate('2015-07-01','2018-08-30')
                  .filter(ee.Filter.lte('CLOUDY_PIXEL_PERCENTAGE',5))

// Add the date to the collection
  var addDate = function(image) {
  var date = ee.Date(image.get('system:time_start')).format("YYYY-MM-dd");
   date = ee.Date(date);
    return image.set('date', date);
  } // This function was copied from Phillip Gaertner's GitHub page

var withDate = filtered.map(addDate);

var sorted = withDate.sort('date', false)

var limitedTo10 = sorted.limit(10)

var masked = limitedTo10.map(cloudmask)




// Create RGB composite. I use the median of the 10 most recent images
// to enhance the changes that really no clouds are present and the 
// complete ROI is covered.
var median = masked.reduce(ee.Reducer.median())
               .select(['B4_median','B3_median','B2_median']);
// Added visualization parameters so that you can see it better
var imageVisParam = {opacity:1,
              bands:["B4_median","B3_median","B2_median"],
              min:396,
              max:1632.5,
              gamma:0.864}

Map.addLayer(median,imageVisParam);
  • Thanks a ton Sean! that works. I think you might want to edit the last few lines to add var in front of imageVisParam – Vijay Ramesh Feb 11 at 20:29
  • @SeanRoulet- shouldn't it be var median = masked.reduce..... since you want to find the median reflectance values of the 10 least cloudy and masked image collection (previous few lines) – tg110 Feb 16 at 21:30
  • @tg110 you are correct. I will edit accordingly. – Sean Roulet Feb 18 at 15:15
  • @SeanRoulet I used your corrected version, but I see certain holes in the median where the data is not being displayed. Any suggestions on correcting the same? – Vijay Ramesh Feb 18 at 21:49
  • 1
    @Vijay Ramesh, The problem lies with the limit to 10. If you add these lines to the APP section. var bounds = sentinel2.filterBounds(WG) var clipped = bounds.map(clipper);. And modify the masking line to do it to the sorted list, not just the limit(10) it should work. var masked = sorted.map(cloudmask) This limits the original collection form nearly 2 million images to just 188. and then the application of the reductions (median and mask) can be done to all 188 images, with no overhead. – Sean Roulet Feb 19 at 15:56

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.