1

I am trying to calculate the area of each class of my grid. My code reference is from here.

My code: Earth engine link

var grid = ee.FeatureCollection("users/miaohu828/finalGridexpot1000")
var modisLandcover = ee.ImageCollection("MODIS/006/MCD12Q1")
var bp = ee.FeatureCollection("users/miaohu828/bankment_polygon")

var filtered2018 = modisLandcover.filter(
  ee.Filter.date('2018-01-01', '2018-12-31'))
// The filtered collection has 1 classified image for 2018
// We can call .first() to get that element and cast it to an image
var landcover2018 = ee.Image(filtered2018.first())
// Each year's image contains multiple bands with different classification scheme
// We pick the band 'LC_Type1' for this example and extract it.
var classified2018 = landcover2018.select('LC_Type1')
// There are 17 classes in the scheme as described in https://developers.google.com/earth-engine/datasets/catalog/MODIS_006_MCD12Q1
// We pick the color associated with each class from the documentation and create a palette
var palette = ['05450a', '086a10', '54a708', '78d203', '009900',//forest
               'c6b044', 'dcd159','fbff13', //shrub, savannah
               'b6ff05', //grassland
               '27ff87', //wetland
               'c24f44','ff6d4c',//croplands
               'a5a5a5',//urban
               '69fff8', //snow
               'f9ffa4', //Barren
               '1c0dff'//water
               ].join(',');
               
var vis_params = {min: 0, max: 17, palette: palette};
// Visualize the classified image
//Map.addLayer(classified, {min:1, max:17, palette: palette}, 'MODIS Landcover 2018')
var bpLandcover2018 = classified2018.clip(bp)
Map.addLayer(bpLandcover2018,{min:1, max:17, palette: palette},'Yangtze Land Cover 2018')

// Add a new property 'grid_name'
var finalGrid = grid.map(function(f) {
  return f.set('grid_name', ee.String('grid_').cat(f.get('system:index')))
})
print(finalGrid.first())

var calculateClassArea = function(feature) {
    var areas = ee.Image.pixelArea().addBands(classified2018).reduceRegion({
      reducer: ee.Reducer.sum().group({
      groupField: 1,
      groupName: 'class',
    }),
    geometry: feature.geometry(),
    scale: 500,
    maxPixels: 1e10
    })

    var classAreas = ee.List(areas.get('groups'))
    var classAreaLists = classAreas.map(function(item) {
      var areaDict = ee.Dictionary(item)
      var classNumber = ee.Number(
        areaDict.get('class')).format()
      var area = ee.Number(
        areaDict.get('sum')).divide(1e6).round()
      return ee.List([classNumber, area])
    })
    
    var result = ee.Dictionary(classAreaLists.flatten())
    var grid_name = feature.get('grid_name')
    return ee.Feature(
      feature.geometry(),
      result.set('grid_name', grid_name))
}

var districtAreas = grid.map(calculateClassArea)
var classes = ee.List.sequence(1, 17)
var outputFields = ee.List(['grid_name']).cat(classes).getInfo()

print(districtAreas)
 
Export.table.toDrive({
    collection: districtAreas,
    description: 'class_area_by_grid',
    folder: 'earthengine',
    fileNamePrefix: 'class_area_by_grid',
    fileFormat: 'CSV',
    selectors: outputFields
    })

My code runs with an error:

FeatureCollection (Error)
Error in map(ID=000000000000000000ab):
Dictionary. set: Parameter 'value' is required.

I can't find a way to solve it.

1
  • 5
    Change 'grid' into 'grid_name' in line 56. var grid_name = feature.get('grid_name') Commented May 13, 2021 at 5:02

1 Answer 1

0

I think the problem is here:

var grid_name = feature.get('grid_name')

Try to have the same name with your feature.

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.