1

For my Google Earth Engine project, I am finding the mean NDVI for each year between 2000 and 2020 in protected areas a part of the World Database on Protected Areas (WDPA). As an example, I have included code written to find the yearly mean NDVI values in Salonga National Park, which is located in the Democratic Republic of the Congo (DRC). I am using the MODIS vegetation index produced on 16-day intervals.

The code finds the mean NDVI value within the park boundaries and then places it into a dictionary with the years as keys and mean NDVI as values. Is there an easier/more concise way to write this code? This example finds the mean NDVI for 2000 and 2001 so far.

var countries = ee.FeatureCollection('USDOS/LSIB_SIMPLE/2017');
var drcongo = countries.filter(ee.Filter.eq('country_co', 'CG'));
var styleParams = {
  fillColor: 'b5ffb4',
  color: '868686',
  width: 2.5,
  opacity: 0.6,
};
//Map.addLayer(drcongo, styleParams, 'DRC');
Map.setCenter(23.5, -2.5, 6);

var salonga = ee.FeatureCollection('WCMC/WDPA/current/polygons')
.filter(ee.Filter.and(
    ee.Filter.bounds(drcongo.geometry()),
    ee.Filter.eq('ORIG_NAME', 'Parc national de la Salonga')
  ));
//Map.addLayer(dataset, {}, 'Parc national de la Salonga');

var modisNDVI = ee.ImageCollection('MODIS/006/MOD13Q1')
              .select('NDVI')
              .map(
                      function(img) {
                        var rescaled_NDVI = img.select('NDVI')
                                                .multiply(0.0001)
                                                .rename('NDVI_rescaled');
                        return img.addBands(rescaled_NDVI);
                   
  }
);
                    
var modisNDVI_2000 = modisNDVI.filter(ee.Filter.date('2000-01-01', '2000-12-31'));
var modisNDVI_2000_clip = modisNDVI_2000.mean().clip(salonga);
var mean_dict_2000 = modisNDVI_2000_clip.reduceRegion({
  reducer: ee.Reducer.mean(),
  geometry: salonga.geometry(),
  scale: 500,
});

var modisNDVI_2001 = modisNDVI.filter(ee.Filter.date('2001-01-01', '2001-12-31'));
var modisNDVI_2001_clip = modisNDVI_2001.mean().clip(salonga);
var mean_dict_2001 = modisNDVI_2001_clip.reduceRegion({
  reducer: ee.Reducer.mean(),
  geometry: salonga.geometry(),
  scale: 500,
});

var dictionary = ee.Dictionary({
  2000: mean_dict_2000.get('NDVI_rescaled'),
  2001: mean_dict_2001.get('NDVI_rescaled')
});
print('Parc national de la Salonga Annual NDVI', dictionary);

1 Answer 1

3

Make a list of years and filter the modisNDVI:

var years = ee.List.sequence(2000, 2019, 1).map(function(year){
  var start = ee.Number(year);
  var end = start.add(ee.Number(1));
  var ndviMeanYear = modisNDVI.select('NDVI_rescaled')
                              .filter(ee.Filter.calendarRange(start, end, 'year')).mean();
  return ndviMeanYear.set('year', start)
});

Then, get the ndvi mean for each PA and set the year:

    var ndviMean = ee.ImageCollection(years).map(function(img){
      var rR = img.reduceRegions({collection: pa,
                                reducer: ee.Reducer.mean(),
                                scale: 500,
                                tileScale: 4})
      return rR.map(function(feat){
        return feat.set('year', img.get('year'))
      })
}).flatten()

You will end up with a FeatureCollection with the 'mean', and 'year' properties for each PA.

https://code.earthengine.google.com/355b8a95f609f06529eadd73b4c2ae74

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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