I want to analyse and extract the total annual deforestation between 2001-2021 using the Hansen forest change dataset in Google Earth Engine.
For that, I use a feature collection containing (let's say) sub-national boundaries. The code has referred to as Shape, which has about 8-9k polygons.
The code that analyses annual deforestation (area in km2) for each shape (subset of Feature Collection Shapes) and saves it as an array is pasted below (this part works fine):
var annualforestloss = function(ID)
{
// Get a feature collection with just the custom shapefile feature.
// Shape contains multiple features (polygons)
// Use any shapefile, if mine doesn't work
var geometry = Shape.filter(ee.Filter.eq('ID', ID));
//print(geometry);
Map.addLayer(geometry);
// Get the loss image.
// This dataset is updated yearly, so check for the latest version.
var gfc2021 = ee.Image('UMD/hansen/global_forest_change_2021_v1_9');
var lossImage = gfc2021.select(['loss']);
//Converting m2 to km2
var lossAreaImage = lossImage.multiply(ee.Image.pixelArea()).divide(1000000);
var lossYear = gfc2021.select(['lossyear']);
var lossByYear = lossAreaImage.addBands(lossYear).reduceRegion({
reducer: ee.Reducer.sum().group({
groupField: 1
}),
geometry: geometry,
scale: 30,
maxPixels: 1e9
});
//print(lossByYear);
var statsFormatted = ee.List(lossByYear.get('groups'))
.map(function(el) {
var d = ee.Dictionary(el);
return [ee.Number(d.get('group')).format("20%02d"), d.get('sum')];
});
var statsDictionary = ee.Dictionary(statsFormatted.flatten());
//print(statsFormatted)
//print(statsDictionary)
// Since there has been no forest loss for some years, the code above does not include those years.
// To remedy that, we use another dictionary with values as zero and later combine them without overlap
var dict1 = ee.Dictionary({
"2001": 0, "2002": 0, "2003": 0, "2004": 0, "2005": 0, "2006": 0, "2007": 0, "2008": 0,
"2009": 0, "2010": 0, "2011": 0, "2012": 0, "2013": 0, "2014": 0, "2015": 0, "2016": 0,
"2017": 0, "2018": 0, "2019": 0, "2020": 0, "2021": 0,});
statsDictionary = statsDictionary.combine(dict1, false);
//print(statsDictionary)
// Platting chart
/*
var chart = ui.Chart.array.values({
array: (statsDictionary.values()),
axis: 0,
xLabels: statsDictionary.keys()
}).setChartType('LineChart')
.setOptions({
title: 'Yearly Forest Loss (ID-'+ID+')',
hAxis: {title: 'Year', format: '####'},
vAxis: {title: 'Area (square kilometer)'},
legend: { position: "none" },
lineWidth: 1,
pointSize: 3
});
print(chart)
*/
// Adding 'ID' for seamless data analysis in python
return statsDictionary.values().add(ID)
};
However, the problem I am facing is: Looping through the features in Shapes: Although I know that 'for loops' are not recommended, I am limited by my ability to use map() function in GEE, which returns some client/server error.
// This function combines results from arrays in a loop
var combinearray = function(array1, array2)
{
var arraynew = (ee.Array.cat([array1, array2], 0))
return arraynew
}
// Size of the features (total shapes about 8664)
print(Shape.size())
//**Limited by skill to run it with 'map' function. ########## Please help!**!!!!!!!############
// Works fine for less loops. Hangs for double digit loops
var list = (annualforestloss(0))
for (var ID = 1; ID <= 6; ID++) {
var listnew = (annualforestloss(ID))
var list = (combinearray(list, listnew))
}
print(list)
//reshaping the array for export
var combinedarray = (list.reshape([7,22]))
print('--')
print(combinedarray)
Any help is greatly appreciated. The following link can be used to see the code editor: https://code.earthengine.google.com/cd335cd1356dc69e3c9adc4ba9710bcb