3

Using the code below, I've computed 20 ndvi images for modis data.

How can I stack and save them in a single file?

var mod13 = ee.ImageCollection("MODIS/006/MOD13Q1")
.filterBounds(geometry)
.filterDate("2000-01-01","2001-01-01")
.select("NDVI");

var mod13ndvi = mod13.map( function(img){
  return img.multiply(0.0001)
  .copyProperties(img,['system:time_start','system:time_end']);
});

print(mod13ndvi);

2 Answers 2

8

You just need the function 'toBands()' and apply that on the image collection. Unfortunately, the function cannot deal with similar band names (at least, for your MODIS collection: it works fine for landsat and Sentinel), so you will need to change the band names. For simplicity, I changed the band names to the date the image was acquired.

Update: bandnames are now renamed to only their date, eliminating the 'null_' the function toBands() appends to it when image number lacks.

// Define a random geometry
var geometry = /* color: #0b4a8b */ee.Geometry.Polygon(
    [[[-111.6384952013949, 33.80963070262341],
      [-111.7153994982699, 32.92883163730542],
      [-109.0017764513949, 33.526181694205995]]]);

// make the image collection
var mod13 = ee.ImageCollection("MODIS/006/MOD13Q1") .filterBounds(geometry) 
.filterDate("2000-01-01","2001-01-01") .select("NDVI");
print(mod13);

// change the name of the NDVI band to the image ID (date it is acquired)
var mod13ndvi = mod13.map( function(img){ 
  var id  = img.id()
  img = img.select('NDVI').rename(id);
return img.multiply(0.0001).copyProperties(img,['system:time_start','system:time_end']); });
print(mod13ndvi);

// Apply the function toBands() on the image collection to set all bands into one image
var multiband = mod13ndvi.toBands();
print(multiband)

// Reset the bandnames
var names = multiband.bandNames();
// rename the bandnames 
var newNames = names.map(function(name){
  var ind = names.indexOf(name);
  return ee.String(names.get(ind)).slice(5);
});
var multiband = multiband.rename(newNames);
print(multiband);

https://code.earthengine.google.com/48569dcfe14420f0ed8f6729e3ec7d15

2
  • If I want to save all bands with correspond date (as band name), what should I do? could you help me, please? Commented Nov 1, 2018 at 20:10
  • You could replace id to make it more convenient by: var dateString = ee.Date(img.get('system:time_start')).format('yyyy-MM-dd'); But it returns the exactly the same answers as img.id();
    – Kuik
    Commented Nov 2, 2018 at 9:40
-1

thanks for your response , I've rewrite code like this:

var mod13 = ee.ImageCollection("MODIS/006/MOD13Q1")
.filterBounds(geometry)
.filterDate("2000-01-01","2001-01-01")
.select("NDVI");

var mod13ndvi = mod13.map( function(img){
  return img.multiply(0.0001)
  .copyProperties(img,['system:time_start','system:time_end']);
});

print(mod13ndvi);

var multiband = mod13ndvi.iterate(function(image, result) {
  return ee.Image(result).addBands(image);
});


Export.image.toDrive({
  image : multiband,
  description : "stack_test_ex",
  scale : 250,
  region : geometry
})

but it doesn't save multi band and show this error:

Error: Image.addBands, argument 'dstImg': Invalid type. Expected: Image. Actual: ImageCollection.

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.