1

I have an image with 3 distinct value (0,1,2), and use the frequency histogram reducer to count frequency of each pixel value in certain geometry (sort of like a zonal statistic function).

I am trying to export the result of the frequency histogram to a CSV file, but it always returns an error saying "Invalid argument: 'collection' must be a FeatureCollection."

Is there a way to convert the frequency histogram object into a feature collection? OR even better, any other straight forward method to export that simple table as CSV?

Here's snippet of my code:

 // COUNT EACH PIXEL VALUE FREQUENCY (ZONAL STATS)
var stats = primary2020.reduceRegion({
  reducer: ee.Reducer.frequencyHistogram(),
  geometry: geometry,
  scale: 30,
  maxPixels: 1e15
});

print(stats)

// EXPORT TABLE TO DRIVE
Export.table.toDrive({
  collection: stats,
  description: 'PrimaryForest2020stats',
  fileFormat: 'CSV',
});
2
1

Apparently I missed one section on the GEE tutorial that specifically explains how to create a null geometry. This is the additional lines before the exporting process.

// Make a feature without geometry and set the properties to the dictionary of means.
var featurestats = ee.Feature(null, histo);

// Wrap the Feature in a FeatureCollection for export.
var stats = ee.FeatureCollection([featurestats]);

// EXPORT
Export.table.toDrive({
  collection: stats,
  description: 'PrimaryForest2020Stats',
  fileFormat: 'CSV',
});
1

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.