[GIS] Earth Engine: Map over list versus ee.list

google-earth-enginejavascript

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

Best Answer

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

Related Question