1

I've computed maximum NDVI for each month. but I don't know how can I write this code through a loop in Google Earth Engine?

code link: https://code.earthengine.google.com/99fd025cdb0688dce74ea5fa0966a2c2

Map.centerObject(table);
Map.addLayer(table);

// ndvi jan

var jan = ee.ImageCollection("COPERNICUS/S3/OLCI")
.filterBounds(table)
.filterDate('2018-01-01','2018-02-01')
.map(function(img){
  return img.normalizedDifference(['Oa17_radiance','Oa08_radiance']);
})
.max()
.clip(table)
.rename('jan');

// ndvi feb

var feb = ee.ImageCollection("COPERNICUS/S3/OLCI")
.filterBounds(table)
.filterDate('2018-02-01','2018-03-01')
.map(function(img){
  return img.normalizedDifference(['Oa17_radiance','Oa08_radiance']);
})
.max()
.clip(table)
.rename('feb');

// ndvi mar

var mar = ee.ImageCollection("COPERNICUS/S3/OLCI")
.filterBounds(table)
.filterDate('2018-03-01','2018-04-01')
.map(function(img){
  return img.normalizedDifference(['Oa17_radiance','Oa08_radiance']);
})
.max()
.clip(table)
.rename('mar');

// ndvi apr

var apr = ee.ImageCollection("COPERNICUS/S3/OLCI")
.filterBounds(table)
.filterDate('2018-04-01','2018-05-01')
.map(function(img){
  return img.normalizedDifference(['Oa17_radiance','Oa08_radiance']);
})
.max()
.clip(table)
.rename('apr');

// ndvi may

var may = ee.ImageCollection("COPERNICUS/S3/OLCI")
.filterBounds(table)
.filterDate('2018-05-01','2018-06-01')
.map(function(img){
  return img.normalizedDifference(['Oa17_radiance','Oa08_radiance']);
})
.max()
.clip(table)
.rename('may');

// ndvi jun

var jun = ee.ImageCollection("COPERNICUS/S3/OLCI")
.filterBounds(table)
.filterDate('2018-06-01','2018-07-01')
.map(function(img){
  return img.normalizedDifference(['Oa17_radiance','Oa08_radiance']);
})
.max()
.clip(table)
.rename('jun');

// ndvi jul

var jul = ee.ImageCollection("COPERNICUS/S3/OLCI")
.filterBounds(table)
.filterDate('2018-07-01','2018-08-01')
.map(function(img){
  return img.normalizedDifference(['Oa17_radiance','Oa08_radiance']);
})
.max()
.clip(table)
.rename('jul');

// ndvi aug

var aug = ee.ImageCollection("COPERNICUS/S3/OLCI")
.filterBounds(table)
.filterDate('2018-08-01','2018-09-01')
.map(function(img){
  return img.normalizedDifference(['Oa17_radiance','Oa08_radiance']);
})
.max()
.clip(table)
.rename('aug');

// ndvi sep

var sep = ee.ImageCollection("COPERNICUS/S3/OLCI")
.filterBounds(table)
.filterDate('2018-09-01','2018-10-01')
.map(function(img){
  return img.normalizedDifference(['Oa17_radiance','Oa08_radiance']);
})
.max()
.clip(table)
.rename('sep');

// ndvi oct

var oct = ee.ImageCollection("COPERNICUS/S3/OLCI")
.filterBounds(table)
.filterDate('2018-10-01','2018-11-01')
.map(function(img){
  return img.normalizedDifference(['Oa17_radiance','Oa08_radiance']);
})
.max()
.clip(table)
.rename('oct');

// ndvi nov

var nov = ee.ImageCollection("COPERNICUS/S3/OLCI")
.filterBounds(table)
.filterDate('2018-11-01','2018-12-01')
.map(function(img){
  return img.normalizedDifference(['Oa17_radiance','Oa08_radiance']);
})
.max()
.clip(table)
.rename('nov');

// ndvi dec

var dec = ee.ImageCollection("COPERNICUS/S3/OLCI")
.filterBounds(table)
.filterDate('2018-12-01','2019-01-01')
.map(function(img){
  return img.normalizedDifference(['Oa17_radiance','Oa08_radiance']);
})
.max()
.clip(table)
.rename('dec');

var ndvistack = jan.addBands(feb).addBands(mar).addBands(apr).addBands(may)
.addBands(jun).addBands(jul).addBands(aug).addBands(sep).addBands(oct)
.addBands(nov).addBands(dec);
0

I think this should get you going. Inspiration in addition to copy/paste from a few sources including here and here. Your table isn't public so I used a random county from Maine instead.

var maineCounties = ee.FeatureCollection('TIGER/2016/Counties')
  .filter(ee.Filter.eq('NAME', 'Waldo'));
print(maineCounties);
var table = maineCounties;
Map.addLayer(table);

// Function to get image NDVI
var getNDVI = function(img){
  var NDVI = img
    .normalizedDifference(['Oa17_radiance','Oa08_radiance'])
    .rename("NDVI");
  return NDVI;
};

// Make start and end layers
var start = ee.Date.fromYMD(2018,01,01);
var months = ee.List.sequence(0, 11);
var startDates = months.map(function(d) {
  return start.advance(d, 'month');
});
print("Start dates",startDates);

// Collect imagery by month
var monthmap = function(m){
  var start = ee.Date(m);
  var end = ee.Date(m).advance(1,'month');
  var date_range = ee.DateRange(start,end);
  var S3Month = ee.ImageCollection('COPERNICUS/S3/OLCI')
    .filterDate(date_range)
    .filterBounds(table)
    .map(getNDVI)
    .map(function(img){return img.clip(table)});
  return(S3Month.max());
};
print("monthmap",monthmap);

var list_of_images = startDates.map(monthmap);
print('list_of_images', list_of_images);
var mt = ee.ImageCollection(list_of_images);
print("Monthly NDVI", mt);

Map.addLayer(mt,{},"NDVI");

mt should be a 12-layer object with each layer being NDVI for that month.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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