TransWikia.com

Pass elements of list as function arguments to ee.Image.cat in Google Earth Engine

Geographic Information Systems Asked by cako on January 9, 2021

I would like to pass a list of images as arguments to the function ee.Image.cat.

Just like in this example, except not calling each element of list seperately with .get:

var list = ee.List([]);
var image1 = ee.Image(1)
var image2 = ee.Image(2)
list = list.add(image1);
list = list.add(image2)
var img = ee.Image.cat(list.get(0), list.get(1))
print(img) //Output: Image (2 bands)

I have tried .getInfo, .map, .apply and spread(...) without success and cannot find hints on any other way of doing it. I have read in the documentation for GEE that ES6 functionality is not supported currently, which explains why spread(...) does not work.

.getInfo() returns an error:

var img = ee.Image.cat(list.getInfo())
print(img) //Output: Unrecognized argument type to convert to an Image: [object Object]

.map returns (as expected) a list of the images:

var img = ee.Image.cat.map(list);
print (img) //Output: List (2 elements)

.apply returns an empty image:

var img = ee.Image.cat.apply(null, list); 
print(img) //Output: Image (0 bands)

spread(...) returns an error:

var img = ee.Image.cat(...list);
print(img) //Output: SyntaxError: Unexpected token

Does anyone know how to do this?

2 Answers

With or without ES6 support, JavaScript operations aren't going to be useful here because they are client-side — operating on elements of JavaScript arrays, not server-side ee.List elements — and you can't getInfo an image and send it back in a form that works for further computation so you can't combine getInfo and apply either. (You could if you were working with, say, numbers instead of images. But it still would have all the inefficiencies of multiple round-trips.)

The best solution is to avoid using ee.Image.cat at all. Instead, convert your list to an ImageCollection, then use ImageCollection.toBands.

var img = ee.ImageCollection.fromImages(list).toBands();

If you can avoid making a list in the first place and make an image collection instead, that may have even better performance, though the benefits for very large collections are moot here since putting many bands into one image is costly in the same ways constructing a list is.

Correct answer by Kevin Reid on January 9, 2021

Could iterate over list:

var img = list.iterate(function(image, stack){return ee.Image.cat(stack, image)}, ee.Image([]))

Answered by korndog on January 9, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP