[GIS] Getting List elements from .reduceRegion() results in Google Earth Engine

google-earth-engine

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

Best Answer

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])