1

I have put together some GEE script to generate a cloud-masked NDVI mosaic over a pretty large AOI. However, I'd like to modify it so that the output image contains the mean NDVI pixel values for the time period. I'm new to GEE and I'm not sure the best way to implement this (.mean, or through a reducer?). I'm also not sure if I should calculate the mean before or after masking the clouds (it seems like masking first would be the best).

//polygon that contains the study area
var poly = ee.Geometry.Polygon(
        [[[-122.203894, 43.805966],
          [-120.027298, 37.792365],
          [-114.769423, 38.723926],
          [-116.502654, 44.826888]]]);

var l8collection = ee.ImageCollection('LANDSAT/LC08/C01/T1_SR')
.filterBounds(poly)
.filterDate('2015-05-01', '2015-08-30') //month period
.filterMetadata('CLOUD_COVER', 'less_than', 10);
print(l8collection);

// create function to mask clouds
var maskClouds = function(image){
  var pixel_qa = image.select('pixel_qa');
  return image.updateMask(pixel_qa.eq(322));
};
var l8masked = l8collection.map(maskClouds);
print (l8masked);

// Compute NDVI
var getNDVI = function(img){
  return img.addBands(img.normalizedDifference(['B5','B4']).rename('NDVI'));
  };

// map over image collection
var l8ndvi = l8masked.map(getNDVI);
print(l8ndvi);
var ndvi = l8ndvi.select('NDVI');
print(ndvi);

// Export
Export.image.toDrive({
  image: ndvi.mosaic().clip(poly),
  description: 'NDVI',
  scale: 30,
  maxPixels: 1e13,
  skipEmptyTiles: true
});

1 Answer 1

0

Calling mean() on your collection is the same thing as calling reduce(ee.Reducer.mean()). It's just a shorthand. You can read up on reducing a collection here and here. You could consider calculating the median instead of the mean too. That way, bad pixels left from your masking will not influence the result (unless you really have bad data).

You definitely want to mask clouds before reducing, otherwise the mean will be skewed by the inclusion of cloudy pixels. At the end, you might also want to clip your resulting image to your AOI.

var meanNdvi = ndvi
  .mean()
  .clip(poly)
3
  • Thanks, Daniel! If I'm understanding this correctly, I would create the meanNDVI variable right after I create the ndvi variable, then export a meanNDVI mosaic, right? When I experimented with this, I get an error that "meanNDVI.mosaic" is not a function. This is where my understanding of the script fails me. Is it because meanNDVI has to be defined as an image?
    – Gus
    Commented Feb 18, 2022 at 17:50
  • You call mosaic() on an image collection to turn it into a single image. In this case you already got an image, meanNdvi. That's the one to export. Commented Feb 18, 2022 at 18:55
  • Thank you for the clarification. I think I have it working now! Thank you!
    – Gus
    Commented Feb 18, 2022 at 21:45

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.