0

If I create an image, that I add properties to, I can no longer add the image to the Map. How can I add it, or how can I add properties so that it doesn't break?

var myPoint = ee.Geometry.Point([-64.9823, -36.4917]);

var myArea = myPoint.buffer(5000);

var myImage = ee.Image(1).clip(myArea);

Map.centerObject(myImage);
Map.addLayer(myImage); // This works fine.


var myImageWithProperties = myImage.setMulti({
  Name: "ones",
  ID: 1234
}); 

Map.addLayer(myImageWithProperties); // after adding properties
1

Some functions in Google Earth Engine make the object 'loose' its type. If you have a look at setMulti documentation, it says that returns an Element not an Image. So you have to tell Earth Engine what it is (this is called "cast a variable").

var myImageWithProperties = myImage.setMulti({
  Name: "ones",
  ID: 1234
});

// cast it
myImageWithProperties = ee.Image(myImageWithProperties)
  • So even if a function is under the Image section of the docs, it can make a an object 'lose' the type. Strangely, when I print it to the console, it still is an image type. But I made the suggested changes to the code, and it works. Thanks. – Sean Roulet Jan 21 at 12:40
  • I cannot know how Earth Engine works internally, but to me, what happens is that setMulti() is an ee.Element method, and that is why it returns an Element. ee.Image and ee.Feature are subclasses of ee.Element – Rodrigo E. Principe Jan 21 at 13:06
  • Understood. In any case, the solution is as you suggested, Casting it to ee.Image. – Sean Roulet Jan 21 at 14:15

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.