I have a Google Earth Engine function that aggregates the area of plantation
(ee.Image; input of the function) within the boundary of a feature (geometry of the function) and exports it to a Table with columns as Hansen lossyear
.
However, my data plantation also has multiple sub-classes from 1 to 190. How do I modify the code below such that it individually extracts the area for each class and export it in the same Table by adding the sub-class as a column?
Check the GEE snapshot link: https://code.earthengine.google.com/8fa5d01312ab95d284954ed3eb67ce07
//###########################################################
// For plantation attribution
//###########################################################
function calculateForestLoss_var(input, geometry, savefilename) {
// Adding evaluated/processed 'area' bands to the Hansen dataset and calculating area
var variable = area_HansenLoss.updateMask(input.gt(0));
var Hansen_data_area = Hansen_data.addBands(variable);
// Calculate the sum of forest loss pixels for each feature in the geometry
var area_geometrysum = variable.reduceRegions({
collection: geometry,
reducer: ee.Reducer.sum(),
scale: Hansen_scale,
});
// Define the list of years to iterate over
var startYear = 2001;
var endYear = new Date().toISOString().slice(0,4)-2;
var years = ee.List.sequence(startYear - 2000, endYear - 2000);
// Function to add the forest loss data as properties to a feature
var addVar = function(feature) {
// Function to iterate over the sequence of years
var addVarYear = function(year, feat) {
// Cast var
year = ee.Number(year).toInt();
feat = ee.Feature(feat);
// Actual year to write as property
var actual_year = ee.Number(2000).add(year);
// Filter the data by year
var filtered = Hansen_data_area.select("lossyear").eq(year);
// Apply the filter to the data
filtered = Hansen_data_area.updateMask(filtered);
// Reduce the forest loss data over the feature
var reduc = filtered.reduceRegion({
geometry: feature.geometry(),
reducer: ee.Reducer.sum(),
scale: Hansen_scale,
maxPixels: 1e13
});
// Get the forest loss value
var loss = ee.Number(reduc.get("arealoss"));
// Set the name of the property for the current year
var nameloss = ee.String("loss_").cat(actual_year);
// Condition to handle cases where there is no forest loss data available
var cond = loss.gt(0);
// Set the property for the current year
return ee.Algorithms.If(cond,
feat.set(nameloss, loss),
feat);
};
// Iterate over the sequence of years and add the properties to the feature
var newfeat = ee.Feature(years.iterate(addVarYear, feature));
// Return the feature with the new properties
return newfeat;
};
// Map the addVar function over the FeatureCollection
var areas = area_geometrysum.map(addVar);
print('FOREST LOSS TO '+savefilename, areas);
// Generate the list of properties to export based on the start and end years
var propertiesToExport = ['ADM0_CODE', 'ADM1_CODE', 'ADM2_CODE'];
for (var i = startYear; i <= endYear; i++) {
propertiesToExport.push('loss_' + i);
}
// Export the results to Google Drive
Export.table.toDrive({
collection: areas,
description: 'Forest_loss_to_'+ savefilename +'_' + startID + '-' + endID + '_' + new Date().toISOString().slice(0,10),
folder: 'CP/LUC',
fileFormat: 'CSV',
selectors: propertiesToExport
});
}
calculateForestLoss_var(FL_att_to_old_plantations, geometry, 'OLD-PLANTATION')