1

I'm trying to calculate the percent forest cover for 125 basins using the MapBiomas 30m land cover product for Brazil. The data is provided as an image with band values for each year. When I try to use a reducer to calculate the number of forest pixels in each basin, I get a feature collection with 0 elements and a column for each year.

Script link: https://code.earthengine.google.com/892ca26af47891fe40b44bb8f9036b76

Asset: https://code.earthengine.google.com/?asset=users/sharsid94/MAPBIOMAS/all_sheds_v2

Script:

// Objective: calculate % forest % savannah and % pasture cover for each year for each basin
Map.setCenter(-50,-16,5);

// import data
var basins = ee.FeatureCollection('users/sharsid94/MAPBIOMAS/all_sheds_v2')

//clip collection to basin extent and years 2001-2019
var collection = ee.Image('projects/mapbiomas-workspace/public/collection6/mapbiomas_collection60_integration_v1')
                .clip(basins)
                .select(['classification_2001','classification_2002','classification_2003',
                'classification_2004','classification_2005','classification_2006',
                'classification_2007','classification_2008','classification_2009',
                'classification_2010','classification_2011','classification_2012',
                'classification_2013','classification_2014','classification_2015',
                'classification_2016','classification_2017','classification_2018','classification_2019']);
print(collection);
Map.addLayer(collection)
//Map.addLayer(basins);

var forest_collection = collection.eq(3);
var savanna_collection =  collection.eq(4);
var pasture_collection =  collection.eq(15);

// function: calculate number of pixels in image collection for each basin
var reduceRegions = function(image) {
  var countPixels = image.reduceRegions({
    collection: basins,
    reducer: ee.Reducer.count(),
    scale: 300}); // data is at 30m resolution
  return countPixels
    .filter(ee.Filter.notNull(['count']))
    .map(function(feature) {
      return feature.set({
        'imgID': image.id()
      })})};

var forest_count = reduceRegions(forest_collection)
print('N Results:',forest_count); 

1 Answer 1

2

You're removing all features where count is null, however there is no count property in your output, because the reducer produces properties with the same name as the bands. Removing that filter, you can see the properties:

properties: Object (21 properties)
    FILENAME: br_0000247
    Id: 17091000
    classification_2001: 5036135
    classification_2002: 5036135
    classification_2003: 5036135
    classification_2004: 5036135
    classification_2005: 5036135
    classification_2006: 5036135
    classification_2007: 5036135
    classification_2008: 5036135
    classification_2009: 5036135
    classification_2010: 5036135
    classification_2011: 5036135
    classification_2012: 5036135
    classification_2013: 5036135
    classification_2014: 5036135
    classification_2015: 5036135
    classification_2016: 5036135
    classification_2017: 5036135
    classification_2018: 5036135
    classification_2019: 5036135

Update that filter to include the correct property names.

2
  • Thanks for your answer. However, it seems to work when I broke it down by individual years: code.earthengine.google.com/0c68a047855c2646a7ffaee1eb42bcbc. I checked if there was a difference with and without the null filter, and the values were the same.. I'm not so familiar with gee so I wonder why 1) the null filter didn't make a difference 2) why doing it one year at a time worked?
    – sharsid94
    Mar 9, 2022 at 18:44
  • If you have only 1 band, the reducer out is named the same as the reducer. If you have more than 1 band, that's not possible, so the outputs are named for the input bands. Mar 11, 2022 at 9:43

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.