I am working with the Hansen et al. forest loss dataset. Effectively, I am using these layers to compute the amount of forest cover (pixels) in a given area. However, the locations that were sampled span several different years. I therefore need to create a set of layers with the forest cover in 2014,2015,etc.

To do this I created several groups of forest loss years, then masked them from the forest cover laYer. While this works for 2020, by just masking by the binary tree loss layer. However, for variables comprised of several bands, this just seems to have the opposite effect, as it leaves me only with the pixels that were lost, rather than the pixels which remain.

// Import Global Forest Change dataset.
var hansen_2020 = ee.Image("UMD/hansen/global_forest_change_2020_v1_8");

//Select bands.
//Tree cover
var treeCover = hansen_2020.select('treecover2000');
//Tree loss (Binary)
var treeLossAll = hansen_2020.select('loss');
//Tree loss (categorial)
var treeLoss = hansen_2020.select(['lossyear']);

//Select Tree loss up until (and including) a particular year
var Year2014 = treeLoss.lte(14)
var Year2015 = treeLoss.lte(15) 
var Year2016 = treeLoss.lte(16) 
var Year2017 = treeLoss.lte(17) 
var Year2018 = treeLoss.lte(18) 
var Year2019 = treeLoss.lte(19) 
var Year2020 = treeLoss.lte(20) 

//Visualising some of the tree losses (not done for all)

// Select tree cover variables
//Canopey cover %
var cc = ee.Number(60);

//Apply canopy cover threshold
var canopyCover = treeCover.gte(cc).selfMask();

// Show the 2000 tree cover layer
Map.addLayer(canopyCover,{palette: 'FF00FF'}, 'Tree Cover 2000');

//Update tree cover with loss 2014
var maskedtreeCover2014 = canopyCover.updateMask(Year2014);
Map.addLayer(maskedtreeCover2014,{palette: '228C22'},'Tree Cover 2014');

//Update tree cover with loss 2020
var maskedtreeCover2020 = canopyCover.updateMask(treeLossAll.eq(0));
Map.addLayer(maskedtreeCover2020,{palette: '228C22'},'Tree Cover 2020');

A nice way to check each step in your script is adding all the layers used in the calculation of interest. In your script, if you visualize the tree loss layers (e.g. Year2014), you'll see that all the areas that correspond to forest that was not lost in the analysed period (2000 - 2020) are masked. Therefore, in order to obtain the remaining forest for each year, you need to first unmask those pixels and then apply the mask to the initial forest layer. Here's the solution for the 2014 case, you just need to apply the same process to the other layers.

// Visualize the Year2014 image. You'll notice the masked areas
Map.addLayer(Year2014,{min:0, max:1},'Year2014');

// Unmask the masked areas and set them as 0
Year2014 = Year2014.unmask(0);
// Get the inverse mask, i.e., set 1 for areas that were not lost and 0 to areas lost in the 2000 - 2014 period
Year2014 = Year2014.eq(0);
// Apply mask to the canopyCover layer (gte 60 % Tree cover)
var TreeCover2014 = canopyCover.updateMask(Year2014);

// Show the 2000 tree cover layer for areas with more than 60 % values
Map.addLayer(canopyCover,{palette: '#25b61c'}, 'Tree Cover 2000');
// Add layer of remaining forest in 2014

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.