0

I am trying to calculate the monthly LAI composite, but somehow code is not working. Need Suggestions? It shows this error, Line 36: image.get is not a function.

// Define the start and end dates
var startDate = '2006-01-01';
var endDate = '2020-12-31';

// Function to calculate monthly LAI mean
var calculateMonthlyLAI = function(year, month) {
  var startDate = ee.Date.fromYMD(year, month, 1);
  var endDate = startDate.advance(1, 'month');
  
  var lai = ee.ImageCollection('MODIS/061/MOD15A2H')
              .filterBounds(region)
              .filterDate(startDate, endDate)
              .select('Lai_500m')
              .mean()
              .set('system:time_start', startDate.millis())
              .set('system:time_end', endDate.millis());
  
  return lai;
};

// Create an empty image collection to store the monthly LAI
var monthlyLAI = ee.ImageCollection([]);

// Iterate over the years and months
for (var year = 2006; year <= 2020; year++) {
  for (var month = 1; month <= 12; month++) {
    var lai = calculateMonthlyLAI(year, month);
    var temporaryCollection = ee.ImageCollection([lai]); // Create a temporary collection with the single lai image
    monthlyLAI = monthlyLAI.merge(temporaryCollection);
  }
}

// Function to export an image to Google Drive
var exportImage = function(image) {
  var dateString = ee.Date(image.get('system:time_start')).format('YYYY-MM');
  var fileName = ee.String('monthly_lai_').cat(dateString);
  
  Export.image.toDrive({
    image: ee.Image(image),
    description: fileName,
    folder: 'GEE_exports',
    scale: 500,
    region: region,
    fileFormat: 'GeoTIFF'
  });
};

// Export each monthly LAI image
monthlyLAI.toList(monthlyLAI.size()).getInfo().forEach(function(image) {
  exportImage(image);
});

1 Answer 1

1

When calling getInfo(), you're turning your images into client-side objects. The will not have EE methods like get().

The trick I use when I need to export a set of images is to create a list of unique properties, using ee.ImageCollection.aggregate_array(). Then I turn this into a client-side list with evaluate() (try to avoid getInfo(), as it freezes the GUI). I do client-side iteration on this list, using each element to filter down the collection to the corresponding image. This image is then finally exported.

There is a second issue with your script. The fileName is a server-side object, but the description in toDrive() must be client-side. In your case, you could use the fileName as your unique property. That way you'll get it converted to a client-side string.

var calculateMonthlyLAI = function(year, month) {
  var startDate = ee.Date.fromYMD(year, month, 1);
  var endDate = startDate.advance(1, 'month');
  var lai = ee.ImageCollection('MODIS/061/MOD15A2H')
    .filterBounds(region)
    .filterDate(startDate, endDate)
    .select('Lai_500m')
    .mean()
    .set('system:time_start', startDate.millis())
    .set('system:time_end', endDate.millis())
    .set('fileName', ee.String('monthly_lai_').cat(startDate.format('YYYY-MM')))
  return lai;
};

...

var exportImage = function(image, fileName) {
  Export.image.toDrive({
    image: image,
    description: fileName,
    folder: 'GEE_exports',
    scale: 500,
    region: region,
    fileFormat: 'GeoTIFF'
  });
};
monthlyLAI.aggregate_array('fileName').evaluate(function (fileNames) {
  fileNames.forEach(function(fileName) {
    var image = monthlyLAI
      .filter(ee.Filter.eq('fileName', fileName))
      .first()
    exportImage(image, fileName);
  })
})

https://code.earthengine.google.com/66eccdffb29344d3b1014e0b62a85cee

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.