I need to view imagery from a collection as separate layers. Since I have to do this for several sites and the number of images in the collection therefore varies, I tried to apply a for loop to solve this.
My working code without for loop looks like this:
// Load a landsat image and select three bands for Mount Spurr
// HSV-based Pan-Sharpening of Landsat 7 TOA images.
var panSharpenL8 = function(image) {
var rgb = image.select('B3', 'B2', 'B1');
var pan = image.select('B8');
// Convert to HSV, swap in the pan band, and convert back to RGB.
var huesat = rgb.rgbToHsv().select('hue', 'saturation');
var upres = ee.Image.cat(huesat, pan).hsvToRgb();
return image.addBands(upres);
};
var dataset = ee.ImageCollection('LANDSAT/LE07/C01/T1_TOA')
.filterDate('2004-04-01', '2006-06-30')
.sort('system:time_start')
.map(panSharpenL8)
// short form
var Image_filtered = dataset.filterBounds(geometry_Mount_Spurr)
.filterMetadata('CLOUD_COVER','less_than',10)
print(Image_filtered.size())
var listOfImages = Image_filtered.toList(Image_filtered.size());
print('List:',listOfImages);
var img1 = ee.Image(listOfImages.get(0));
var img2 = ee.Image(listOfImages.get(1));
var img3 = ee.Image(listOfImages.get(2));
var img4 = ee.Image(listOfImages.get(3));
var img5 = ee.Image(listOfImages.get(4));
var img6 = ee.Image(listOfImages.get(5));
var img7 = ee.Image(listOfImages.get(6));
var img8 = ee.Image(listOfImages.get(7));
var img9 = ee.Image(listOfImages.get(8));
var img10 = ee.Image(listOfImages.get(9));
var img11 = ee.Image(listOfImages.get(10));
var img12 = ee.Image(listOfImages.get(11));
var img13 = ee.Image(listOfImages.get(12));
var img14 = ee.Image(listOfImages.get(13));
var img15 = ee.Image(listOfImages.get(14));
var img16 = ee.Image(listOfImages.get(15));
var img17 = ee.Image(listOfImages.get(16));
var img18 = ee.Image(listOfImages.get(17));
// add pansharpened bands
Map.addLayer(img1, {bands:['red','green','blue'], max: 0.3}, 'Pansharpened');
Map.addLayer(img2, {bands:['red','green','blue'], max: 0.3}, 'Pansharpened');
Map.addLayer(img3, {bands:['red','green','blue'], max: 0.3}, 'Pansharpened');
Map.addLayer(img4, {bands:['red','green','blue'], max: 0.3}, 'Pansharpened');
Map.addLayer(img5, {bands:['red','green','blue'], max: 0.3}, 'Pansharpened');
Map.addLayer(img6, {bands:['red','green','blue'], max: 0.3}, 'Pansharpened');
Map.addLayer(img7, {bands:['red','green','blue'], max: 0.3}, 'Pansharpened');
Map.addLayer(img8, {bands:['red','green','blue'], max: 0.3}, 'Pansharpened');
Map.addLayer(img9, {bands:['red','green','blue'], max: 0.3}, 'Pansharpened');
Map.addLayer(img10, {bands:['red','green','blue'], max: 0.3}, 'Pansharpened');
Map.addLayer(img11, {bands:['red','green','blue'], max: 0.3}, 'Pansharpened');
Map.addLayer(img12, {bands:['red','green','blue'], max: 0.3}, 'Pansharpened');
Map.addLayer(img13, {bands:['red','green','blue'], max: 0.3}, 'Pansharpened');
Map.addLayer(img14, {bands:['red','green','blue'], max: 0.3}, 'Pansharpened');
Map.addLayer(img15, {bands:['red','green','blue'], max: 0.3}, 'Pansharpened');
Map.addLayer(img16, {bands:['red','green','blue'], max: 0.3}, 'Pansharpened');
Map.addLayer(img17, {bands:['red','green','blue'], max: 0.3}, 'Pansharpened');
Map.addLayer(img18, {bands:['red','green','blue'], max: 0.3}, 'Pansharpened');
//Map.addLayer(Image_filtered, {bands:['red','green','blue'], max: 0.3}, 'Pansharpened');
As you can see, there are many Map.addLayer
expressions. I wish to not copy and paste the lines to add each image to the Map, so I built an ee.List
and looped like this:
var listOfImages = ee.List(Image_filtered);
print('List:',listOfImages);
var len = listOfImages.length;
for (var i=0; i < len; i++) {
var img = ee.Image(listOfImages.get(i));
Map.addLayer(img, {bands:['red','green','blue'], max: 0.3}, 'Pansharpened');
}
However, it does not add layers to the map and does not result in an error message.
How do I solve the problem?