2

I have code that retrieve images from Landsat 5 , calculate NDVI , display the rasters and export it to my drive, giving names to th rasters according to their ordinal number. My problem is that I want the images to get their names according to the date. until now I did it manually but it takes time and also worried me because it seems like it retrieve the images not on chronologically order. I thought that the solution should be creating a list from the dates but I am not sure how to do it.

enter image description here

this is my code-

/**
* Function to mask clouds based on the pixel_qa band of Landsat SR data.
* @param {ee.Image} image Input Landsat SR image
* @return {ee.Image} Cloudmasked Landsat image
*/
var cloudMaskL457 = function(image) {
  var qa = image.select('pixel_qa');
  // If the cloud bit (5) is set and the cloud confidence (7) is high
  // or the cloud shadow bit is set (3), then it's a bad pixel.
  var cloud = qa.bitwiseAnd(1 << 5)
                  .and(qa.bitwiseAnd(1 << 7))
                  .or(qa.bitwiseAnd(1 << 3));
  // Remove edge pixels that don't occur in all bands
  var mask2 = image.mask().reduce(ee.Reducer.min());
  return image.updateMask(cloud.not()).updateMask(mask2);
};

var dataset = ee.ImageCollection('LANDSAT/LT05/C01/T1_SR')
                  .filterDate('1986-01-01', '2011-12-31')
                  .filterBounds(geometry)
                  .map(cloudMaskL457);

var visParams = {
  bands: ['B3', 'B2', 'B1'],
  min: 0,
  max: 3000,
  gamma: 1.4,
};
Map.centerObject(geometry,10);




// Get the number of images.
var count = dataset.size();
print('Count: ',count);
//print(clippedCol);
print(dataset,'dataset');

//clip the dataset according to the geometry
var clippedCol=dataset.map(function(im){ 
  return im.clip(geometry);
});

var listOfImages =(clippedCol.toList(clippedCol.size()));
// var Image1=ee.Image(listOfImages.get(0));
// var Image2=ee.Image(listOfImages.get(1));
// var Image3=ee.Image(listOfImages.get(2));
// var Image4=ee.Image(listOfImages.get(3));
// var Image5=ee.Image(listOfImages.get(5));
// var Image6=ee.Image(listOfImages.get(15));
// var Image7=ee.Image(listOfImages.get(19));
// Map.addLayer(Image1,visParams,'1');
// Map.addLayer(Image2,visParams,'2');
// Map.addLayer(Image3,visParams,'3');
// Map.addLayer(Image4,visParams,'4');
// Map.addLayer(Image5,visParams,'5');
// Map.addLayer(Image6,visParams,'6');
// Map.addLayer(Image7,visParams,'7');


//function to calculate NDVI in LANDSAT4
var addNDVI = function(image) {
  var NDVI = image.normalizedDifference(['B4', 'B3'])
  .rename('NDVI')
  .copyProperties(image,['system:time_start']);
  return image.addBands(NDVI);

};

//NDWI to the clipped image collection
var withNDVI = clippedCol.map(addNDVI).select('NDVI');

var NDVIcolor = {
  min: 0,
  max:1,
  palette: ['FFFFFF', 'CE7E45', 'DF923D', 'F1B555', 'FCD163', '99B718', '74A901',
    '66A000', '529400', '3E8601', '207401', '056201', '004C00', '023B01',
    '012E01', '011D01', '011301'],
};


var listOfImages =(withNDVI.toList(withNDVI.size()));



print(ui.Chart.image.series(withNDVI, geometry, ee.Reducer.mean(), 30));
print(ui.Chart.image.series(withNDVI, geometry, ee.Reducer.max(), 30));
print(ui.Chart.image.series(withNDVI, geometry, ee.Reducer.min(), 30));



var listOfNumbers = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27];



for (var i in listOfNumbers) {
  var image = ee.Image(listOfImages.get(listOfNumbers[i]));
  var toexport=image.visualize(NDVIcolor).addBands(image);


  // display and export
  Map.addLayer(image, NDVIcolor, i);
  Export.image.toDrive({
  image: toexport.toFloat(),
  description: i,
  scale:30,
  crs:'EPSG:4326',
  maxPixels:1310361348,
  region:geometry.geometry().bounds()

});


 }

