0

I am trying to batch download all the monthly images (Blue band, SR_B2) of Landsat 8 but I am getting the following error:

ImageCollection (Error)
Collection query aborted after accumulating over 5000 elements.

In users/fitoprincipe/geetools:batch
collection.size is not a function

I am trying to use @Rodrigo E. Principe's batch.Download.ImageCollection.toDrive() function for this purpose.

My last attempt was:

var batch = require('users/fitoprincipe/geetools:batch')

Map.centerObject(table);

//Create mask function
function maskL8sr(image) {
  // Bits 2, 3 and 5 are water, cloud shadow and cloud, respectively.
  var cloudShadowBitMask = (1 << 3);
  var cloudsBitMask = (1 << 5);
  var waterBitMask = (1 << 2);
  // Get the pixel QA band.
  var qa = image.select('QA_PIXEL');
  // Both flags should be set to zero, indicating clear conditions.
  var mask = qa.bitwiseAnd(cloudShadowBitMask).eq(0)
                 .and(qa.bitwiseAnd(cloudsBitMask).eq(0));
  return image.updateMask(mask);
}

var landsat = ee.ImageCollection('LANDSAT/LC08/C02/T1_L2')
.map(maskL8sr);

var landsat = landsat.select('SR_B2')
print (landsat)

var mean1 = landsat.select('SR_B2').reduce(ee.Reducer.mean()).multiply(0.0000275).add(-0.2).clip(table);

var B2 = mean1.select('SR_B2_mean')

// export collection to google drive
batch.Download.ImageCollection.toDrive(
  B2, 
 'Folder', 
  {crs: 'EPSG: 3309',
  type: 'float',
  scale: 130,
  region: table});

My end goal is to have the entire monthly ImageCollection named as B2_YYYY_MM, where YYYY = year and MM = month. How can I do this?

I have a folder on my GDrive called Folder.

2
  • There is a batch downloader function made by Rodrigo E. Principe. Are you looking for this?
    – Padmanabha
    Sep 12, 2023 at 18:48
  • I am aware of this function but I'm not sure that this the one I am looking for. Or at least I don't know how to modify it so I can download monthly images and spectral indices.
    – Nikos
    Sep 12, 2023 at 18:59

1 Answer 1

1

Instead of using the function made by Rodrigo Principe, I created a for loop to export monthly images for several years. The only problem is that I can't download every month for the entire ImageCollection, so I have to download it (the collection) in chunks. The code is:

var landsat = ee.ImageCollection('LANDSAT/LC08/C02/T1_L2')
Map.centerObject(table);

//Create mask function
function maskL8sr(image) {
  // Bits 2, 3 and 5 are water, cloud shadow and cloud, respectively.
  var cloudShadowBitMask = (1 << 3);
  var cloudsBitMask = (1 << 5);
  var waterBitMask = (1 << 2);
  // Get the pixel QA band.
  var qa = image.select('QA_PIXEL');
  // Both flags should be set to zero, indicating clear conditions.
  var mask = qa.bitwiseAnd(cloudShadowBitMask).eq(0)
                 .and(qa.bitwiseAnd(cloudsBitMask).eq(0));
  return image.updateMask(mask);
}

