1

I have implemented the answer of Noel Gorelick to the question: Earth Engine get dates from imageCollection It is the following code:

var dates = s1_collection_f
    .map(function(image) {
      return ee.Feature(null, {'date': image.date().format('YYYY-MM-dd')})
    })
    .distinct('date')
    .aggregate_array('date')

It successfully returned a list of dates in the format 'YYYY-MM-dd', but I cannot read that list. I need to get every single date to be joined in the layers names. It seems to be weird, but I can't loop through the elements of the list. I have tried the following:

  1. The console returns for each element of the list:

    for (var index in dates) { print(dates[index]); }

  2. Nothing is returned:

    for (var i=0; i< dates.length(); i++) { print(dates[i]); }

How can I loop through the list and get each date?

1
  • 1
    The use of for-loops is discouraged in Earth Engine. You should use map method with dates list instead.
    – xunilk
    Jul 19, 2022 at 14:42

1 Answer 1

0

Following code is an example for joining layers names to every single date, in an arbitrarily selected ('COPERNICUS/S1_GRD') Image Collection, by using map instead a for loop.

var pt = ee.Geometry.Point(-104.18, 40.47); 

//Load S1 ImageCollection and filter by Location and Date
var s1_collection = ee.ImageCollection('COPERNICUS/S1_GRD')
  .filterDate('2017-01-01', '2018-01-01')
  .filter(ee.Filter.bounds(pt));

var s1_collection_lst = s1_collection.toList(s1_collection.size());

var names = s1_collection_lst.map(function (img)  {
  
  return ee.Image(img).id().slice(0, 17);
  
});

print(names);

var dates = s1_collection.aggregate_array("system:time_start");

dates = dates.map(function (ele) {
  
  return ee.Date(ele).format().slice(0,10);
  
});

var paired = names.zip(dates);

var join_name_date = paired.map(function (ele) {
  
  return ee.String(ee.List(ele).get(0)).cat(ee.List(ele).get(1));
  
});

print(join_name_date);

Script can be ran in following link.

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.