0

I have Image collection that I converted into list and I want to take specific images from that list. The thing is the images I need are not in a certain order, it's like image no. 3,7,10 etc. The problem is that I have around 50 images to create so doing it manually is not very efficient and my code is very long. I thought maybe to create a list variable with the specific numbers of images I want and then to create a function but not sure how to do that.

This is the code I wrote before adding the export to drive part-

var geometry=table1;
/**
 * Function to mask clouds using the Sentinel-2 QA band
 * @param {ee.Image} image Sentinel-2 image
 * @return {ee.Image} cloud masked Sentinel-2 image
 */
function maskS2clouds(image) {
  var qa = image.select('QA60');

  // Bits 10 and 11 are clouds and cirrus, respectively.
  var cloudBitMask = 1 << 10;
  var cirrusBitMask = 1 << 11;

  // Both flags should be set to zero, indicating clear conditions.
  var mask = qa.bitwiseAnd(cloudBitMask).eq(0)
      .and(qa.bitwiseAnd(cirrusBitMask).eq(0));

  return image.updateMask(mask).divide(10000)
  .copyProperties(image, ['system:time_start']);
}

// Map the function over one year of data and take the median.
// Load Sentinel-2 TOA reflectance data.
var dataset = ee.ImageCollection('COPERNICUS/S2')
                  .filterDate('2019-01-01', '2019-07-27')
                  // Pre-filter to get less cloudy granules.
                  .filter(ee.Filter.lt('CLOUDY_PIXEL_PERCENTAGE', 20))
                  .select('B2','B3','B4','B8','QA60')
                  .filterBounds(geometry)
                  .map(maskS2clouds);

var rgbVis = {
  min: 0.0,
  max: 0.3,
  bands: ['B4', 'B3', 'B2'],
};

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

//test if clipping the image collection worked
Map.centerObject(geometry);
Map.addLayer(clippedCol.median(), rgbVis, 'RGB');

// Get the number of images.
var count = dataset.size();
print('Count: ',count);
// print(clippedCol);//here I get the error messege "collection query aborted after accumulation over 5000 elements
// print(dataset,'dataset');//the same error here


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

};

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



// // Test the addNDVI function on a single image.
// var ndvi1 = withNDVI.select('NDVI').mean();


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

//get NDVI images

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


var list0= ee.Image(listOfImages.get(0));
var list3 =ee.Image(listOfImages.get(3));
var list7 =ee.Image(listOfImages.get(7));
var list10 =ee.Image(listOfImages.get(10));
var list12 =ee.Image(listOfImages.get(12));
var list15 =ee.Image(listOfImages.get(15));
var list17 =ee.Image(listOfImages.get(17));
var list21 =ee.Image(listOfImages.get(21));
var list24 =ee.Image(listOfImages.get(24));
var list28 =ee.Image(listOfImages.get(28));
var list30 =ee.Image(listOfImages.get(30));
var list34 =ee.Image(listOfImages.get(34));
var list38 =ee.Image(listOfImages.get(38));
var list39 =ee.Image(listOfImages.get(39));
var list41 =ee.Image(listOfImages.get(41));
var list43 =ee.Image(listOfImages.get(43));
var list46 =ee.Image(listOfImages.get(46));
var list50 =ee.Image(listOfImages.get(50));
var list52 =ee.Image(listOfImages.get(52));
var list56 =ee.Image(listOfImages.get(56));
var list59 =ee.Image(listOfImages.get(59));
var list61 =ee.Image(listOfImages.get(61));
var list63 =ee.Image(listOfImages.get(63));
var list65 =ee.Image(listOfImages.get(65));
var list69 =ee.Image(listOfImages.get(69));
var list71 =ee.Image(listOfImages.get(71));
var list75 =ee.Image(listOfImages.get(75));
var list78 =ee.Image(listOfImages.get(78));
var list82 =ee.Image(listOfImages.get(82));
var list84 =ee.Image(listOfImages.get(84));
var list86 =ee.Image(listOfImages.get(86));
var list89 =ee.Image(listOfImages.get(89));
var list90 =ee.Image(listOfImages.get(90));
var list93 =ee.Image(listOfImages.get(93));
var list95 =ee.Image(listOfImages.get(95));
var list98 =ee.Image(listOfImages.get(98));
var list102 =ee.Image(listOfImages.get(102));
var list105 =ee.Image(listOfImages.get(105));
var list109 =ee.Image(listOfImages.get(109));
var list112 =ee.Image(listOfImages.get(112));
var list115 =ee.Image(listOfImages.get(115));
var list119 =ee.Image(listOfImages.get(119));
var list122 =ee.Image(listOfImages.get(122));
var list133 =ee.Image(listOfImages.get(133));
var list134 =ee.Image(listOfImages.get(134));
var list138 =ee.Image(listOfImages.get(138));
var list140 =ee.Image(listOfImages.get(140));
var list144 =ee.Image(listOfImages.get(144));

Map.centerObject(geometry);