for (var y = 2019; y < 2020; y++) {
for (var i = 2; i < 3; i++) {
var landsat = landsat.filter(ee.Filter.calendarRange(i, i, 'month'))
                      .filter(ee.Filter.calendarRange(y, y, 'year'))
                      .filterBounds(table)
.map(maskL8sr);
var landsat = landsat.select('SR_B2', 'SR_B3', 'SR_B4', 'SR_B5', 'SR_B6', 'SR_B7', 'ST_B10')
print (landsat)

var mean2 = landsat.select('ST_B10').reduce(ee.Reducer.mean()).multiply(0.00341802).add(149.0).clip(table);

//Create variable for each band
var B2 = landsat.select('SR_B2').reduce(ee.Reducer.mean()).multiply(0.0000275).add(-0.2).clip(table);
var B3 = landsat.select('SR_B3').reduce(ee.Reducer.mean()).multiply(0.0000275).add(-0.2).clip(table);
var B4 = landsat.select('SR_B4').reduce(ee.Reducer.mean()).multiply(0.0000275).add(-0.2).clip(table);
var B5 = landsat.select('SR_B5').reduce(ee.Reducer.mean()).multiply(0.0000275).add(-0.2).clip(table);
var B6 = landsat.select('SR_B6').reduce(ee.Reducer.mean()).multiply(0.0000275).add(-0.2).clip(table);
var B7 = landsat.select('SR_B7').reduce(ee.Reducer.mean()).multiply(0.0000275).add(-0.2).clip(table);
var B10 = landsat.select('ST_B10').reduce(ee.Reducer.mean()).multiply(0.00341802).add(149.0).clip(table);

var B10 = B10.subtract(273.15)

var ndvi = B5.subtract(B4).divide(B5.add(B4)).rename('ndvi');

var ndbi = B6.subtract(B5).divide(B6.add(B5)).rename('ndbi');

var mean1 = landsat.select('SR_B2', 'SR_B3', 'SR_B4', 'SR_B5', 'SR_B6', 'SR_B7').reduce(ee.Reducer.mean()).multiply(0.0000275).add(-0.2).clip(table);

var ebbi = mean1.expression('(SWIR - NIR)/ 10 * sqrt(SWIR + TIRS)',
{
'SWIR':B6,
'NIR':B5,
'TIRS': B10
}).rename('ebbi');

var nbi = mean1.expression('(RED - SWIR1) / (NIR)',
{
  'RED':B4,
  'NIR':B5,
  'SWIR1':B6,
}).rename('nbi');

var gndvi = mean1.expression('(NIR - GREEN) / (NIR + GREEN)',
{
  'NIR':B5,
  'GREEN':B3,
}).rename('gndvi');

var nbai = mean1.expression('((SWIR2 - SWIR1) / GREEN) / ((SWIR2 + SWIR1) / GREEN)',
{
  'SWIR2':B7,
  'SWIR1':B6,
  'GREEN':B2
}).rename('nbai');

var mbai = mean1.expression('(NIR + (1.57 * GREEN) + (2.4 * SWIR1)) / (1 + NIR)',
{
  'NIR':B5,
  'SWIR1':B6,
  'GREEN':B3
}).rename('mbai');

var evi = mean1.expression('2.5 * ((NIR - RED) / (NIR + 6 * RED - 7.5 * BLUE + 1))',
{
  'NIR':B5,
  'RED':B4,
  'BLUE':B2
}).rename('evi');

var bandSpectralList = [B4, B5, B10, evi, ndvi, gndvi, ndbi, ebbi, nbi, nbai, nbi];

var desc = "";

for (var banda = 0; banda < bandSpectralList.length; banda++) {
  
  switch (bandSpectralList[banda]) {
        case B2:
            desc = "blue";
          
            break;
        case B3:
            desc = "green";
           
            break;
        case B4:
            desc = "red";
            
            break;
        case B5:
            desc = "nir";
            
            break;
         case B6:
            desc = "swir1";
            break;
         case B7:
            desc = "swir2";
            break;
         case B10:
            desc = "tirs";
            break;
        case B10:
            desc = "tirs";
            break;
        case B10:
            desc = "tirs";
            break;
        case evi:
            desc = "evi";
            break;
        case ndvi:
            desc = "ndvi";
            break;
            
        case gndvi:
            desc = "gndvi";
            break;
            
        case ndbi:
            desc = "ndbi";
            break;
            
        case ebbi:
            desc = "ebbi";
            break;
        case nbi:
            desc = "nbi";
            break;
         case nbai:
            desc = "nbai";
            break;
        case mbai:
          desc = "mbai";
          break;
        // Add more cases as needed
        default:
            desc = "wrong_name";
            break;
    }
    
var currentBand = bandSpectralList[banda];

Export.image.toDrive({
image: currentBand,
description: desc.toString() + "_" + y + "_" +  ee.Number(i).format('%02d').getInfo(),
scale: 130, //100 for Band10
maxPixels: 1000000000000,
region: table, 
folder: 'Landsat-8',
crs: 'EPSG:3309'
})
}
}
}

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.