0

i get the following Error when running the code: ImageCollection (Error) User memory limit exceeded.

I want to calculate the difference between two images in an image Collection. Can anybody help me? Here is my code:

// Load Sentinel-2 TOA reflectance data.
var Sentinel2 = ee.ImageCollection('COPERNICUS/S2')
              .filterDate('2018-01-01', '2018-12-31')
              // Pre-filter to get less cloudy granules.
              .filter(ee.Filter.lt('CLOUDY_PIXEL_PERCENTAGE', 20))
              .map(maskS2clouds)
              .filterBounds(Greece);

function addNBR(image) {
// Compute the Normalized Difference Vegetation Index (NDVI).
var nir = image.select('B8A');
var swir = image.select('B12');
var nbr = nir.subtract(swir).divide(nir.add(swir)).rename('NBR');
return image.addBands(nbr)
}
var Sentinel2_collection = Sentinel2.map(addNBR)

print ('Sentinel2_collection: ', Sentinel2_collection)

var NBR = Sentinel2_collection.select('NBR').sort('system:time_start');

print ('NBR is ', NBR)

var NBRlist = ee.List(NBR.aggregate_array('system:time_start'))
print ('NBRlist ', NBRlist)

var NBRdiff = ee.ImageCollection.fromImages(NBRlist.map(function(date){

  // get the image corresponding to the date
  var currentImage = NBR.filter(ee.Filter.eq('system:time_start',    date)).first();

 // Now we have to get the 'previous' image in the collection
  var indexCurrent = NBRlist.indexOf(date);
  var indexPrevious = indexCurrent.subtract(1);
  var datePrevious = NBRlist.get(indexPrevious);
  var previousImage = NBR.filter(ee.Filter.eq('system:time_start',    datePrevious)).first();

  // Subtract the current image from the previous
  var diffImage = currentImage.subtract(previousImage).select(['NBR'], ['NBRdiff']);

 // return the image
  return currentImage.addBands(diffImage)
        .set('system:index_previous', previousImage.get('system:index'));
}));

// print and add to the map
print('Difference Image Collection', NBRdiff);

1 Answer 1

0

use tileScale in reduceRegion() method to counter this error. Setting a tile scale to parallelize the computation so overcoming memory exceed error.

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.