Google Earth Engine – Getting Histogram Results from ReduceRegions and Opening Containers

google-earth-engine

This question is building on Getting List elements from .reduceRegion() results in Google Earth Engine?

My issue is identical, I need to calculate histograms for polygons and extract pixel counts.

As I need to process many polygons and getInfo inteferes with client side operations, how else can I iterate through a list of feature and extract the histogram counts?

The Earth Engine help suggests this maybe possible with "callback" functions, but I am unsure how to make this work, and in any case it is not recommended.

Linked below is my example code and the Earth Engine help page for Client vs. Server issues:

https://code.earthengine.google.com/133215210eb0c09bca4e3fd2ebd52ce1

https://developers.google.com/earth-engine/client_server

Best Answer

In this question, you are asking to handle a histogram output, which is a e.Dictionary(). So you should cast the output to a dictionary and then use get('bucketMeans') to get the single outputs.

// produce histogram of pixel values
var results = dataset.reduceRegions({
  collection: geometry,
  reducer: ee.Reducer.histogram(4, 1),
  scale: 30,
});

//  This should work
function resultsHisto(feature) {
  var histprops = ee.Dictionary(feature.get('histogram'));
  return  ee.Feature(feature)
              .set('counts', histprops.get('histogram'), 'means', histprops.get('bucketMeans'));
}

// correctly done
var resultsFinal = results.map(resultsHisto);
print(resultsFinal, "resultsFinal");

link code

A fixedHistogram reducer returns an array, see the answer I posted in your previous question.

Related Question