I have created an Image collection in GEE and with the help of a function I have computed NDVI index and mapped it to create another collection with NDVI as a band.

Now I want the create a stacked image with NDVI bands of the entire image collection into one image. So it should be like NDVI_1, NDVI_2 and so on...

How can I do this? I am pasting the code that shows the NDVI collection that I have so far

// Collection of Images 
var collection = ee.ImageCollection([feb1,feb2,Mar2,April1, April2, May1, May2, Jun1,Jun2,
July2, Aug2, Sep1, Sep2,Oct1, Oct2, Nov1, Nov2, Dec1, Dec2 ]);

//Using the following function,NDVI of the entire collection is computed
var indicesS2 = function(scene)
{ var ndvi = scene.normalizedDifference(['B8', 'B4']).rename('NDVI');
  var image = ee.Image()
                .set('system:time_start', ee.Date(scene.get('system:time_start')));
         return image.addBands([ndvi]).clip(Sheikhupura);
var NDVIcollection = collection.map(indicesS2);
print (NDVIcollection, 'NDVI');

3 Answers 3


Here is an example of creating a stacked image, using the ee.ImageCollection.iterate() method.

I also included code to define to define an example region and image collection, so that it is a working example.

// Define a sample Region-of-Interest 
var roi = ee.Geometry.Polygon(
        [[[-109.1, 37.0],
          [-109.1, 36.9],
          [-108.9, 36.9],
          [-108.9, 37.0]]]);

// Define an example collection.
var collection = ee.ImageCollection('COPERNICUS/S2')
                   .filterDate('2016', '2017')
print('collection', collection);
print('Number of images in collection:', collection.size());

// Calculate NDVI.
var calculateNDVI = function(scene) {
  // get a string representation of the date.
  var dateString = ee.Date(scene.get('system:time_start')).format('yyyy-MM-dd');
  var ndvi = scene.normalizedDifference(['B8', 'B4']);
  return ndvi.rename(dateString);
var NDVIcollection = collection.map(calculateNDVI);

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) {
    return ee.Image(previous).addBands(image);
  return ee.Image(collection.iterate(appendBands, first));
var stacked = stackCollection(NDVIcollection);
print('stacked image', stacked);

// Display the first band of the stacked image.
Map.addLayer(stacked.select(0).clip(roi), {min:0, max:0.3}, 'stacked');

Note that the new, better way to do this is with imageCollection.toBands().


I think this code would be useful: https://code.earthengine.google.com/5be5a41a0a7fd12d73c9d3e42b6cf674


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.