1

I would like to calculate the loss area in a certain elevation class in GEE. When I run code 1 below, it gives the same amount as the total loss area in my study region, code 2. I switched arealoss and class3 in code 1 as well, and didn't work. Besides, elevation(1), (2), .. all classes give the same result. How can I calculate the loss area for each elevation class?

code 1:

var class3 = elevation.eq(3).selfMask();
var stats1 = arealoss.reduceRegion({ reducer: 
ee.Reducer.sum(),
geometry: class3.geometry(),
scale: 30,
maxPixels: 1e9,
bestEffort: true });

code 2:

var stats2 = arealoss.reduceRegion({
            reducer: ee.Reducer.sum(),
            geometry: peru.geometry(),
            scale: 30, 
            maxPixels: 1e9,
            bestEffort: true });

Besides, I want to repeat this calculation for 7 different elevation classes. is it possible to write a function for this calculation in GEE?

1 Answers1

0

class3.geometry() just gives you the footprint of the image — the region in which it is known to have data. It doesn't care at all about the mask, or the values of the pixels.

What you need is to mask the image that you're reducing by your classification. When you do that, the reducer (which works on a per-pixel basis) will ignore all masked pixels.

var class3 = elevation.eq(3);
var stats1 = arealoss
  .updateMask(class3)  // This hides all non-class-3 pixels in arealoss
  .reduceRegion({ 
    reducer: ee.Reducer.sum(),
    geometry: peru.geometry(),   // This only needs to be big enough, not exact
    scale: 30,
    maxPixels: 1e9,
    bestEffort: true
  });
Kevin Reid
  • 37,492
  • 13
  • 80
  • 108
  • thank you!!! I am a beginner and learning GEE. I will pay more attention on how to combine different methods in the code, especially in ReduceRegion. – youcodegirl Jan 07 '21 at 17:24