0

I've had this error in Google Earth Engine trying to correct for sunglint in order to remote sense seagrass.

The error reads as such:

Sunglint Corrected: Layer error: Image.reduceRegion: Too many pixels in the region. Found 2048511108, but maxPixels allows only 10000000. Ensure that you are not aggregating at a higher resolution than you intended; that is a frequent cause of this error. If not, then you may set the 'maxPixels' argument to a limit suitable for your computation; set 'bestEffort' to true to aggregate at whatever scale results in 'maxPixels' total pixels; or both

I've tried changing maxpixels, changing best effort to true, but so far I've had no luck.

Here is my code. Here is also a link to it: https://code.earthengine.google.com/72ce8a26fa1d08d3f2c58d5da89e46d6

   var dataset = ee.ImageCollection('COPERNICUS/S2_SR')
                  .filterDate('2021-02-12', '2021-07-25')
                  
                  .filter(ee.Filter.lt('CLOUDY_PIXEL_PERCENTAGE',20))
                  .filterBounds(roi)
                  
                  
                  .select(['B2','B3','B4','B1', 'B8']);
                   
                  
                   
                   
                  
                  
var rgbVis = {
 min: 0.0,
 max: 1000,
 bands: ['B3', 'B2', 'B4'],
};

Map.addLayer(dataset, rgbVis, 'Filtered Collection'); 
var mosaic = dataset.mosaic();


var medianComposite = dataset.min(); 
Map.addLayer(medianComposite, rgbVis, 'Median Composite', 0);

var hansenImage = ee.Image('UMD/hansen/global_forest_change_2015');
var datamask = hansenImage.select('datamask');
var mask = datamask.eq(2);
var maskedComposite = medianComposite.updateMask(mask);
Map.addLayer(maskedComposite, rgbVis, 'masked');

Map.addLayer(maskedComposite, {
  bands: ['B4', 'B3', 'B2',],
  min: 0,
  max: 1000,
  scale:16,
}, 'BOA'); 

var B2 = maskedComposite.select(['B8', 'B2']);
var B3 = maskedComposite.select(['B8', 'B3']);
var B4 = maskedComposite.select(['B8', 'B4']);

var lfitB2 = B2.reduceRegion({
  reducer: ee.Reducer.linearFit(),
  geometry: glint,
  scale: 5,
  tileScale: 16, 
  maxPixels: 40e9,
  bestEffort: true
});

var lfitB3 = B3.reduceRegion({
  reducer: ee.Reducer.linearFit(),
  geometry: glint,
  scale: 5,
  tileScale: 16, 
  maxPixels: 40e9,
  bestEffort: true, 
});

var lfitB4 = B4.reduceRegion({
  reducer: ee.Reducer.linearFit(),
  geometry: glint,
  scale: 5,
  tileScale: 16, 
  maxPixels: 40e9,
  bestEffort: true, 
});

print(lfitB4)

//print('B2 OLS estimates:', lfitB2);
//print('B2 y-intercept:', lfitB2.get('offset'));
//print('B2 Slope:', lfitB2.get('scale'));
//print('B3 Slope:', lfitB3.get('scale'));
//print('B4 Slope:', lfitB4.get('scale'));

var slope_B2 = ee.Image.constant(lfitB2.get('scale')).clip(roi).rename('slope_B2');
var slope_B3 = ee.Image.constant(lfitB3.get('scale')).clip(roi).rename('slope_B3');
var slope_B4 = ee.Image.constant(lfitB4.get('scale')).clip(roi).rename('slope_B4');
var min_B8 = ee.Image.constant(maskedComposite.select('B8').reduceRegion(ee.Reducer.min(),roi, 3).get('B8')).rename('min_B8');

var glint_factors = ee.Image([slope_B2, slope_B3, slope_B4, min_B8]);
var S2 = maskedComposite.addBands(glint_factors);

/*var deglint_B2 = S2.select('B8').subtract(min_B8);
var deglint_B2 = slope_B2.multiply(deglint_B2);
var deglint_B2 = S2.select('B2').subtract(deglint_B2);
Map.addLayer(deglint_B2);*/

var deglint_B2 = S2.expression(
    'Blue - (Slope * (NIR - MinNIR))', {
    'Blue': S2.select('B2'),
    'NIR': S2.select('B8'),
    'MinNIR': S2.select('min_B8'),
    'Slope': S2.select('slope_B2')
}).rename('B2');

var deglint_B3 = S2.expression(
    'Green - (Slope * (NIR - MinNIR))', {
    'Green': S2.select('B3'),
    'NIR': S2.select('B8'),
    'MinNIR': S2.select('min_B8'),
    'Slope': S2.select('slope_B3')
}).rename('B3');

