8

I started with GoogleEngine recently and I was wondering how I can display all images of my collection as layers. I guess there is a better way than manually copy past / typing:

 var img1 = ee.Image('COPERNICUS/.....);
 var img2 = ....;
 var imgx = ....

 Map.addLayer (img1);
 Map.addLayer(img2);
 Map.addLayer(imgx)....

This questions was already asked in 2017 but I was unable to retrace the link/shared google group...

Kersten
  • 9,899
  • 3
  • 37
  • 59
Andreas Eugster
  • 177
  • 3
  • 8

2 Answers2

9

If you are only displaying a reasonable amount of images you can use a client-side function to add each image as a layer to the map.

Here's an example using 15 images in a polygon you would draw and rename roi.

var s2 = ee.ImageCollection("COPERNICUS/S2")
  .filterBounds(roi)
  .select("B2")
  .limit(15)

function addImage(image) { // display each image in collection
  var id = image.id
  var image = ee.Image(image.id)
  Map.addLayer(image)
}

s2.evaluate(function(s2) {  // use map on client-side
  s2.features.map(addImage)
})
Kersten
  • 9,899
  • 3
  • 37
  • 59
  • the above code gives the output but displays only one band as constant. how to go about it – rao Jan 22 '20 at 12:05
  • 1
    @Kersten, could you please explain why we need the id inside of the addImage() function? – cengstro Feb 07 '22 at 00:36
  • The ID is used to identify what image to display as part of the map function. You could shorten the code to a single line but I prefer readability over brevity for examples. – Kersten Feb 12 '22 at 08:21
  • I'm also puzzled by the id part... are you actually using the id object (i.e. doing image.image.id), or is `id unused in your script? – Matifou Jun 14 '22 at 10:14
7

In addition to the answer of Kersten, you could also add this piece of code to easily view all the images by moving a slider:

var slider = ui.Slider();
slider.onSlide(function(value) {
  var int_value = value * (Map.layers().length() - 1) >> 0;
  Map.layers().get(int_value).setOpacity(1);
  for (var i = int_value + 1; i < Map.layers().length(); i++) {
    Map.layers().get(i).setOpacity(0);
}
});
print(slider);

It does this by changing the opacity of each layer.

Bert Coerver
  • 1,945
  • 10
  • 22