1

I'm very new to GEE and programming in general. I have this code that filters Landsat 5 imagery, creates a collection of NDVI layers, clips the NDVI layers, and creates mosaics of two NDVI layers of the same date. I would like to keep the original name of the image (for example, LANDSAT/LT05/C02/T1_L2/LT05_039037_20010106) but when the images pass through the iterate function they lose all of their properties.

Here is the first part of the code:

// filtrar imagenes landsat 5
var dataset = ee.ImageCollection('LANDSAT/LT05/C02/T1_L2').filterDate(start, finish).filter(ee.Filter.eq('WRS_PATH',39)).filter(ee.Filter.rangeContains('WRS_ROW', 37, 38));

// NDVI
var addNDVI = function(image) {
  var ndvi = image.normalizedDifference(['SR_B4', 'SR_B3']).rename('NDVI');
  return image.addBands(ndvi);
};
var ndvi = dataset.map(addNDVI);
var ndvi = ndvi.select(['NDVI']);

// Clip al area de estudio
var clipToCol = function(image){
  return image.clip(DR014);
};
var clip = ndvi.map(clipToCol);

This is the part of the code where the images lose all of their properties:

// mosaico NDVI por fecha
var diff = finish.difference(start, 'day');

var range = ee.List.sequence(0, diff.subtract(1)).map(function(day){
  return start.advance(day,'day')});

var day_mosaics = function(date, newlist) {
  date = ee.Date(date);
  newlist = ee.List(newlist);
  var filtered = clip.filterDate(date, date.advance(1,'day'));
  var image = ee.Image(filtered.mosaic()).set({date:date});
  return ee.List(ee.Algorithms.If(filtered.size(), newlist.add(image), newlist));
};
var mosaico = ee.ImageCollection(ee.List(range.iterate(day_mosaics, ee.List([]))));

1 Answer 1

0

First, you are losing all of your properties in your iterate function. What you want to do is tell it explicitly to keep the date property, like this:

var image = ee.Image(filtered.mosaic()).set({date:date});

Then, in the same loop where you are clipping your image collection, you can use that date to rename your images (well the image band):

var clipToCol = function(image){
  var date = ee.Date(image.get('date'))
  return image.clip(DR014).rename(date.format('YYYY-MM-dd'));
};

I chose that format arbitrarily, but you can choose other formats.

On a separate note, clipping image collections, especially large ones is generally discouraged. It wastes computation time and is not necessary. I suggest you read this Coding Best Practices guide for GEE.

2
  • Also, use .filterBounds(DR014) instead of filtering by 'WRS_PATH' and 'WRS_ROW', it'll save you the effort of finding the path and row IDs.
    – M. Nicolas
    Jun 28, 2022 at 22:13
  • Thank you very much. I did what you suggested and the bands were renamed, however, now I can't visualize them on the map and this error appears (x21): Layer 2: Tile error: Expected a homogeneous image collection, but an image with incompatible bands was encountered: First image type: 1 bands ([2001-01-06]). Current image type: 1 bands ([2001-01-22]). Image ID: 1 Some bands might require explicit casts. Here's the "corrected" code: code.earthengine.google.com/3cdc0acdbd660a8a17a7e09d31a1413e
    – Deus
    Jun 29, 2022 at 0:49

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.