1

I have made a map of vegetation loss and gain for a polygon in Google Earth Engine using NDVI and would like to calculate the area where loss occurred. I attempted to use the reducer to get the sum of pixels but I somehow need to specify the range of pixels to count only negative values where loss occurred and then total in square feet. My code is posted below, bottom part is where I'm having difficulty.

Can I use the stats.get function to only return negative values somehow?

var collection = ee.ImageCollection('LANDSAT/LE07/C01/T1_8DAY_NDVI');

var filtered2000 = collection.filterDate('2000-01-01', '2000-12-31');

var ndvi = filtered2000.median();

print(ndvi)

var geometry = ee.FeatureCollection('ft:1peQqvrT-WKfsXa_c63gI5h0-Wo_LmBuMnrEg3C').filterMetadata('geometry_vertex_count', 'equals', 10642)

Map.addLayer(ndvi, {palette: '000000, 00FF00', min: 0, max:.8});

//compare the ndvi in 2000 and 2010

var collection = ee.ImageCollection('LANDSAT/LE07/C01/T1_8DAY_NDVI');

//filter for the year 2000

var filtered2000 = collection.filterDate('2000-01-01', '2000-12-31')

//filter for the year 2010

var filtered2010 = collection.filterDate('2010-01-01', '2010-12-31');

//identify the median pixel value per year for 2000 and 2010 

var ndvi2000 = filtered2000.median();

var ndvi2010 = filtered2010.median();

//subtract the 2000 ndvi values from the 2010 ndvi values

var difference = ndvi2010.subtract(ndvi2000).clip(geometry);

//add layer with loss in red, gain in green

Map.addLayer(difference,
 {palette: 'FF0000, 000000, 00FF00', min: -0.3, max: 0.3});

//count pixels where loss occurred 

var stats = difference.reduceRegion({
  reducer: ee.Reducer.sum(),
  geometry: geometry,
  scale: 30,
  maxPixels: 1e9
});

print (stats);
print('Area where vegetation loss ocurred: ', stats.get(''), 'square meters')
  • Is this a public feature collection? I get an error that the fusion table is not found. – JepsonNomad Feb 17 at 21:47
1

In lieu of the geometry you use, here's an example with Maine. You can mask out all values you don't want, then multiply the ones you want by 0 and add 1 in order to get pixel value = 1 for each pixel that had a negative difference. Then by summing over the region you're just getting a total pixel count for where there was loss. Some simple math from there will get you the total area.

var maineCounties = ee.FeatureCollection('TIGER/2016/Counties')
  .filter(ee.Filter.eq('NAME', 'Waldo'));
print(maineCounties);
var geometry = maineCounties;
Map.addLayer(geometry);

var negmask = function(image) {
  return image.updateMask(image.lt(0));
};
print("negmask function",negmask);

var collection = ee.ImageCollection('LANDSAT/LE07/C01/T1_8DAY_NDVI');

var filtered2000 = collection.filterDate('2000-01-01', '2000-12-31');

var ndvi = filtered2000.median();

print(ndvi);

Map.addLayer(ndvi, {palette: '000000, 00FF00', min: 0, max:.8});

//compare the ndvi in 2000 and 2010

var collection = ee.ImageCollection('LANDSAT/LE07/C01/T1_8DAY_NDVI');

//filter for the year 2000

var filtered2000 = collection.filterDate('2000-01-01', '2000-12-31')

//filter for the year 2010

var filtered2010 = collection.filterDate('2010-01-01', '2010-12-31');

//identify the median pixel value per year for 2000 and 2010 

var ndvi2000 = filtered2000.median();

var ndvi2010 = filtered2010.median();

//subtract the 2000 ndvi values from the 2010 ndvi values

var difference = ndvi2010.subtract(ndvi2000).clip(geometry);

var diff_neg = negmask(difference);    
var diff_neg0 = diff_neg.expression(
    '0 * DIFF', {
      'DIFF': diff_neg
});
var diff_to1 = diff_neg0.expression(
    '1 + DIFF', {
      'DIFF': diff_neg0
});

Map.addLayer(diff_neg0,
 {palette: 'FF0000, 000000, 00FF00', min: -0.3, max: 0.3},"Negative differences");
Map.addLayer(diff_to1,
 {palette: 'FF0000, 000000, 00FF00', min: -0.3, max: 0.3},"Diff to 1");

//count pixels where loss occurred 

var stats = diff_to1.reduceRegion({
  reducer: ee.Reducer.sum(),
  geometry: geometry,
  scale: 30,
  maxPixels: 1e9
});

print (stats);
  • 1
    I must have had a typo in my feature collection id but this worked great nonetheless. I added the ee.Image.pixelArea() method at the end to calculate in sq. meters. var areaImage = diff_to1.multiply(ee.Image.pixelArea()) //count pixels where loss occurred var stats = areaImage.reduceRegion({ reducer: ee.Reducer.sum(), geometry: geometry, scale: 30, maxPixels: 1e9 }); print (stats); print('Area where vegetation loss ocurred: ', stats.get('constant'), 'square meters') – Misslee Feb 17 at 23:38

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.