My end goal is to: 1. understand why is the order not chronical 2. give each image name based on the acquisition date

1

Ordering is alphabetical, which is in case of images from 1 path/row combination equal to the acquisition date. Because you are selecting images from different path row combinations the ordering is not truly chronological and you should define the ordering of your image collection.

With respect to your second question, you'll have to use getInfo's probably:

/**
* Function to mask clouds based on the pixel_qa band of Landsat SR data.
* @param {ee.Image} image Input Landsat SR image
* @return {ee.Image} Cloudmasked Landsat image
*/
var cloudMaskL457 = function(image) {
  var qa = image.select('pixel_qa');
  // If the cloud bit (5) is set and the cloud confidence (7) is high
  // or the cloud shadow bit is set (3), then it's a bad pixel.
  var cloud = qa.bitwiseAnd(1 << 5)
                  .and(qa.bitwiseAnd(1 << 7))
                  .or(qa.bitwiseAnd(1 << 3));
  // Remove edge pixels that don't occur in all bands
  var mask2 = image.mask().reduce(ee.Reducer.min());
  return image.updateMask(cloud.not()).updateMask(mask2);
};

var dataset = ee.ImageCollection('LANDSAT/LT05/C01/T1_SR')
                  .filterDate('1986-01-01', '2011-12-31')
                  .filterBounds(geometry)
                  .map(cloudMaskL457);

var visParams = {
  bands: ['B3', 'B2', 'B1'],
  min: 0,
  max: 3000,
  gamma: 1.4,
};
Map.centerObject(geometry,10);




// Get the number of images.
var count = dataset.size();
print('Count: ',count);
//print(clippedCol);
print(dataset,'dataset');

//clip the dataset according to the geometry
var clippedCol=dataset.map(function(im){ 
  return im.clip(geometry);
});

var listOfImages =(clippedCol.toList(clippedCol.size()));
// var Image1=ee.Image(listOfImages.get(0));
// var Image2=ee.Image(listOfImages.get(1));
// var Image3=ee.Image(listOfImages.get(2));
// var Image4=ee.Image(listOfImages.get(3));
// var Image5=ee.Image(listOfImages.get(5));
// var Image6=ee.Image(listOfImages.get(15));
// var Image7=ee.Image(listOfImages.get(19));
// Map.addLayer(Image1,visParams,'1');
// Map.addLayer(Image2,visParams,'2');
// Map.addLayer(Image3,visParams,'3');
// Map.addLayer(Image4,visParams,'4');
// Map.addLayer(Image5,visParams,'5');
// Map.addLayer(Image6,visParams,'6');
// Map.addLayer(Image7,visParams,'7');


//function to calculate NDVI in LANDSAT4
var addNDVI = function(image) {
  var NDVI = image.normalizedDifference(['B4', 'B3'])
  .rename('NDVI')
  .copyProperties(image,['system:time_start']);
  return image.addBands(NDVI);

};

//NDWI to the clipped image collection
var withNDVI = clippedCol.map(addNDVI).select('NDVI');

var NDVIcolor = {
  min: 0,
  max:1,
  palette: ['FFFFFF', 'CE7E45', 'DF923D', 'F1B555', 'FCD163', '99B718', '74A901',
    '66A000', '529400', '3E8601', '207401', '056201', '004C00', '023B01',
    '012E01', '011D01', '011301'],
};


var listOfImages =(withNDVI.toList(withNDVI.size()));
var count = withNDVI.size()

// print(ui.Chart.image.series(withNDVI, geometry, ee.Reducer.mean(), 30));
// print(ui.Chart.image.series(withNDVI, geometry, ee.Reducer.max(), 30));
// print(ui.Chart.image.series(withNDVI, geometry, ee.Reducer.min(), 30));



ee.List.sequence(0,ee.Number(count.subtract(1))).getInfo()
.map(function(i){
  // print(img)

  var image = ee.Image(listOfImages.get(i))
  // print(listOfImages.get(i))
  var toexport=image.visualize(NDVIcolor).addBands(image);



  var name= 'NDVI_'+image.id().getInfo()
  Map.addLayer(image, NDVIcolor, name)

  Export.image.toDrive({
    image: toexport.toFloat(),
    description: name,
    scale:30,
    crs:'EPSG:4326',
    maxPixels:1e13,
    region:geometry});
})

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.