4

I have a FeatureCollection that I am using .reduceRegions() over the Hansen Forest Loss dataset using ee.Reducer.fixedHistogram() to get the count of pixels per year/per feature.

The result is a feature which I can see the results in the 'histogram' property but can't select those results to store in another variable to do further analysis.

How can I select the results from the reducer (ex: how can I select 3.011764705882353 in the first element)?

I keep getting the following error:

ComputedObject (Error)
List.get, argument 'list': Invalid type. Expected: List<Object>. Actual: 
Float<dimensions=2>.

Here is the code I have:

var forest_years = forest.select('lossyear')

var test = forest_years.reduceRegions({
  collection:GB_patches,
  reducer: ee.Reducer.fixedHistogram(0,18,18),
  scale:30,
})


var first = ee.Feature(test.first())
var patch_years = ee.List(first.get('histogram'))
print (first)
print (patch_years)
print (patch_years.get(1))

Link to GEE code: https://code.earthengine.google.com/9ab9b35f4cc49b45f29386c70a0cb2cc

2 Answers 2

5

Yes, certainly it's a weir behavior. When you print(patch_years) it clearly says it's a List, but when trying to get a value it says that it is a Float<dimensions=2>, which matches with the column named 'histogram' in the FeatureCollection.

A workaround would be to bring it to the client side, and get it there:

var forest_years = forest.select('lossyear')

var test = forest_years.reduceRegions({
  collection:GB_patches,
  reducer: ee.Reducer.fixedHistogram(0,18,18),
  scale:30,
})


var first = ee.Feature(test.first())
var patch_years = ee.List(first.get('histogram'))
var patch_years_list = patch_years.getInfo()
print (first)
print (patch_years_list)
print (patch_years_list[1])
0
4

The output of a fixed histogram is an array with dimension 0 and 1. You should cast the output to an array and then slice over the 1-axis to get only the buckets means or counts:

var forest_years = forest.select('lossyear')

var test = forest_years.reduceRegions({
  collection:GB_patches,
  reducer: ee.Reducer.fixedHistogram(0,18,18),
  scale:30,
});

// rewrite the bucketMeans and counts from the histogram
test = test.map(function(feat){
  feat = ee.Feature(feat);
  // cast to an array and get the bucket means and counts
  var hist = ee.Array(feat.get('histogram'));
  var means = hist.slice(1, 1, 2).project([0]);
  var counts = hist.slice(1, 0, 1).project([0]); // eventually cast to a list using toList()
  return feat.set('means', means, 'counts', counts);
});

print(test);

link code. Note that I draw some geometries as a sample feature collection.

1
  • 1
    really nice observation @Kuik, I didn't realize it was an Array. Thumbs up! Commented Apr 25, 2019 at 0:31

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.