0

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

2

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.

  • This correctly fixes the issue in my example data. Unfortunately when I attempt to apply this to larger datasets I receive this error: "FeatureCollection (Error) Error in map(ID=): Dictionary.get: Dictionary does not contain key: histogram." When I use substitute feature collections that are identical to my real data except for fewer polygons I have no issue. What explains this? – Mike Rhodes Apr 25 at 2:49
  • 1
    Possibly, there is no data inside the geometry of that feature, so no histogram is made. You should possibly filter out the geometries outside the data range of the map or filter out the features not containing histogram (ee.Filter.notNull(['histogram']) ?). – Kuik Apr 25 at 7:40

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.