5

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.

1
  • If it complains about non-image elements, you probably have non-image elements...can you the give sample output of the function? (console.log it). Also make sure your function is correct. The API docs say you should use ee.ImageCollection.fromImages()
    – Senshi
    Commented Jan 26, 2018 at 8:53

1 Answer 1

7

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.

3
  • Thanks a lot @Tyler. That partially solved the issue. However, on further coding, the same error is showing up when I use 2 Landsat images and return a mosaic of them. I checked that the individual images are not null.The code is in the edited question above. Thanks.
    – shahryar
    Commented Jan 27, 2018 at 17:49
  • Your second code listing is quite different... different image characteristics, includes cloud masking, uses a mosaic reducer, etc. How about starting a new question that describes the bigger picture issue you are trying to solve? Something along the lines of: "How do you generate a collection of Landsat cloud-masked monthly composites?" or whatever question best describes the goal of your second code listing. Commented Jan 28, 2018 at 1:39
  • Ok, I have posted a new question. Hope to get a solution soon. Thanks for your suggestion.
    – shahryar
    Commented Jan 28, 2018 at 1:40

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.