1

I'm trying to export a CSV file with the columns: Date, Vegetation type, Mean NDVI.

Currently, I have an image collection with 2 bands: band 0 is NDVI and band 1 is vegetation type.

I have been able to create an object that gives me the mean NDVI per vegetation type for a single date (1 image in my image collection). However, I get an error (ImageCollection (Error) Collection.map: A mapped algorithm must return a Feature or Image.) when I try to map my group reducer to the entire image collection. How can I use my current image collection to export the CSV file I want?

Here is my current code:

var vegGroupMeans = function(image){
  return image.reduceRegion({
    reducer: ee.Reducer.mean().group({
      groupField: 1 ,
      groupName: 'LC_Prop3'
      }),
      geometry: modis_VI.first().geometry(),
      maxPixels: 1e10
  })};
  
var mean_singleImage = modis_VI.first().reduceRegion({
  reducer: ee.Reducer.mean().group({
    groupField: 1,
    groupName: 'LC_Prop3'
  }),
  geometry: modis_VI.first().geometry(),
  maxPixels: 1e10
});

print(mean_singleImage)

print(modis_VI.map(vegGroupMeans))

Here is the mean_singleImage output: enter image description here

And here is the formatting of my image collection: enter image description here

2
  • 1
    Please remember to post your code as text. Pictures of code require anyone who would help to retype your code. Please help others to help you by including the actual code.
    – Vince
    Nov 10, 2022 at 1:42
  • Sorry about that - I just updated my original question :)
    – carissa
    Nov 10, 2022 at 2:40

1 Answer 1

1

There are two things you should consider to run properly the desired process: 1) you'll need to put the result from reduceRegion inside an Feature object without a geometry (i.e., simple table) so the map function works, and 2) you'll need to extract the desired entries of the reduceRegion from the resulting dictionary to avoid having them nested inside a list in the exported table.

var vegGroupMeans = function(image){
  var reduct = image.reduceRegion({
    reducer: ee.Reducer.mean().group({
      groupField: 1 ,
      groupName: 'LC_Prop3'
      }),
    geometry: ee.Feature(table).geometry()
  });
  // Extract the properties from the dictionary and then form the list
  // By default, when using a grouped reducer there will be an entry
  // called groups. The resulting object can be casted into a List and 
  // then extract the first (and only) entry.
  reduct = ee.List(ee.Dictionary(reduct).get('groups')).get(0);

  // Put the results into a Feature without geometry
  return ee.Feature(null, reduct);
};

// Map the function
var resul = modis_VI.map(vegGroupMeans);

print(resul);

// Export, notice you'll need to cast the result into a FeatureCollection
Export.table.toDrive({
  collection: ee.FeatureCollection(resul),
  description: 'LC_Prop3'
});
3
  • Thank you for your help! Returning the Feature object, rather than a plain "Object" helped me to apply the function over my full Image Collection. But, I had 2 followup questions: (1) Inside of the vegGroupMeeans function, what does table refer to in "geometry: ee.Feature(table).geometry()"? For that line, I used "geometry: modis_VI.first().geometry()". But, I'm not sure if that was the intended geometry? (2) When I print resul, I only see the mean NDVI for the first landcover type from "LC_Prop3", rather than the mean for all 8 land cover types that I'm interested in. How can I address that?
    – carissa
    Nov 10, 2022 at 2:31
  • 1) I am assuming that the area of interest corresponds to the table extent, thus, that's why I put that in the geometry argument; however, it might as well be the image extent (depends on the analysis purpose); 2) have you tried using reduceRegions, similar to reduceRegion, but can reduce over multiple polygons at once. Nov 10, 2022 at 15:28
  • Thank you for your response! I ended up changing the last 2 lines of the function definition to: reduct = ee.Dictionary(reduct); return ee.Feature(null, reduct); Which allowed me to store the mean for all 8 landcover types, for each pixel, in each image.
    – carissa
    Nov 11, 2022 at 0:46

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.