1

I am trying to export a feature collection of histogram data for an image collection as a table in GEE. The image collection has three bands, NDSI_Snow_Cover, NDVI, and "remapped," which contains counts for 0, 1, 2, null values. The final table would have four columns: [system:index, 0, 1, 2, null], where the values under the 0, 1, 2, and null correspond to the histogram values from the remapped band.

I used the following link to get the histogram info: Exporting frequency histogram result as CSV in GEE?

But because I have an image collection, when I try to add the information to an empty feature collection the result is an empty variable. Instead, I add it as a property using .set(), but then it is harder to work with it as a property. Do I need to declare my null feature collection outside of my .map function? Is there a better way to get it into a feature collection or to export this information?

 // COUNT EACH PIXEL VALUE FREQUENCY (ZONAL STATS)
var Hist_featureCollection = Collection.map(function(image){
  var stats = image.reduceRegion({
  reducer: ee.Reducer.frequencyHistogram(),
  geometry: geometry,
  scale: 30,
  maxPixels: 1e15})
  var image_Stats = image.set('pixelCount', stats) 
  var featurestats = ee.Feature(null, stats);
  var FeatStats = ee.FeatureCollection([featurestats]);
  return image_Stats // if i return FeatStats and export, the data table is empty
});

print(Hist_featureCollection)
// this is the right info but not as a feature collection
var pixelCountList = Hist_featureCollection.aggregate_array('pixelCount');
print(pixelCountList)

// Get a list of the dates.
Export.table.toDrive({
  collection: Hist_featureCollection,
  folder: 'GEE',
  description:'pixelHist',
  fileFormat: 'CSV'
});
1
  • You're gettig pixelCount out of a FeatureCollection, why aren't you just exporting that collection? You can select() which properties you want. Jan 20 at 13:04

1 Answer 1

2

You were really close of getting the desired output. The only changes you need to do is pass the stats dictionary as a named property for the ee.Feature without geometry. Additionally, you need to cast the list of Features obtained from the map to an ee.FeatureCollection. Finally, you might be interested in using the selectors argument to select the properties to export in Export.table.toDrive.

// COUNT EACH PIXEL VALUE FREQUENCY (ZONAL STATS)
var Hist_featureCollection = ee.FeatureCollection(Collection.map(function(image){
  var stats = image.reduceRegion({
  reducer: ee.Reducer.frequencyHistogram(),
  geometry: geometry,
  scale: 30,
  maxPixels: 1e15});
  //var image_Stats = image.set('pixelCount', stats) 
  var featurestats = ee.Feature(null, {'pixelCount': stats});
  //var FeatStats = ee.FeatureCollection([featurestats]);
  return featurestats; // if i return FeatStats and export, the data table is empty
}));

print(Hist_featureCollection);

// Get a list of the dates.
Export.table.toDrive({
  collection: Hist_featureCollection,
  folder: 'GEE',
  description:'pixelHist',
  selectors: ['system:index','DATE_ACQUIRED', 'pixelCount'],
  fileFormat: 'CSV'
});

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.