I would like to add a band to each image in an image collection ('median_ndvi' in the linked code). The band should be a sequence of ee.Image() constants from 1 to 12.


1 Answer 1


You can make your image collection way more convenient by looping over the years you are interested in. While doing so, you can also add a constant band to each of the images.

// get image composite for years between: 
var startyear = 1983
var endyear   = 2018
// composite of how many years (1 = one composite per year, 2 = one composite every 2 years etc.)
var step = 3

// make image composites
var loopsteps = ee.List.sequence(startyear,endyear,step)
var composites = ee.ImageCollection.fromImages(loopsteps.map(function(x){
  var endYear = ee.Number(x).add(ee.Number(step)).subtract(1);
  var sdate = ee.Date.fromYMD(x,1,1);                // startdate of current loop
  var edate = ee.Date.fromYMD(endYear,12,31);        // enddate of current loop
  var index = loopsteps.indexOf(x).add(1);
  var constant = ee.Image.constant(index);
  var comp = overall_collection.filterDate(sdate,edate).reduce(reducer).addBands(constant)
                                    .set('system:time_start', sdate).set('system:time_end', edate)
                                    .set("system:id", index);
  return comp;


Note that for further calculations you might have to cast the different types of bands to a similar bandtype. Most likely, you will have to cast the constant image to a floating number using


You can then get only the bands you are interested in using:

var median_ndvi = composites.select(["ndvi_median", "constant"]);


  • Whilst this is interesting code, it isn't what I was getting at. I want to add the constant band after 'median_ndvi' has been split out from the collection Commented Feb 1, 2019 at 15:06
  • See the last part of the answer. use select(['bandName1', 'bandName2', ...]) to select the bands you want
    – Kuik
    Commented Feb 1, 2019 at 16:00
  • That still isn't what I want to do Commented Feb 4, 2019 at 9:57
  • code.earthengine.google.com/4f6ca36b9eefca6014bed3df0d1ab391 Does this do what you expected? Otherwise, I am unable to interpret your question correctly from your description.
    – Kuik
    Commented Feb 4, 2019 at 10:22

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.