0

While exporting images the namings are not setting properly with the image date. How can I solve the problem?

I have attached the associated code:

// Import the MODIS NDVI dataset.
var modisNDVI = ee.ImageCollection("MODIS/006/MOD13Q1")
  .select("NDVI"); // Select the NDVI band.

// Define a region of interest (ROI) or multiple ROIs (e.g., a feature collection).
var roi = ee.Geometry.Rectangle(-120.0, 35.0, -70.0, 45.0); // Example ROI.

// Define a time period.
var startDate = ee.Date('2021-01-01');
var endDate = ee.Date('2021-12-31');

// Function to export all MODIS NDVI images in the collection.
var exportAllNDVI = function(collection, roi, startDate, endDate) {
  var imageList = collection
    .filterDate(startDate, endDate)
    .filterBounds(roi)
    .toList(collection.size());
  
  var imageCount = imageList.size().getInfo();
  
  for (var i = 0; i < imageCount; i++) {
    var image = ee.Image(imageList.get(i));
    var date = ee.Date(image.get('system:time_start')).format("YYYY-MM-dd");
    
    // Export the image to Google Drive with the date as the filename.
    Export.image.toDrive({
      image: image,
      description: 'MODIS_NDVI_' + date,
      folder: 'MODIS_NDVI_Exports',
      region: roi,
      scale: 250, // Adjust the scale as needed.
      crs: 'EPSG:4326',
      maxPixels: 1e13,
      fileFormat: 'GeoTIFF'
    });
  }
};

// Run the exportAllNDVI function for the specified ROI and time period.
exportAllNDVI(modisNDVI, roi, startDate, endDate);
1
  • What is the result name and what are you expecting?
    – Bera
    Commented Sep 18, 2023 at 5:15

1 Answer 1

1

The problem is that date is a server-side ee.String while the exported description must be a client-side string. To resolve this, you will need to use getInfo or evaluate to retrieve the formatted date string before exporting.

Below is a modified version of exportAllNDVI that uses evaluate to asynchronously retrieve each image date. Once the date is available, the callback function will start the export.

var exportAllNDVI = function(collection, roi, startDate, endDate) {
  var imageList = collection
    .filterDate(startDate, endDate)
    .filterBounds(roi)
    .toList(collection.size());
  
  var imageCount = imageList.size().getInfo();
  
  for (var i = 0; i < imageCount; i++) {
    var image = ee.Image(imageList.get(i));
    var date = ee.Date(image.get('system:time_start')).format("YYYY-MM-dd");
    
    date.evaluate(function(date_string) {
      // Export the image to Google Drive with the date as the filename.
      Export.image.toDrive({
        image: image,
        description: 'MODIS_NDVI_' + date_string,
        folder: 'MODIS_NDVI_Exports',
        region: roi,
        scale: 250, // Adjust the scale as needed.
        crs: 'EPSG:4326',
        maxPixels: 1e13,
        fileFormat: 'GeoTIFF'
      });
    });
  }
};

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.