Google Earth Engine – How to Get Dates from an ImageCollection

google-earth-enginejavascriptlist

I try to get all acquisition dates of my ImageCollection in Google Earth Engine Code Editor.

var begin = ee.Date.fromYMD(2017,1,1,'Europe/Berlin');
var end = ee.Date.fromYMD(2018,1,1,'Europe/Berlin');
var orbnr = ee.Number(15);

//Load S1 ImageCollection and filter by Location and Date
var s1_collection = ee.ImageCollection('COPERNICUS/S1_GRD')
  .filterDate(begin,end);

//Filter S1 IC by OrbitNr.
var s1_collection_f = s1_collection
  .filter(ee.Filter.eq('relativeOrbitNumber_stop',orbnr));

//Get Dates with ee.image.date() while mapping over the ImageCollection
var get_date = function(image) {
  var dates = ee.List(image.date())
  return s1_dates.add(dates)};

s1_collection_f.map(get_date);

Last 4 lines of code seems to be wrong. Do i write the function the right way? First time i try to write my own function in javascript….
Do i have to assign a empty list bevore i call s1_dates.add(dates) ? When how can i assign a empty list. Or is it better to use a 1D array instead?

Best Answer

The s1_dates variable isn't defined, but even if it was, you can't modify global variables inside mapped functions. You probably just want this:

dates = s1_collection_f.aggregate_array("system:time_start")

which is equivalent to this:

var dates = s1_collection
    .reduceColumns(ee.Reducer.toList(), ["system:time_start"])
    .get('list')

But, looking ahead, you probably didn't really want 361,000 separate acquisition times. You probably want at least distinct times, and more likely, distinct days (Y-M-D), in which case you need to convert the date somewhere along the way (it's in milliseconds). The easiest way to do that is to map a function over the images, returning a feature for each image containing just the formatted date. Then you can use distinct().

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