0

I am new to GEE and Javascript. I was looking for a solution to calculate the area of NDVI values above 0.2 from the years 1989 and 2018, and quantify vegetation loss. Is this possible? I have pasted the codes below. https://code.earthengine.google.com/48b9e089abdb51354c45333c7ea8b0f2

var MMR = 
/* color: #d63000 */
/* displayProperties: [
{
"type": "rectangle"}
] */
ee.Geometry.Polygon(
[[[112.21780240632957, 30.2114140665558],
  [112.21780240632957, 29.295784619630325],
  [113.23403775789207, 29.295784619630325],
  [113.23403775789207, 30.2114140665558]]], null, false);
// Load 1989 and 2010 Landsat5 annual composites.
var image1989 = ee.ImageCollection("LANDSAT/LT05/C01/T1").filterDate('1989-01-01','1989-12-31')
.filterBounds(MMR)
.median()
.clip(MMR);

var image2010 = ee.ImageCollection("LANDSAT/LT05/C01/T1").filterDate('2010-01-01','2010-12-31')
.filterBounds(MMR)
.median()
.clip(MMR);

// Compute NDVI the easy way.
var ndvi1989 = image1989.normalizedDifference(['B4', 'B3']);
var ndvi2010 = image2010.normalizedDifference(['B4', 'B3']);

// Compute the multi-band difference image.
var diff = ndvi1989.subtract(ndvi2010);

var NDVI_Palette = '00FF00, FFFF00, 000000';
Map.addLayer(ndvi1989.gte(0.2), {min: -0.3, max: 0.4, palette: NDVI_Palette}, "NDVI 1989");
Map.addLayer(ndvi2010.gte(0.2), {min: -0.3, max: 0.4, palette: NDVI_Palette}, "NDVI 2010");
Map.addLayer(diff, {min: -0.3, max: 0.4, palette: NDVI_Palette}, "Difference NDVI", false);
Map.centerObject(MMR, 9);
2

Here's how I'd do it: mask the image of interest with .gte(0.2).selfMask(), multiply by ee.Image.pixelArea(), get the total area within the region of interest using .reduceRegion().

var area_1989 = ndvi1989.gte(0.2).selfMask()
                            .multiply(ee.Image.pixelArea()).rename('Area')
                            .reduceRegion({reducer:ee.Reducer.sum(),
                                          geometry:MMR,
                                          scale:30,
                                          maxPixels:1e9
                                          })

print('Area 1989:', area_1989.get('Area'))

var area_2010 = ndvi2010.gte(0.2).selfMask()
                            .multiply(ee.Image.pixelArea()).rename('Area')
                            .reduceRegion({reducer:ee.Reducer.sum(),
                                          geometry:MMR,
                                          scale:30,
                                          maxPixels:1e9
                                          })

print('Area 2010):', area_2010.get('Area'))
3
  • Thanks for your help. @hooge048 However, I made an error when I tried to run this code. ComputedObject (Error) Dictionary. get: Dictionary does not contain key: Area. – Eva Mar 19 at 1:06
  • Sorry, that's my bad. I've updated the code. – hooge048 Mar 19 at 5:39
  • Thank you very much! It works! I also have a small question. I'm wondering if I want to choose the ndvi value from 0.1 to 0.2. What should I do? – Eva Mar 19 at 6:40

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.