print('NDVI Images',withNDVI.select('NDVI'));
Map.addLayer(list0,colorizedVis,'0');
Map.addLayer(list3,colorizedVis,'3');
Map.addLayer(list7,colorizedVis,'7');
Map.addLayer(list10,colorizedVis,'10');
Map.addLayer(list12,colorizedVis,'12');
Map.addLayer(list15,colorizedVis,'15');
Map.addLayer(list17,colorizedVis,'17');
Map.addLayer(list21,colorizedVis,'21');
Map.addLayer(list24,colorizedVis,'24');
Map.addLayer(list28,colorizedVis,'28');
Map.addLayer(list30,colorizedVis,'30');
Map.addLayer(list34,colorizedVis,'34');
Map.addLayer(list38,colorizedVis,'38');
Map.addLayer(list39,colorizedVis,'39');
Map.addLayer(list41,colorizedVis,'41');
Map.addLayer(list43,colorizedVis,'43');
Map.addLayer(list46,colorizedVis,'46');
Map.addLayer(list50,colorizedVis,'50');
Map.addLayer(list52,colorizedVis,'52');
Map.addLayer(list56,colorizedVis,'56');
Map.addLayer(list59,colorizedVis,'59');
Map.addLayer(list61,colorizedVis,'61');
Map.addLayer(list63,colorizedVis,'63');
Map.addLayer(list65,colorizedVis,'65');
Map.addLayer(list69,colorizedVis,'69');
Map.addLayer(list71,colorizedVis,'71');
Map.addLayer(list75,colorizedVis,'75');
Map.addLayer(list78,colorizedVis,'78');
Map.addLayer(list82,colorizedVis,'82');
Map.addLayer(list84,colorizedVis,'84');
Map.addLayer(list86,colorizedVis,'86');
Map.addLayer(list89,colorizedVis,'89');
Map.addLayer(list90,colorizedVis,'90');
Map.addLayer(list93,colorizedVis,'93');
Map.addLayer(list95,colorizedVis,'95');
Map.addLayer(list98,colorizedVis,'98');
Map.addLayer(list102,colorizedVis,'102');
Map.addLayer(list105,colorizedVis,'105');
Map.addLayer(list109,colorizedVis,'109');
Map.addLayer(list112,colorizedVis,'112');
Map.addLayer(list115,colorizedVis,'115');
Map.addLayer(list119,colorizedVis,'119');
Map.addLayer(list122,colorizedVis,'122');


Map.addLayer(list133,colorizedVis,'133');
Map.addLayer(list134,colorizedVis,'134');
Map.addLayer(list138,colorizedVis,'138');
Map.addLayer(list140,colorizedVis,'140');
Map.addLayer(list144,colorizedVis,'144');


//Long-Term Time Series

print(ui.Chart.image.series(withNDVI, geometry, ee.Reducer.stdDev(), 30));


// var toexport6 = June6.visualize(colorizedVis).addBands(June6);
// var toexport13 = June13.visualize(colorizedVis).addBands(June13);
// var toexport18 = June18.visualize(colorizedVis).addBands(June18);
// var toexport23 = June23.visualize(colorizedVis).addBands(June23);
// var toexport28 = June28.visualize(colorizedVis).addBands(June28);

My end goal is to be able to export those specific images to my drive with their original visualization and resolution, without writing so many lines in my code.

2

As Map.addLayer and Export are client side function, it must be done in the client side, so if you want to do it straight forward, make a simple loop

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

var toExport = [1, 3, 4]

for (var i in toExport) {
  var image = ee.Image(listOfImages.get(toExport[i]))
  // do what ever you need with image
  Map.addLayer(image, colorizedVis, i);
}

And if you want to make it a bit complex, make a function and then iterate

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

var processImage = function(image, vis, name) {
  // do what ever you need with image
  Map.addLayer(image, vis, name);
}

var toExport = [1, 3, 4]

for (var i in toExport) {
  var image = ee.Image(listOfImages.get(toExport[i]))
  processImage(image, colorizedVis, i)
}

But I think in your case with the first option is just fine.

link

  • Thank you for you answer, it works, but one thing I didn't understand- what is the part of the function you ised ''processImage'' and what is it suppose to do, does it suppose to save the original viualization of the image , because now I get it in black andwhite. processImage(image, colorizedVis, i) – Reut Jul 29 at 11:44
  • processImage is just a function to separate the process from the loop, it should work the same as doing all in the loop. Check the parameters you pass to the function. I tested it as I proposed and worked fine, the visualization is correct – Rodrigo E. Principe Jul 29 at 11:59
  • I get the rifht visuzlization in the platform but when I download I get it black, I tried to change the cunftion to this- ~~~for (var i in listOfNumbers) { var image = ee.Image(listOfImages.get(listOfNumbers[i])); var toexport=image.visualize(colorizedVis).addBands(image); // do what ever you need with image Map.addLayer(image, colorizedVis, i); Export.image.toDrive({ image: toexport.toFloat(), description: i, scale:2.5, maxPixels:259600750, region:geometry.geometry().bounds() }); ~~~ but all I get is black polygons – Reut Jul 29 at 12:14
  • It's not an Earth Engine issue. What software are you using for opening the downloaded image? – Rodrigo E. Principe Jul 29 at 12:33
  • I use qgis, I had this problem in the past because whenever downloading rasters from GEE it doesn't get the iriginal colors unless I define it – Reut Jul 29 at 12:41

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.