3

I am trying to extract NDVI data for provinces in Cambodia over time. The following codes work for a specific year (2002).

Can someone show me a better way to collect the data over time automatically on GEE?

var collection = ee.ImageCollection('LANDSAT/LE07/C01/T1_32DAY_NDVI')
.filterDate('2002-01-01', '2002-12-31').select('NDVI');

var mean = collection.reduce(ee.Reducer.mean());

var meanDictionary = mean.reduceRegions({
  collection: Cambodia,
  reducer: ee.Reducer.mean(),
  scale: 30,
});

Export.table.toDrive(meanDictionary,"NDVI","NDVI","NDVI_Cambodia");

1 Answer 1

4

You will need to define a list of years you are interested in, and map over that list to get a mean NDVI image for every year. Then use reduceRegion on every image to obtain the mean NDVI value in the geometry. Export the resulting image collection as a table.

// Load collection and geometry
var Cambodia = geometry;
var collection = ee.ImageCollection('LANDSAT/LE07/C01/T1_32DAY_NDVI').select('NDVI');

// Declare years of interest
var years = ee.List.sequence(2000, 2010);

// Map a function to select data within the year and apply mean reducer
var byYear = ee.ImageCollection.fromImages(
    years.map(function(y) {
      var image = collection.filter(ee.Filter.calendarRange(y, y, 'year'))
              .mean();
        // get the mean value for each year
      var meanDictionary = image.reduceRegion({ // use reduce region for single geometry
        geometry: Cambodia,
        reducer: ee.Reducer.mean(),
        scale: 30,
      });
      //set data to image
      return image.set(meanDictionary)
          // add time information to image
          .set('year', y,'system:time_start', ee.Date.fromYMD(y,1,1).millis())
    })
  );

// to test code printing, increase the scale or decrease size goemetry
// print(byYear)

// export the image collection as a table (CSV)
Export.table.toDrive(byYear,"NDVI","NDVI","NDVI_Cambodia");

link code

-- EDIT --

user requested the code for featurecollections:

// Map a function to select data within the year and apply mean reducer
var byYear = ee.FeatureCollection(years.map(function(y) {
      var image = collection.filter(ee.Filter.calendarRange(y, y, 'year'))
              .mean();
        // get the mean value for each year
      var newFC = image.reduceRegions({ 
                              // use reduceRegion for single geometry
                              // use reduceRegions for multiple features
        collection: Cambodia,
        reducer: ee.Reducer.mean(),
        scale: 30,
        tileScale : 1, // increase tileScale if running out of computation or export
      }) // NOTE THAT THIS RETURNS A FEATURE COLLECTION, thus we map over the features to add date
        .map(function(feat){
          return feat.set('year', y,'system:time_start', ee.Date.fromYMD(y,1,1).millis());
        });
        // now we return the featurecollection, not the image
      return newFC; 
    })
  ).flatten(); // flatten() the feature collection of feature collections

link code

6
  • Hi Kuik, thank you for help! Unfortunately, when I replicated the codes, I got the following error: "Image.reduceRegion: Too many pixels in the region. Found 384225549, but maxPixels allows only 10000000. Ensure that you are not aggregating at a higher resolution than you intended; that is a frequent cause of this error. If not, then you may set the 'maxPixels' argument to a limit suitable for your computation; set 'bestEffort' to true to aggregate at whatever scale results in 'maxPixels' total pixels; or both."
    – Huong Bui
    Feb 12, 2020 at 21:29
  • In your "image.reduceRegion()" function, add the 'maxPixels: 384225549'. This ensures that it still calculates. The number is quite high, so be ready for a timeout... Feb 12, 2020 at 21:41
  • Hi Sean, thank you very much for your suggestion! It works when I add the 'maxPixels: 384225549'. Still, the final table only produces average NDVI data aggregated at the country level, while I am looking for data at province level.
    – Huong Bui
    Feb 12, 2020 at 22:40
  • You didn't share the feature collection you are working with, share it to make sure you get appropriate help. If it is a FC, as I suggest it is, apply something like the following: link.
    – Kuik
    Feb 13, 2020 at 19:31
  • I am using shapefile of Cambodia (ADM 1) available at: diva-gis.org/gdata. It can also be downloaded from code.earthengine.google.com/?asset=users/tronganhtrinh/…. However, I got the following error: Error: ImageCollection.fromImages, argument 'images': Invalid type. Expected: List<Image<unknown bands>>. Actual: List<FeatureCollection>. Also, is it possible to extract the monthly NDVI?
    – Huong Bui
    Feb 13, 2020 at 20:25

Your Answer

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

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