var deglint_B4 = S2.expression(
    'Red - (Slope * (NIR - MinNIR))', {
    'Red': S2.select('B4'),
    'NIR': S2.select('B8'),
    'MinNIR': S2.select('min_B8'),
    'Slope': S2.select('slope_B4')
}).rename('B4');

var S2_deglint = ee.Image([deglint_B2, deglint_B3, deglint_B4]);

Map.addLayer(S2_deglint, {
  bands: ['B4', 'B3', 'B2'],
  min: 0.0,
  max: 40e9,
  scale: 5, 
  tileScale: 16,
  bestEffort: true
}, 'Sunglint Corrected');

 var linkedMap = ui.Map();

Map.addLayer(S2, {
bands: ['B4', 'B3', 'B2'],
min: 0.0,
max: 40e9, 
}, 'Top-of-Atmosphere Reflectance');

Map.addLayer(S2_deglint, {bands: ['B4', 'B3', 'B2'], min: 0.0, max: 0.2, scale: 16}, 'Sunglint Corrected');

var linker = ui.Map.Linker([ui.root.widgets().get(0), linkedMap]);

var b2b3 = S2_deglint.select(['B2', 'B3']);
var b2b4 = S2_deglint.select(['B2', 'B4']);
var b3b4 = S2_deglint.select(['B3', 'B4']);

var lfitb2b3 = b2b3.reduceRegion({
  reducer: ee.Reducer.linearFit(),
  geometry: sand,
  scale: 16,
  bestEffort: true
});

var lfitb2b4 = b2b4.reduceRegion({
  reducer: ee.Reducer.linearFit(),
  geometry: sand,
  scale: 16,
});

var lfitb3b4 = b3b4.reduceRegion({
  reducer: ee.Reducer.linearFit(),
  geometry: sand,
  scale: 16,
});


var slope_b2b3 = ee.Image.constant(lfitb2b3.get('scale')).clip(roi).rename('slope_b2b3');
var slope_b2b4 = ee.Image.constant(lfitb2b4.get('scale')).clip(roi).rename('slope_b2b4');
var slope_b3b4 = ee.Image.constant(lfitb3b4.get('scale')).clip(roi).rename('slope_b3b4');

var dii_slopes = ee.Image([slope_b2b3, slope_b2b4, slope_b3b4]);
var S2_deglint = S2_deglint.addBands(dii_slopes);

var dii_b2b3 = S2_deglint.expression(
    'log(b2) - abs(slope * log(b3))', {
    'b2': S2_deglint.select('B2'),
    'b3': S2_deglint.select('B3'),
    'slope': S2_deglint.select('slope_b2b3')
}).rename('DII_b2b3');

var dii_b2b4 = S2_deglint.expression(
    'log(b2) - abs(slope * log(b4))', {
    'b2': S2_deglint.select('B2'),
    'b4': S2_deglint.select('B4'),
    'slope': S2_deglint.select('slope_b2b4')
}).rename('DII_b2b4');

var dii_b3b4 = S2_deglint.expression(
    'log(b3) - abs(slope * log(b4))', {
    'b3': S2_deglint.select('B3'),
    'b4': S2_deglint.select('B4'),
    'slope': S2_deglint.select('slope_b3b4')
}).rename('DII_b3b4');

var DII = ee.Image([dii_b2b3, dii_b2b4, dii_b3b4])

Map.addLayer(DII, {
  bands: ['DII_b2b4', 'DII_b3b4', 'DII_b2b3'],
  min: -7,
  max: -3
}, 'Depth-Invariant Index');

var training = maskedComposite.sample({
  region: roi,
  scale: 5,
  tileScale:   16, 
  numPixels: 3000
});

// Instantiate the clusterer and train it.
var clusterer = ee.Clusterer.wekaKMeans(5).train(training);

// Cluster the input using the trained clusterer.
var result = maskedComposite.cluster(clusterer);


// Display the clusters with random colors.
Map.addLayer(result.randomVisualizer(), {}, 'clusters'); 

1 Answer 1

0

The problem here is with the scale you are using, i.e. 5 meters. This is the scale at which you are aggregating your images in order to get mean values, and is completely different from tileScale which can range between 1 and 16 approx. In GEE terms a 5m aggregation is impossibly small, and useless considering the datasets you are using have a resolution of 10m at best. Also, consider that your ROI is very large. You need to change your scale to something more realistic like 1000 meters, then decrease it gradually to see if you can go finer.

As a side note, maxPixels cannot go higher than 1e13.

2
  • Ok thanks! I tried shrinking the roi. Now I get a different error: Sunglint Corrected: Layer error: Image.constant: Parameter 'value' is required. Commented Jun 16, 2022 at 12:36
  • That's a different question, please make a new post.
    – M. Nicolas
    Commented Jun 16, 2022 at 16:03

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.