0

I'm trying to create a list of images in Google Earth Engine. These images are generated by a function. The input to this function is a number. Here's a simplified version of the problem I'm encountering:

I have the following function that multiplies a number by an image, and returns an image as a result:

function multiply_by_2(num) {
  // Multiply the number by an image with the value 2
  var image1 = ee.Image(2);
  // Convert num to an image for multiplication
  var image2 = ee.Image(num);
  // Multiply the two images together
  var result = image1.multiply(image2);
  return result;
}

If I call the function with a single number, it behaves as expected:

var number = ee.Number(5);
var multiplied_number = multiply_by_2(number);
print(multiplied_number);

Result in console:

An image with one band and the value 10

But if I try to use map, which should apply this function to every number in a list, I get an error:

var number_list = ee.List.sequence(1,10);
var multiplied_list = number_list.map(multiply_by_2);
print(multiplied_list)

Result in console:

List(Error): Image.multiply, argument 'image2': Invalid Type. Expected Type: Image. Actual type: Float. Actual Value: 1.02

The error complains that Image.multiply has been given a floating point number instead of an image. But in my function, I'm giving Image.multiply an image! What's going on here?

1 Answer 1

1

To resolve the invalid type error in your code, you need to ensure that the input value num is converted to an ee.Number. Once this conversion is done, the map will work correctly.

function multiply_by_2(num) {
  // Cast the input to an ee.Number
  num = ee.Number(num);
  // Multiply the number by an image with the value 2
  var image1 = ee.Image(2);
  // Convert num to an image for multiplication
  var image2 = ee.Image(num);
  // Multiply the two images together
  var result = image1.multiply(image2);
  return result;
}

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.