1

I want to use a list (of image names) as input to map, and obtain a list of images. Curiously, this works when the input is a (JavaScript) list, but not when it is an ee.List()!? In other words:

  • ee.List(inputs).map(function(x) {return ee.Image(x)}): does not work (returns list of strings)
  • inputs.map(function(x) {return ee.Image(x)}): works (returns list of images)!

Why? What is wrong in doing ee.List() first? What is ee.List().map() doing compared to list.map()? The problem is that I want to use the ee.List() approach as it seems Python has no native list.map() function.

Pseudo-example (my case involves two distinct images not available elsewhere in an ImageCollection):

var list_years = ['USDA/NASS/CDL/2016', 'USDA/NASS/CDL/2017']


var CDLs = ee.List(list_years).map(function(x) { return ee.Image(x)})
var CDLs2 = list_years.map(function(x) { return ee.Image(x)})

print(CDLs)
print(CDLs2, "CDLs2")

Link to code: https://code.earthengine.google.com/7425530dc7eadd49ab87c2dfff416d0a

1

In general, beware of for loops, but:

var list = ['USDA/NASS/CDL/2016', 'USDA/NASS/CDL/2017'];

var images = [];
for (var i=0; i<list.length; i++) {
  var image = ee.Image(list[i]);
  images.push(image);
}
print(images); // List of images.

// Turn the list into an ImageCollection
var collection = ee.ImageCollection(images);
print(collection);
Map.addLayer(collection.select('cropland').mode())
  • Thanks! I am not sure yet how your solution help for case 1 (using ee.List)? It seems an alternative to case 2 (use raw list), which is unfortunately javascript specific so not portable to python? – Matifou Apr 15 at 20:08
  • As mentioned in the other answer, you can't use ee.List for this. But you can use a for loop in Python, too. – Nicholas Clinton Apr 15 at 21:02
0

You have to do that a little bit differently. Images can only be loaded with client-side strings, so that's why your second approach does work and your first doesn't. When doing a server-side mapping in the earth engine, you have to get the individual images of a collection using server-side objects.

Generally, all image collection in the Earth engine contain the property 'system:time_start', which contains the date the image is from. So if we make a server-side list of years, we can do mapping and filter the image collection on that property to get individual images:

// make some random years
var list_years = ['2005','2012','2014','2016', '2017'];

// map over the list
var CDLs = ee.List(list_years).map(function(x) { 
  // you could filter on the property 'system:time_start' using filterDate()
  var image = ee.ImageCollection("USDA/NASS/CDL").filterDate(ee.Date(x), ee.Date(x).advance(1, 'year')).first();
  return image;
});

// cast the list of images to an image collection
var imageCol = ee.ImageCollection.fromImages(CDLs);
print(imageCol);

Link code

  • Thanks! Unfortunately, my real case is more complicated than the CDL example here, what I have is really only image names not in an imageCollection. I tried then using getInfo(), thinking it would give me client side, does not seem to be working? – Matifou Apr 15 at 16:02
  • You can't use getInfo() in mapped functions. You will need to find a way to add properties to each image so you can obtain the single images while mapping over a list of properties. If you can give access to a couple of those images, there should be a workaround. – Kuik Apr 15 at 18:04
  • Mmh, I am not sure how I can add properties to these images if I can not even read them? Giving access to my data would not really help, the example above with two separate CDL images is exactly my case, (just assume there is not a CDL ImageCollection). Is there any way to read images from an input list ['USDA/NASS/CDL/2016', 'USDA/NASS/CDL/2017']? Thanks so much! – Matifou Apr 15 at 18:35
  • Well they should have a property named 'system:index' over which you can map. Try ee.List(YOUR_COLLECTION.aggregate_array('system:index')) – Kuik Apr 16 at 7:55
  • Sorry if I wasn't clear, there is no imageCollection at all, only two strings to import images from. So at that point there are no images, so no properties to extract :-( But I guess the answer is simply it is not possible to use ee.List(strings).map(ee.Image) – Matifou Apr 16 at 17:30

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.