2

I have a variable that is a function that returns an image collection of the monthly mode from google's dynamic world. This works great.

I'm attempting to use this while mapping over a feature collection. It works but the image collections returned appears to be empty. Image collection of unknown elements.

https://code.earthengine.google.com/e997fa4ee21cdb2474d21ea640b93fea

In the above code I'm printing out the initial variable then my attempt at mapping over the feature collection. I want to get the initial output for each feature in the feature collection.

Where am I going wrong?

In the example I have 3 features. Each feature should have 12 images (1 for each month). I can flatten the output and I do correctly get 36 images that appear as they should. I can add a property that associates each image with the feature using the feature id then later filter by that property. This works https://code.earthengine.google.com/a28fe8fb412d738605c4d4d13208bcee

1 Answer 1

3

The issue with your code is that if you map a particular function to a ee.FeatureCollection, the expected output will be also be a ee.FeatureColletion. In this case, the intended output should be an ee.ImageCollection, instead. One trick to avoid this is to map over a ee.List which returns a ee.List which you can easily convert it to any type of object you desire.

// Try to do the same as above but by feature
var byFeature = function(index){
  // Select features one by one according to system:index
  var ft = geometry.filter(ee.Filter.eq('system:index',ee.String(index)))
                   .first();
  // Removed the ImageCollection.fromImages so everything is stored as elements of a list
  var byMonth = months.map(function (m) {
          return dwCol.filter(ee.Filter.calendarRange(m, m, 'month'))
                      .mode()
                      .set('month', m)
                      .clip(ft);
  });  
  return byMonth;
};

print(geometry);

// Get the system:index property as a list
var list = geometry.aggregate_array('system:index');
print(list);

// Map over the system:index list
var all = list.map(byFeature);
print(all);

// Flatten the list of lists into a list of images and convert to ImageCollection
var resul = ee.ImageCollection.fromImages(all.flatten());
print(resul);

// Add the first image to confirm that the output is correct
Map.addLayer(resul.first(), {}, 'first');

Not the answer you're looking for? Browse other questions tagged or ask your own question.