0

Consider the following code for Google Earth Engine:

var sample_list = ee.List([0, 1, 2, 3]);
var sample_bandNames = ee.List(['B1', 'B2', 'B3', 'B4']);

var sample_function = function(image) {
    var var1 = image.select('VAR1');
    var var2 = image.select('VAR2');

    for (var i = 0; i < sample_list.length; i++) {
        var temp = sample_list[i];

        var numer = temp.multiply(var1);
        var result = numer.divide(var2);

        image = image.addBands(result, [ee.String(sample_bandNames[i])]);
    }

    return image;
}

var img_collection = img_collection.map(sample_function);

I would expect the function to continuously add bands to every image of the image collection, but every time I print the image collection, the number of bands remains unchanged. How can I continuously add bands to each image of the image collection?

1 Answer 1

0

I can't believe the answer is simple.

The problem with my code is that the function never enters the loop because sample_list.length does not return a number, but rather a function, so no bands are added to the image.

I just replaced sample_list with [0, 1, 2, 3]--removing the ee.List()--and it worked!

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.