2

I want to generate a collection of mosaicked images from Landsat 5 over multiple years. I have checked that the individual images are not null but on returning the mosaic in the code below throws the error:

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

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();      

  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.
Kersten
  • 9,899
  • 3
  • 37
  • 59
shahryar
  • 694
  • 7
  • 21

2 Answers2

5

The following code creates a list of mosaic images, where each mosaic image is constructed from images of a specified time interval. Within each time interval, the "least cloudy pixel", determined by ranking a vegetation index (NDVI), is returned.

This code should work anywhere and for any time that the input data (Landsat 5) is available.

var Date_Start = ee.Date('2000-05-01');
var Date_End = ee.Date('2007-12-01');
var num_months_in_interval = 3;
var viz_params = {bands:"B3,B2,B1", min:0, max:0.3};
var images_to_display = 5;

// 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, num_months_in_interval);
var make_datelist = function(n) {
  return Date_Start.advance(n,'month');
};
dates = dates.map(make_datelist);

function addLandsat5NDVI(image) {
  var ndvi = image.normalizedDifference(['B4', 'B3']).rename('NDVI');
  return image.addBands(ndvi);
}

var CreateMosaic = function(d1) {
  var start = ee.Date(d1);
  var end = ee.Date(d1).advance(num_months_in_interval, 'month');
  var date_range = ee.DateRange(start, end);
  var name = start.format('YYYY-MM').cat(' to ').cat(end.format('YYYY-MM'));
  return ee.ImageCollection('LANDSAT/LT5_L1T_TOA') 
    .filterDate(date_range)
    .map(addLandsat5NDVI)
    .qualityMosaic('NDVI')
    .set({name: name});
};
var list_of_images = dates.map(CreateMosaic);

// Add a few monthly mosaics to the map.
for (var i=0; i<images_to_display; i++) {
  var month_mosaic = ee.Image(list_of_images.get(i));
  var label = ee.String(month_mosaic.get('name')).getInfo();
  Map.addLayer(month_mosaic, viz_params, label);
}
Tyler Erickson
  • 5,401
  • 17
  • 26
  • Thanks Tyler but I am specifically looking to solve the problem of returning the mosaic of 2 images. This actually is part of a bigger code where I need the mosaic for another purpose. – shahryar Jan 28 '18 at 19:36
3

I was able to solve the issue (with the help of Google Earth Developer Group's folks). Use var mosaic = ee.Image(S1).blend(ee.Image(S2)) to mosaic instead of mosaic() and make sure the dates have non-null images.

Hope this helps to future readers.

shahryar
  • 694
  • 7
  • 21
  • does this code enables me to create a mosaic from 3 independent images saved each in a different variable and giving the mean pixel value for overlapping areas between images? If not, how could this be done? ( an equivalen to ArcGIs mosaic with mean for overlapping areas.) – Gab Sep 05 '20 at 15:04