Google Earth Engine – Mapping Over List of Dates

google-earth-enginejavascript

My function fnc returns as Image but on using the ee.ImageCollection(dates.map(fnc)), it returns an error ImageCollection (Error)
ImageCollection.fromImages: Attempt to create an ImageCollection with non-image elements.
in the code below.

var Date_Start = ee.Date('2015-05-01');
var Date_End = ee.Date('2017-12-01');
var Date_window = ee.Number(30);

// Create list of dates for time series
var n_months = Date_End.difference(Date_Start,'month').round();
var dates = ee.List.sequence(0,n_months,1);
var make_datelist = function(n) {
  return Date_Start.advance(n,'month')
}
dates = dates.map(make_datelist);

var fnc = function(d1) {
  var start = ee.Date(d1);
  var end = ee.Date(d1).advance(1,'month');
  var date_range = ee.DateRange(start,end);
  var S1 = ee.ImageCollection('COPERNICUS/S1_GRD')
    .filterDate(date_range)
    .filter(ee.Filter.listContains('transmitterReceiverPolarisation', 'VV'))
    .filter(ee.Filter.eq('instrumentMode', 'IW'))

  return(S1.first())
}

var mt = ee.ImageCollection(dates.map(fnc));
print(mt)

How can I get the image collection back from the mapped function?

EDIT
After trying further, I am now getting a similar error while mosaicing two images (this time from Landsat 5) and then returning the mosaic. Code is below:

var p1 = ee.Geometry.Point([103.521,13.028]);
var p2 = ee.Geometry.Point([105.622,13.050]);

var Date_Start = ee.Date('2000-05-01');
var Date_End = ee.Date('2007-12-01');
var Date_window = ee.Number(30);

// Create list of dates for time series
var n_months = Date_End.difference(Date_Start,'month').round();
var dates = ee.List.sequence(0,n_months,1);
var make_datelist = function(n) {
  return Date_Start.advance(n,'month');
};
dates = dates.map(make_datelist);
var fnc = function(d1) {
  var start = ee.Date(d1);
  var end = ee.Date(d1).advance(14,'month');
  var date_range = ee.DateRange(start,end);
  var S1 = ee.ImageCollection('LANDSAT/LT5_L1T_TOA') 
    .filterDate(date_range)
    .sort('CLOUD_COVER')
    .filterBounds(p1).first()

  var S2 = ee.ImageCollection('LANDSAT/LT5_L1T_TOA')
    .filterDate(date_range)
    .sort('CLOUD_COVER')
    .filterBounds(p2).first()

  var mosaic = ee.ImageCollection([ee.Image(S1), ee.Image(S2)]).mosaic();
  var image = mosaic.clip(TSL);


  return mosaic
};

var list_of_images = dates.map(fnc);
print('list_of_images', list_of_images);
var mt = ee.ImageCollection(list_of_images);
print(mt);
Map.addLayer(mt, {}, 'mt');

ERROR:
List (Error)
ImageCollection.fromImages: Attempt to create an ImageCollection with non-image elements.

Best Answer

The error

ImageCollection.fromImages: Attempt to create an ImageCollection with non-image elements.

will occur if you are attempting to create an image collection from a list of null objects. In your code, this will occur if you attempt to take the .first() element of an empty month-filtered S1 collection. The following code demonstrates this, by filtering the monthly collections to a region in the ocean where no Sentinel-1 images have been collected:

var geometry = ee.Geometry.Point([-136.14, 35.17]);
var Date_Start = ee.Date('2015-05-01');
var Date_End = ee.Date('2017-12-01');
var Date_window = ee.Number(30);

// Create list of dates for time series
var n_months = Date_End.difference(Date_Start,'month').round();
var dates = ee.List.sequence(0,n_months,1);
var make_datelist = function(n) {
  return Date_Start.advance(n,'month');
};
dates = dates.map(make_datelist);

var fnc = function(d1) {
  var start = ee.Date(d1);
  var end = ee.Date(d1).advance(1,'month');
  var date_range = ee.DateRange(start,end);
  var S1 = ee.ImageCollection('COPERNICUS/S1_GRD')
    .filterDate(date_range)
    .filter(ee.Filter.listContains('transmitterReceiverPolarisation', 'VV'))
    .filter(ee.Filter.eq('instrumentMode', 'IW'))
    .filterBounds(geometry);
  return(S1.first());
};

var list_of_images = dates.map(fnc);
print('list_of_images', list_of_images);
var mt = ee.ImageCollection(list_of_images);
print(mt);
Map.addLayer(mt, {}, 'mt');

If you instead you modify the filters so that at least one of the monthly collections has an image, the error will be avoided and your object mt will be a valid image collection.

Related Question