I'm using Landsat imagery in Google Earth Engine and am attempting to convert an image collection to a multiband image. However, before doing so I do two things:
- I mosaic the images by date so that there is only one Landsat image per day
- I sort the image collection by date (system:id) so that it goes from oldest to newest rather than being sorted by the Landsat satellite.
This works correctly, but when I use .toBands to convert this into a multiband image the output is gibberish. I need an output multiband image where each band is the correct date for that image in the mosaicked and sorted image collection.
I've included pictures of the problem I'm having and the relevant code at the bottom.
In my example, the Landsat collection contains 560 images.
After mosaicking I have 352 images, but they are not in temporal order:
Sorting correctly sorts the image collection by date:
But when I use .toBands to convert the sorted image collection into a single multiband image, the output is gibberish:
//mosaic images by date
function mosaicByDate(imcol){
var imlist = imcol.toList(imcol.size())
print(imlist)
var unique_dates = imlist.map(function(im){
return ee.Image(im).date().format("YYYY-MM-dd")
}).distinct()
var mosaic_imlist = unique_dates.map(function(d){
d = ee.Date(d)
var im = imcol
.filterDate(d, d.advance(1, "day"))
.mosaic()
return im.set(
"system:time_start", d.millis(),
"system:id", d.format("YYYYMMdd"))
})
return ee.ImageCollection(mosaic_imlist)
}
//apply the mosaic to the Landsat image collections
var ic_NDVI = mosaicByDate(NDVIuse)
print('mosaicked',ic_NDVI)
//Sort by date. The image collection is now sorted by date instead of by Landsat satellite
var ic_NDVI_sorted = ic_NDVI.sort('system:id')
print('sorted',ic_NDVI_sorted)
//Convert the image collection into a single multiband image. The output is gibberish.
var merged_LST = ic_NDVI_sorted.toBands();
print('Sorted multiband',merged_NDVI)