我有一个 Google Earth Engine 函数,它在一个特征(函数的几何形状)的边界内聚合
plantation
(ee.Image;函数的输入)的面积,并将其导出到一个 Table,列为 Hansen lossyear
.
但是,我的数据种植园也有从 1 到 190 的多个子类。我如何修改下面的代码,以便它单独提取每个类的区域并通过添加子类将其导出到同一个Table专栏?
检查 GEE 快照链接: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: 'Chalmers_Postdoc/LUC',
fileFormat: 'CSV',
selectors: propertiesToExport
});
}
calculateForestLoss_var(FL_att_to_old_plantations, geometry, 'OLD-PLANTATION')