0

I have tried something like this:

var empty_list = ee.List([])

However, when adding items to this list, it returns an empty list regardless.

Here is a code snippet. Note that "col_list" is a variable representing an image collection.

var list = ee.List([]);

for(var i = 0; i < 5; i++){

  var img = ee.Image(col_list.get(i));

  var dictionary = ee.Dictionary({
    coords: img.geometry().getInfo()["coordinates"],
    landsat_id: img.get('LANDSAT_ID'),
    satellite: img.get('SATELLITE'),
    sensing_time: img.get('SENSING_TIME'),
    system_index: img.get('system:index')
  });

  var feature = ee.Feature(null, dictionary);
  var list = list.add(feature);
}

This definitely looks wrong but I am not sure how else to do so.

0

Although Nishanta answer is correct if you think in JavaScript, I would like to show you how to do it in the Earth Engine way, which I think is better for this.

var list = col_list.iterate(function(img, first){
  // cast variables
  img = ee.Image(img)
  first = ee.List(first)
  // same as before but without `getInfo`
  var dictionary = ee.Dictionary({
    coords: img.geometry().coordinates(),
    landsat_id: img.get('LANDSAT_ID'),
    satellite: img.get('SATELLITE'),
    sensing_time: img.get('SENSING_TIME'),
    system_index: img.get('system:index')
  });

  var feature = ee.Feature(null, dictionary);
  return first.add(feature)
}, ee.List([]))

// cast the result to ee.List
list = ee.List(list)

print(list)
1

The problem is that you are redefining the variable list within your loop so the scope of that variable extends only within the loop and the list variable outside the loop is never getting updated. It should be fixed by removing the "var" in front of list WITHIN the loop. (line 12 in code below)

var list = ee.List([]);
for(var i = 0; i < 5; i++){
  var img = ee.Image(col_list.get(i));
  var dictionary = ee.Dictionary({
    coords: img.geometry().getInfo()["coordinates"],
    landsat_id: img.get('LANDSAT_ID'),
    satellite: img.get('SATELLITE'),
    sensing_time: img.get('SENSING_TIME'),
    system_index: img.get('system:index')
  });
  var feature = ee.Feature(null, dictionary);
  list = list.add(feature);
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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