1

I'm trying to calculate the mean NDVI for a polygon in an urban area. I have a code that takes Landsat7 images, clip them according to the geometry and knows to select only images with certain number of pixels.

The next step is that I want to take those images that are ranging from 1999-2013, and for each year to create one mean image. so in the end instead of for example 67 images, I'll have one image per year. I have faced two problems:

  1. I have tried to use :
var img2000=withNDVI.filter(ee.Filter.calendarRange,(2000,2001,'year')).mean();

in order to get one mean image from 2000, but I get the next error message:

Received too many arguments to function filter(). Expected at most 1 but got 2.

and I'm also not sure that if I choose 2000-2001, if it knows that I want only 2000 or it will take also 2001.

  1. I'm lazy. I don't want to write this line so many times and I want to have function that does it -that knows to calculate mean for each year but not sure how to do it, should I start with defining a variable for the function?

Here is the code I have:

var geometry=Osaka;
Map.centerObject(geometry,12);
//create Landsat7 database
/**
 * Function to mask clouds based on the pixel_qa band of Landsat SR data.
 * @param {ee.Image} image Input Landsat SR image
 * @return {ee.Image} Cloudmasked Landsat image
 */
var cloudMaskL457 = function(image) {
  var qa = image.select('pixel_qa');
  // If the cloud bit (5) is set and the cloud confidence (7) is high
  // or the cloud shadow bit is set (3), then it's a bad pixel.
  var cloud = qa.bitwiseAnd(1 << 5)
                  .and(qa.bitwiseAnd(1 << 7))
                  .or(qa.bitwiseAnd(1 << 3));
  // Remove edge pixels that don't occur in all bands
  var mask2 = image.mask().reduce(ee.Reducer.min());
  return image.updateMask(cloud.not()).updateMask(mask2).divide(10000)
  .copyProperties(image, ['system:time_start']);
};

//Create LANDSAT7 dataset

var dataset = ee.ImageCollection('LANDSAT/LE07/C01/T1_SR')
                  .filterDate('1999-01-01', '2013-04-29')
                  .select('B3','B4','pixel_qa')
                  .filterBounds(geometry)
                  .map(cloudMaskL457);

//clip the dataset according to the geometry
var clippedCol=dataset.map(function(im){ 
  return im.clip(geometry);
});

// Get the number of images.
var count = clippedCol.size();
print('Count: ',count);

//function to calculate NDVI in LANDSAT7

var addNDVI = function(image) {
  var NDVI = image.normalizedDifference(['B4', 'B3'])
  .rename('NDVI')
  .copyProperties(image,['system:time_start']);
  return image.addBands(NDVI);

};



//NDVI to the clipped image collection
var withNDVI = clippedCol.map(addNDVI).select('NDVI');

var NDVIcolor = {
  min: 0,
  max:1,
  palette: ['FFFFFF', 'CE7E45', 'DF923D', 'F1B555', 'FCD163', '99B718', '74A901',
    '66A000', '529400', '3E8601', '207401', '056201', '004C00', '023B01',
    '012E01', '011D01', '011301'],
};

var img2000=withNDVI.filter(ee.Filter.calendarRange,(2000,2001,'year')).mean();

1 Answer 1

1

You could set up a list of years that you're interested in, then map a function over that list which grabs the relevant imagery from your withNDVI collection and applies the relevant reducer. Note that the output for this is an imageCollection with 15, single-band images, one for each year.

// Declare years of interest
var years = ee.List.sequence(1999, 2013);
// Map a function to select data within the year and apply mean reducer
var byYear = ee.ImageCollection.fromImages(
    years.map(function(y) {
      return withNDVI
        .filter(ee.Filter.calendarRange(y, y, 'year'))
        .reduce(ee.Reducer.mean())
        .set('year', y);
    })
  );
// Look at your output: 15 elemenets suggest we're on the right track
print(byYear, "byYear");
// Add 1999 to the map - looks good!
Map.addLayer(byYear.first(), NDVIcolor, "img1999");
0

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.