4

It is a very simple script, but I cannot figure it out.

The script is to create a list containing the constant images from 0 to 32, and then display them.

var test = ee.List.sequence(0,32).map(function(i){return ee.Image.constant(i); });
print(test);
Map.addLayer(ee.ImageCollection(test),{},'test');

The error is:

test: Tile error: Expected a homogeneous image collection, but an image with an incompatible band was encountered. Mismatched type for band 'constant':
Expected: Type<Float<0.0, 0.0>>.
  Actual: Type<Float<1.0, 1.0>>.
          Image ID: 1
This band might require an explicit cast.
6

ee.Image.constant() produces an image band whose pixel type holds exactly the number you gave it. An ImageCollection requires each image's bands to have the same types as the other images.

Therefore, you need to give the images a more general type that is the same for all of them, such as

return ee.Image.constant(i).toFloat();

to be simple, or

return ee.Image.constant(i).cast({'constant': ee.PixelType('int', 0, 31)});

to give it the type exactly wide enough.

3
  • Dear Reid, your solution absolutely works. But I woder why the .int() (or int8(), int16()... ) does not work, which cast the images to integer instead of float? – Nick Carraway Jul 13 '20 at 6:09
  • @NickCarraway I tried using ee.Image.constant(i).int() and it did not give an error in the example. What are you seeing? (If complex, this might be best posted as a separate question.) – Kevin Reid Jul 13 '20 at 15:21
  • Perhaps the error was caused by my typo, because it didn't give an error this time. Thanks for your answer. – Nick Carraway Jul 19 '20 at 8:48

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.