1

How can I add all the images contained in a list in GEE to a map?

I am trying to learn how to iterate (instead of manually acquiring images for each year) and have learned that there is no for loops in GEE, so I used the map function to iterate over the 10 years I wish to acquire data for.

My script is successfully creating a list of images, but I do not know how to do anything with them. If I try to add them to the map within the function, I get an error saying that says:

"Layer error: A mapped function's arguments cannot be used in client-side operations"

My reading suggests that I can't use Map.addlayer within the mapfunction, but I cannot figure out how to apply it to then list outside of the map function.

Here is my code (I'm new to this):

//First, we will establish the basics. What, Where, and When
var CollectionString = "LANDSAT/LC08/C02/T1_L2"//Indicates that we are using LANDSAT8, Collection 2, Tier 1, Level 1.
var StudyArea = ee.FeatureCollection("users/Matechik4/ACF"); //Here, you can enter the path to a study area of interest that you have uploaded to GEE.
Map.centerObject(StudyArea)// centers the map on the study area.
var year_list = ee.List.sequence(2013, 2022)//Creates a sequence of years starting and ending with the years defined here.
// Now, we will create a function 
var yearly_data = function Get_Data (year) { 
  year = ee.Number(year)//converts the year to a number format.
  var start_date = ee.Date.fromYMD(year,3,1)//creates start date within each year. The year value is taken from the range of years previously entered, the month is the first number, and the day is the second number.
  var end_date = ee.Date.fromYMD(year,5,31)  //creates end date within each year. The year value is taken from the range of years previously entered, the month is the first number, and the day is the second number.

  var CloudMask = require('users/Matechik4/DEVELOP:Cloud_Mask').LandsatC2_CloudMask; //uses another script, called cloud mask, to filter out pixels that represent clouds.

  var collection = ee.ImageCollection(CollectionString)
    .filterBounds(StudyArea) //limits images to study area.
    .filterDate(start_date, end_date) //filter the iamges within each year by the start and end dates


  var medianCollection = collection.median() 
  var cMedianCollection = medianCollection.clip(StudyArea) //clips collection of median raster values to study area.

  return(cMedianCollection)

}

//Iterate function over list using .map
var images = ee.Image(year_list.map(yearly_data))
print(images)// Note that this line does not need to be in the final script, but I include it here for troubleshooting purposes. Printing to the console shows that the script does create a list with 10 images.

1 Answer 1

1

Well, I don't know how can you add all the images contained in a list in GEE to a map. But I know that yes there are for loops in Earth Engine. You can use them in client-side functions (map() is a server-side function).

For instance, you can print the images one at a time:

var print_images = function(images) {
  var imagesList = ee.List(images);
  var n = 10;
  for (var i = 0; i < n; i++) {
    var img = ee.Image(imagesList.get(i));
    print(img);
  }
};

print_images(images);
2
  • Thanks. I'm going to give this a try. I was in a class and they said GEE didn't support loops. I wonder where that came from.
    – Matechik4
    Commented Jun 2, 2022 at 21:07
  • 1
    You are welcome. That came from the documentation: "The use of for-loops is discouraged in Earth Engine" (developers.google.com/earth-engine/tutorials/…). It should explain the difference between iterating on server-side and client-side functions in that section. Commented Jun 3, 2022 at 2:01

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.