1

I have a FeatureCollection of forest loss polygons that I am reducing (using ee.Reducer.fixedHistogram()) over the Hansen forest loss dataset to create a histogram. This should give me a histogram of the pixel count per year. The resulting FeatureCollection has the histogram as a property (GOOD!). I am trying to find the years that have had the most forest loss and the years with the least forest loss (that is greater than zero).

My problem comes from the histogram that is part of the feature, it is an array but I can't sort it or find the index of a specific value (BAD!)(in the example I'm trying to find the min year, but I need to find the max as well).

Neither list methods nor array methods are working well (but that may come from my inexperience with GEE) and I keep getting the error:

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

Here is the link to the code: https://code.earthengine.google.com/743d06e29e4d6168e074d772bb18bada

Also, additionally I can't export the features in the featurecollection citing that it is an invalid type. I believe that this is the same issue as to why I can't sort or manipulate the arrays.

2

1 Answer 1

2

You had a couple bugs. Try this:

var GB_patches = ee.FeatureCollection("users/walteral/GB_patchesv2"),
    forest = ee.Image("UMD/hansen/global_forest_change_2017_v1_5");

//selects the 'lossyear' band
var forest_years = forest.select('lossyear')

//reduces the collection and creates a featurecollection with a histogram per feature
var year_counts = forest_years.reduceRegions({
  collection:GB_patches,
  reducer: ee.Reducer.fixedHistogram(0,18,18),
  scale:30,
})

//for simplification I just took the first feature
var patch_years = ee.Array(year_counts.first().get('histogram'))

//I can slice the array but trying to cast it as a list still doesn't work with list methods
var years = ee.List(patch_years.slice(1,0,1))
var smallarea = ee.List(patch_years.slice(1,1,2))

print (smallarea)
var area = ee.Array(patch_years.slice(1,1,2))
var areaList = area.toList().removeAll([[0]]).flatten()
print(areaList)

//This is the smallest amount of forest loss.
// If I can find the index of this value I can find the 
//index of the year I am looking for in the above list
var minArea = areaList.reduce(ee.Reducer.min()) 
print(minArea)
var index = areaList.indexOf(minArea)

print(index)
2
  • Please upvote if this answer works for you. Commented Jul 24, 2018 at 23:30
  • @walteral Please accept this answer if it works for you.
    – Jon
    Commented Sep 5, 2018 at 15:46

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.