1

I am working with the tropical moist forest dataset (https://forobs.jrc.ec.europa.eu/TMF/data.php#downloads) and I have a script where I can calculate the areas for a specific year based on landcover classes for a feature collection (https://code.earthengine.google.com/0c80ca868e67255c4da9e658575cc531).

As my script is currently written, I have to run each year separately (lines 73-104; see code snippet below) and export a separate CSV file for each year.

var AnnualChangesYear1=TMF_AnnualChanges.select('Dec1990').clip(community_lands); // clipped to annual layers

//---------START f(x) to calculate area by class over a feature

var calculateClassArea = function(feature) {
    var areas = ee.Image.pixelArea().addBands(AnnualChangesYear1)
    .reduceRegion({
      reducer: ee.Reducer.sum().group({
      groupField: 1,
      groupName: 'class',
    }),
    geometry: feature.geometry(),
    scale: 30,
    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(10000).round()
      return ee.List([classNumber, area])
    })
 
    var result = ee.Dictionary(classAreaLists.flatten())
    var district = feature.get('Name')
    return ee.Feature(
      feature.geometry(),
      result.set('district', district))
}

How can I create a stack of the annual years that I can iterate over for my feature collection where my CSV export has each year as a column (31 years) and each row is a feature from my feature collection?

1 Answer 1

0

You can do that creating an annual changes list and mapping it with a function as follows. This function has code lines for including "annualChanges" (32 years; not 31) as property for visualizing it in corresponding CSV file.

var annualChanges = ['Dec1990', 'Dec1991', 'Dec1992', 'Dec1993', 'Dec1994',
                     'Dec1995', 'Dec1996', 'Dec1997', 'Dec1998', 'Dec1999',
                     'Dec2000', 'Dec2001', 'Dec2002', 'Dec2003', 'Dec2004',
                     'Dec2005', 'Dec2006', 'Dec2007', 'Dec2008', 'Dec2009',
                     'Dec2010', 'Dec2011', 'Dec2012', 'Dec2013', 'Dec2014',
                     'Dec2015', 'Dec2016', 'Dec2017', 'Dec2018', 'Dec2019',
                     'Dec2020', 'Dec2021'];

var communityLandsYears = annualChanges.map(function (annualChanges) {

  var scale = TMF_AnnualChanges.select(annualChanges).projection().nominalScale(); 

  //print('Resolution (meters): ', scale); // Scale = 98.95065848192033
  //publications = 0.09 ha (30 x 30 m pixel)??

  AnnualChangesYear1=TMF_AnnualChanges.select(annualChanges);

  Map.addLayer(AnnualChangesYear1.updateMask(AnnualChangesYear1),{
    'min': 1,
    'max': 6,
    'palette': PALETTEAnnualChanges
  }, "JRC - Annual Changes - one year – v1 " + annualChanges, false);


  //=================================================================================================
  //=================================================================================================

  // PART 2:  calculate annual area for each class: 1= undisturbed TMF; 
  // 2=degraded TMF; 3=deforested TMF; 4= forest regrowth; 5= permanent/season water; 6 = other land cover

  var AnnualChangesYear1 = TMF_AnnualChanges.select(annualChanges).clip(community_lands); // clipped to annual layers

  //---------START f(x) to calculate area by class over a feature

  var calculateClassArea = function(feature) {
    var areas = ee.Image.pixelArea().addBands(AnnualChangesYear1)
    .reduceRegion({
      reducer: ee.Reducer.sum().group({
      groupField: 1,
      groupName: 'class',
    }),
    geometry: feature.geometry(),
    scale: 30,
    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(10000).round();
      return ee.List([classNumber, area]);
    });
 
    var result = ee.Dictionary(classAreaLists.flatten());
    var district = feature.get('Name');
    return ee.Feature(
      feature.geometry(),
      result.set('district', district));
  };

  //--------- END f(x)

  var districtAreas = community_lands.map(calculateClassArea); // apply over features

  districtAreas = districtAreas.map(function (feat) {
  
    return feat.set("annualChanges", annualChanges);
  
  });

  return districtAreas;

});

//print(ee.List(communityLandsYears));

var communityLandsYears = ee.List(communityLandsYears).map(function (ele) {
  
  return ee.List(ee.FeatureCollection(ele)
    .toList(ee.FeatureCollection(ele).size())).flatten();
  
}).flatten();

After running complete code in GEE code editor and also running the task, I got following CSV file for all years as follows:

enter image description here

Picture only shows a sample (1990-1997) of all years (1990-2021). In CSV file was deleted geom column for a better visualization.

1
  • Thank you! This is exactly what I needed.
    – TGUY
    Jul 6, 2022 at 17:27

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.