8

I am currently working on google earth engine and I have created an image collection with 15 images in it. What i want to do is to create an image for every single image in my collection. I was able to create an image for the first image of my collection by doing :

var Image1 = ee.Image(Collection.first());

Although i would like to do that for the 15 images. Is there any way to do a loop that reads the entire collection and creates images by iterating. The first one would be Image1, and then Image2, Image3 and so on...

4
  • 1
    Did you check the google group for google earth engine? This question has been answered there. Commented Mar 8, 2017 at 21:29
  • ohh i'm sorry, i did not know this was a thing. Thank you very much.
    – Étienne
    Commented Mar 9, 2017 at 18:48
  • There's a lot of group for google earth engine. Can you tell me the exact name of the group you are talking about please. This help would be very much appreciated
    – Étienne
    Commented Mar 9, 2017 at 19:03
  • 1
    [email protected] Commented Mar 9, 2017 at 19:04

4 Answers 4

21

I think instead of accessing the images by this syntax: list[index], you should use the get() method.

e.g.

var listOfImages = myCollection.toList(myCollection.size());
var firstImage = listOfImages.get(0)
var secondImage = listOfImages.get(1)
var lastImage = listOfImages.get(listOfImages.length().subtract(1));

// Type Cast Image for using ee.Image() function
var listOfImages = ee.Image(myCollection.toList(myCollection.size()));
var firstImage = ee.Image(listOfImages.get(0));
var secondImage = ee.Image(listOfImages.get(1));
var lastImage = ee.Image(listOfImages.get(listOfImages.length().subtract(1)));

// Now you can use ee.Image functions 
var B2 = firstImage.select("B2")
1
  • 1
    this is the right answer ...
    – rao
    Commented Oct 31, 2020 at 9:59
8

I think that the easiest way to do it is to transform the image collection into a List.

var listOfImages = myCollection.toList(myCollection.size());

and access each image using indices, like:

var img1 = listOfImages[0];
var img2 = listOfImages[1];
1
  • 2
    This syntax no longer works, hence outdated. Commented Dec 8, 2020 at 13:14
4

It's recommended to use sytax as below:

var listOfImages = myCollection.toList(myCollection.size());
var img1 = listOfImages.get(0);
var img2 = listOfImages.get(1);

It seemed to be correct but not the best way, as in this way, you are not able to use functions of myCollection, eg. img1.select() would be illegal. To avoid this, use syntax as below:

var listOfImages = myCollection.toList(myCollection.size());
var img1 = ee.Image(listOfImages.get(0));
var img2 = ee.Image(listOfImages.get(1));

Then all the functions provided by myCollection can be utilized normally.

1
  • Thanks for reminding this. Commented Dec 8, 2020 at 13:14
2

I think mohit kaushik's suggestion is better. In addition, add ee.Image at first to select this as image format, e.g.,

// Typecast element as earth engine image
var firstImage = ee.Image(listOfImages.get(0));

Your Answer

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

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