2

I have code working that extracts the area of forest loss from the Hansen Global Forest Change dataset for each available year and attaches it to the properties of a mapped FeatureCollection. This worked fine for the test FeatureCollection with 3 polygons and there was valid data to attach. When it maps over a feature that does not have any data associated with it, I get the error

"Array: No numbers in 'values', must provide a type."

you can see that the properties are the same: Array: No numbers in 'values', must provide a type.

It looks like this is what I need to do, but I'm not sure how to change it from image-based data to the grouped reducer

//var working_aoi = ee.FeatureCollection("users/jrobertsonpanthera/surveyed_grid_3test")
//var aoi = ee.FeatureCollection("users/jrobertsonpanthera/surveyed_grid_4test")

// Hansen Data
var gfc = ee.Image('UMD/hansen/global_forest_change_2022_v1_10')
var lossImage = gfc.select(['loss'])
var lossAreaImage = lossImage.multiply(ee.Image.pixelArea())

var lossYear = gfc.select(['lossyear'])

var stats = aoi.map(function (feature) {
  var tuples = ee.List(lossAreaImage
    .addBands(lossYear)
    .reduceRegion({
      reducer: ee.Reducer.sum().group({groupField: 1}),
      geometry: feature.geometry(),
      scale: 30,
      maxPixels: 1e13
    })
    .get('groups')
  ).map(function (group) {
    group = ee.Dictionary(group)
    var year = group.getNumber('group').add(2000)
    var area = group.getNumber('sum')
    return [year, area]
  })
  var yearsAreas = ee.Array(tuples).transpose()
  var years = yearsAreas
    .slice(0, 0, 1)
    .project([1])
    .toList()
    .map(function (year) {
      return ee.Number(year).format('loss_%d')
    })
  var areas = yearsAreas
    .slice(0, 1)
    .project([1])
    .toList()
  return ee.Feature(feature.geometry(), ee.Dictionary.fromLists(years, areas))
    .set('id', feature.get('id'))
})

print(stats)

Export.table.toDrive({
  collection: stats, 
  description: 'loss'
})

//show aoi, zoom the map
Map.addLayer(ee.Image().paint(aoi, 1, 3), null, 'region'); 
Map.centerObject(aoi,9);

//show the loss layer
var treeLossVisParam = {
  bands: ['lossyear'],
  min: 0,
  max: 1,
  palette: ['yellow', 'red']
};
Map.addLayer(gfc, treeLossVisParam, 'tree loss year');

Code: https://code.earthengine.google.com/b61d4528df6a3259ace7712f87569230

1 Answer 1

1

Something like the below should do the trick. It first extracts the groups, adds them to the feature as a property, adds a property to indicate if there aren't any groups, filters out features without groups, then, using the groups from the feature, continue with the logic from before.

var stats = aoi
  .map(function (feature) {
    var groups = ee.List(lossAreaImage
      .addBands(lossYear)
      .reduceRegion({
        reducer: ee.Reducer.sum().group({groupField: 1}),
        geometry: feature.geometry(),
        scale: 30,
        maxPixels: 1e13
      })
      .get('groups')
    )
    return feature
      .set('groups', groups)
      .set('empty', groups.size().eq(0))
  })
  .filter(ee.Filter.eq('empty', 0))
  .map(function (feature) {
    var tuples = ee.List(feature.get('groups'))
      .map(function (group) {
        ...

https://code.earthengine.google.com/6df42e1c8d03298a4bece53785153994

0

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.