[GIS] Writing code for monthly NDVI medians in Google Earth Engine

google-earth-enginelandsatndvi

My goal is to get monthly NDVI medians in Google Earth Engine. This helpful post got me started: reduce image collection to get annual monthly sum precipitation

However, I am using Landsat (not MODIS). This script does run but the map layer has "no bands to visualize".

  1. How can I visualize the bands? (Maybe the issue is that this dataset only has 1 band?).
  2. How can I add all 12 map layers, one per month? (perhaps with stacking?)
var ndvi = ee.ImageCollection('LANDSAT/LT5_L1T_32DAY_NDVI');

var months = ee.List.sequence(1, 12);

// Group by month, and then reduce within groups by mean();
// the result is an ImageCollection with one image for each
// month.
var byMonth = ee.ImageCollection.fromImages(
  months.map(function (m) {
    return ndvi.filter(ee.Filter.calendarRange(m, m, 'month'))
                .select().median()
                .set('month', m);
}));
print(byMonth);

Map.addLayer(ee.Image(byMonth.first()));

Best Answer

For the record, here is a good way to do this:

var imageCollection = ee.ImageCollection("LANDSAT/LT05/C01/T1");
var months = ee.List.sequence(1, 12);

var composites = ee.ImageCollection.fromImages(months.map(function(m) {
  var filtered = imageCollection.filter(ee.Filter.calendarRange({
    start: m,
    field: 'month'
  }));
  var composite = ee.Algorithms.Landsat.simpleComposite(filtered);
  return composite.normalizedDifference(['B4', 'B3']).rename('NDVI')
      .set('month', m);
}));
print(composites);

var check = ee.Image(composites.first());
Map.addLayer(check, {min: 0, max: 1}, 'check');

For the search robots, this is a "temporally grouped reduction."