Can anyone explain why I am getting the memory limit error when trying to classify using the wekaKMeans algorithm over a cloud-free composite of Bangladesh? I'm trying to differentiate mangrove pixels.

I'm using a rectangle polygon that is the same size as the one used in the GEE tutorial (this one: https://developers.google.com/earth-engine/clustering) to try to avoid this as I assumed it would input the same amount of data into the algorithm.

// Specify feature collection by country
var region = ee.FeatureCollection('ft:1tdSwUL7MVpOauSgRzqVTOwdfy17KDbw-1d9omPw')
  .filterMetadata('Country', 'equals', 'Bangladesh');
var landsat8_collection = ee.ImageCollection('LANDSAT/LC08/C01/T1_TOA');

// Filter to the scenes that intersect your study region.
var landsat8_studyArea = landsat8_collection.filterBounds(region);
var landsat8_SA_2015 = landsat8_studyArea.filterDate('2015-01-01', '2015-12-31');

 // Get an image collection and center the display.
Map.addLayer(landsat8_SA_2015, { min: 0.05, max: 0.8, bands: ['B6', 'B5', 'B4'] }, 'Landsat Collection');
Map.centerObject(region, 7);

// Cloud mask function
// Specify the cloud likelihood threshold.
var cloud_thresh = 40;
// Create the cloud masking function.
var maskClouds = function(image){
   // Add the cloud likelihood band to the image.
   var cloudScore = ee.Algorithms.Landsat.simpleCloudScore(image);
   // Isolate the cloud likelihood band.
   var cloudLikelihood = cloudScore.select('cloud');
   // Compute a mask in which pixels below the threshold are 1.
   var cloudPixels = cloudLikelihood.lt(cloud_thresh);
   // Mask these pixels from the input image.
   // Return the masked input image.
   return image.updateMask(cloudPixels);

// Mask the clouds from all images in the image collection with the map function.
var landsat8_SA_2015NoClouds = landsat8_SA_2015.map(maskClouds);
// Center map.
Map.centerObject(region, 7);

// Median reducer and display cloud free image composite
// Reduce the collection to the median value per pixel.
var median_L8_2015 = landsat8_SA_2015NoClouds.median();
// Print the information of the reduced image.
print(median_L8_2015, 'median_L8_2015');
// Display reduced image in the map window.
 {min: 0.05, max: 0.8, bands: ['B6', 'B5', 'B4']},
 'median composite of cloud free images');

// Unsupervised clustering
// Make the training dataset.
var train = ee.Geometry.Rectangle(88.2, 22.9, 90.8, 21.2);
var training = median_L8_2015.sample({
  region: train,
  scale: 30,
  numPixels: 5000

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

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

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

Here is a link to my script: https://code.earthengine.google.com/0dc528e63b6f340baa82bef70ce63c92

1 Answer 1


If you get "User memory limit exceeded," try increasing tileScale in the sample() call as indicated in this doc.

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.