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...

2
  • Displaying a lot of layers simultaneously would take a lot of time. May 30, 2018 at 11:05
  • My images are clipped, and since I can manually display 15 images, I guess "the time it takes" is not the problem... I',m just looking for an auto-way May 30, 2018 at 15:49

2 Answers 2

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)
})
4
  • the above code gives the output but displays only one band as constant. how to go about it
    – rao
    Jan 22, 2020 at 12:05
  • 1
    @Kersten, could you please explain why we need the id inside of the addImage() function?
    – cengstro
    Feb 7, 2022 at 0: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, 2022 at 8: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, 2022 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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.