How to access elements from a list in Google Earth Engine

Viewed 1338

I am accessing the properties of an image collection in GEE. I have created an image collection and assigned it to the variable "collection". When accessing the date range I used the following syntax

//Create Image Collection
var collection = ee.ImageCollection("LANDSAT/LC08/C01/T1_TOA")
          .filter(ee.Filter.eq("WRS_PATH", 118))
          .filter(ee.Filter.eq("WRS_ROW", 56))
          .filterDate("2020-05-16", "2021-06-17");
Map.addLayer(collection)
Map.centerObject(collection, 9)
print("Collection: ", collection);

//Get date range
var dates = collection.get("date_range");
print ("dates: ", dates)
var daterange = ee.DateRange(dates.get(0), dates.get(1));
print("daterange: ", daterange)

This gives an error "dates.get is not a function". However if I create the "dates" variable with...

var dates = ee.List(collection.get("date_range"));

it all works fine. I don't really understand why this is as the "dates" variable appears to return the same list either way.

Hope someone can explain

2 Answers

I´m having a similar issue. To get the dates I´m using this, maybe it helps you:

// Load MODIS EVI imagery.
var collection = ee.ImageCollection('MODIS/006/MOD13Q1').select('NDVI');
var start_date = ee.Date('2020-12-01')
var end_date = fecha_inicio.advance(90, 'days')


var series = collection.filterDate(start_date , 
end_date).map(function(image) {
   return image.set('system:time_start', image.get('system:time_start'));
 });


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

My problem is that I can´t acces to the elements in the list, but I obtain a list with the dates I need from the collection

Basically Earth Engine doesn't know the type of objects which you get from ee.List and ee.Dictionary. So any time you have an error where it says variable.function is not a function most of the time it is enough to explicitly cast it to the desired type.

In your case casting dates to an ee.List would be enough to make it work.

So in your case this would work:

//Get date range
var dates = ee.List(collection.get("date_range"));
print ("dates: ", dates)
var daterange = ee.DateRange(dates.get(0), dates.get(1));
print("daterange: ", daterange)
Related