Google Earth Engine – Stacking ImageCollection in a Multi-Band Image and Naming Fields After Date

google-earth-engine

In Earth Engine how can the bands generated through a stacking of an ImageCollection to a a multi-band image be named after the corresponding month-year?

I use the following code to stack the ImageCollection, but the bands are simply named sequentially with integer numbers.

def stackCollection(collection):
first = ee.Image(collection.first()).select([])
def appendBands(image, previous):
    return ee.Image(previous).addBands(image)
return ee.Image(collection.iterate(appendBands, first))

stacked = stackCollection(imageCollection)

Best Answer

You could add single band images to one image using the function toBands(), which renames the bands automatically for Landsat and Sentinel-2 images to their ID (which includes the date taken). See: Making stack from many images in Google Earth Engine?

If you would like to add the date in your function, you will have to look up in the image properties where the date is set. For Landsat images, it will be:

var dateString = ee.Date(image.get('system:time_start')).format('yyyy-MM-dd');

For Sentinel-2 images it will for example be:

var dateString = ee.Date(image.get('GENERATION_TIME')).format('yyyy-MM-dd');

You can then rename the bands in your function by changing 'YOUR_BAND_NAME':

var image = image.select('YOUR_BAND_NAME').rename(dateString)