1

I'm trying to export an image from Earth Engine but found that each layer in the .tif export is automatically named using the band index. I want the layer names to be set using band name, rather than band index.

This isn't an issue of the fileNamePrefix argument in Export.image.toDrive(), as I don't need to set prefixes for multiple images; instead I want to export a single raster with layer names defined according to band name. Here is the javaScript code for reproducing my problem, and further down is a quick R script demonstrating the layer naming issue:

// Generate Region of Interest
var ROI = ee.FeatureCollection('TIGER/2016/Counties')
  .filter(ee.Filter.eq('NAME', 'Waldo'));

// Create clipping function
var clipper = function(image){
  return image.clip(ROI);
};

// Create a QA mask function
var masker = function(image){ 
  var mask = image.select('SummaryQA').lte(1);
  return image.updateMask(mask);
};

// Compile the data
var ndvi = ee.ImageCollection('MODIS/006/MOD13Q1')
                  .filter(ee.Filter.date('2001-01-01', '2001-12-31'))
                  .map(clipper)
                  .map(masker)
                  .select('NDVI');
print("MOD13Q1 NDVI Image Collection", ndvi);


// PREPARE DATA FOR EXPORT
// Stacking function by Tyler Erickson
// https://gis.stackexchange.com/a/254778/67264
var stackCollection = function(collection) {
  // Create an initial image.
  var first = ee.Image(collection.first()).select([]);

  // Write a function that appends a band to an image.
  var appendBands = function(image, previous) {
      var dateString = ee.Date(image.get('system:time_start')).format('yyyy-MM-dd');
      return ee.Image(previous).addBands(image.rename(dateString));
  };
  return ee.Image(collection.iterate(appendBands, first));
};

var ndvi_img = stackCollection(ndvi);
print("ndvi_img", ndvi_img);

// Export the data
// See https://developers.google.com/earth-engine/exporting
Export.image.toDrive({
  image: ndvi_img,
  description: 'MOD13Q1_NDVI',
  scale: 1000,
  region: ROI,
  fileFormat: 'GeoTIFF',
  formatOptions: {
    cloudOptimized: true
  }
});

When I load the data and check layer names in R...

myraster = raster::stack("MOD13Q1_NDVI.tif")
names(myraster)

...the layer names are "MOD13Q1_NDVI.1", "MOD13Q1_NDVI.2", "MOD13Q1_NDVI.3", etc. Instead, names(myraster) should return "2001-01-01", "2001-01-17", "2001-02-02", etc.

Your Answer

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

Browse other questions tagged or ask your own question.