I am trying to get the PCA using following codes for sentinel 2A in GEE, but I get the message-computation time out. Is there a way to solve the issue?

function maskS2clouds(image) {
  var qa = image.select('QA60');

  // Bits 10 and 11 are clouds and cirrus, respectively.
  var cloudBitMask = 1 << 10;
  var cirrusBitMask = 1 << 11;

  // Both flags should be set to zero, indicating clear conditions.
  var mask = qa.bitwiseAnd(cloudBitMask).eq(0)

  return image.updateMask(mask).divide(10000);

var dataset = ee.ImageCollection('COPERNICUS/S2_SR_HARMONIZED')
                  .filterDate('2022-1-01', '2023-01-30')

var s2a=dataset.median().clip(nepal);

var bands=['B2','B3','B4','B5','B8','B11'];

var arrayImage = s2a.select(bands).toArray();

var covar = arrayImage.reduceRegion({
  reducer: ee.Reducer.covariance(),
  maxPixels: 1e10,
  bestEffort: true


var covarArray = ee.Array(covar.get('array'));

var eigens = covarArray.eigen();

var eigenVectors = eigens.slice(1, 1);

var principalComponents = ee.Image(eigenVectors)

var pcImage = principalComponents
   .arrayFlatten([['pc1', 'pc2', 'pc3', 'pc4', 'pc5', 'pc6']]);

var pc1=pcImage.select('pc1');

1 Answer 1


This is a scale issue. So, by using an arbitrary area in Nepal (you don't provide one) as your roi and modifying following variable as follows:

var covar = arrayImage.reduceRegion({
  reducer: ee.Reducer.covariance(),
  maxPixels: 1e10,
  bestEffort: true


your code woks without any problem; as it can be observed in following picture:

enter image description here

  • For small area, it is fine, but for larger area like whole Nepal, same problem still exists using scale of 100. Shouldn't be scale the resolution of the sentinel image, rather than any other values.
    – atul
    Mar 15 at 17:10
  • Myself, I've never got that reduceRegion() in PCA to work at smaller scales for larger areas. You can get it working for a bit bigger areas by exporting the image, and perhaps setting tileScale to 16. But that too fails at some point. Mar 16 at 8:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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