I have extracted a list of images from an iteration (using Google Earth Engine how can I return an image for each iteration) but I keep getting an issue when I try to add these images to an image collection.

var collection = ee.ImageCollection(list);

Using this line of code I was able to add the images to a collection, but when I try to make calculations with this collection (collection.max()) an error occurs:

Error message

The entire piece of code:

function drylength(current, previous){   
  previous = ee.List(previous);
  var mask = current.remap([0], [1], 0,"precipitation").rename('precipitation');
  var sum = mask.add(previous.get(-1)).multiply(mask);

  return previous.add(sum); }

var dataset = TRMM .filterDate("2015-01-01","2015-02-01") .sort('system:time_start:');

var list = ee.List(dataset.iterate(drylength, ee.List([   dataset.max()
    .remap([0], [0], 0, "precipitation")
    .rename('precipitation') ])));

var collection = ee.ImageCollection(list);


Map.addLayer(collection, imageVisParam, "max");

1 Answer 1


Earth Engine images and image collections keep track of the numeric range of values that occur in each band. When you create an image collection from multiple images, they must all be consistent, but in your case the images have all different tightly-bounded ranges as the accumulation proceeds.

To avoid this problem, you need to cast the bands of each image to the same range before putting them in the collection. Since you're using iterate to generate cumulative results, the easiest way to do this is to change the type of the initial image you pass to iterate to a wide numeric type:

var list = ee.List(dataset.iterate(drylength, ee.List([
    .remap([0], [0], 0, "precipitation")
    .cast({'precipitation': 'long'})

However, while I'm looking, I'll also note that dataset.max().remap([0], [0], 0, "precipitation") just gives you an image with the value 0, unmasked wherever the collection has images. If that's what you want, there's no reason to calculate .max(), and you could instead use, for example,

var list = ee.List(dataset.iterate(drylength, ee.List([
    .cast({'precipitation': 'long'})

which, in my opinion, is slightly more obviously just zero, if a bit verbose.

  • That fixes it indeed. I thought something like that was the problem but I couldn't seem to find the .cast() function in the docs. Same with the ee.Image.constant(), this really makes it a lot neater! thanks
    – Sam
    Mar 20, 2018 at 10:19
  • Excellent explanation Kevin. Nov 10, 2021 at 12:14

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.