1

I am unable to iterate over an ImageCollection, therefore I have created a list that contained all images in the ImaheCollection but once I try to iterate over it (by sending each image to a certain function) I get this error:

sngl_sen2.reduceRegion is not a function

I get this error from the second call to get_List (the one that sends S2_list[0])

This is the code:

var region = ee.FeatureCollection([ee.Feature(Map.getBounds(true))])

//Dates of Interest
var start = ee.Date("2018-04-01");
var finish = ee.Date("2018-05-01");

//------------------- Data bases ---------------------//
var sen2_for_dates = ee.ImageCollection('COPERNICUS/S2_SR')
  .filterDate(start, finish)
  .filterBounds(region)
  .map(function(image){
    return image
    .clipToCollection(region)
    .updateMask(image.select('QA60').not())
    .addBands(image.normalizedDifference(['B8','B4']).rename('ndvi'))
  })
  .select('ndvi')

//------------------ Creating the feature collection for export --------------//  
function get_List(sngl_sen2){
   var list2 = sngl_sen2.reduceRegion({
    reducer: ee.Reducer.toList(),
    geometry: region,
    maxPixels: 1e8,
    scale: 10
  });

  // Make a feature without geometry and set the properties to the dictionary of means.
  var feature2 = ee.Feature(null, list2);

  // Wrap the Feature in a FeatureCollection for export.
  var featureCollection = ee.FeatureCollection([feature2]); 
  return featureCollection
}

//Making a list of images so I can iterate over all images.
var S2_list = sen2_for_dates.getInfo()['features']

Export.table.toDrive({
  collection: get_List(sen2_for_dates.first()), //works for the first image in the ImageCollection
  fileNamePrefix: 'SEN2_',
  folder: 'AI_data/'+'try',
  selectors: ['ndvi'],
  fileFormat: 'CSV'
  });

Export.table.toDrive({
  collection: get_List(S2_list[0]), //Does not work for the first image in the list
  fileNamePrefix: 'SEN2_',
  folder: 'AI_data/'+'try',
  selectors: ['ndvi'],
  fileFormat: 'CSV'
  });

My final goal is to iterate over the list and pass on the images from the list to the get_List function (because my original code iterates over more than one list, so I need to index the objects from the different lists using the same indexed)

How can I pass S2_list[0] to the function get_List in a manner that will enable it to use it with all the functions that can be carried out in an image (e.g. reduceRegion)?

Many thanks

7
  • Why do you want to export Images as CSVs?
    – JonasV
    May 11, 2020 at 9:52
  • I will later use these CSVs in deep neural networks to estimate NDVI for future dates May 12, 2020 at 19:13
  • Then why not export them as Images/Tifs? You might have to do some more preparation for the neural net, but it's way easier to export and save
    – JonasV
    May 13, 2020 at 9:07
  • Aren't these files fairly big to handle? I will need to download all the images from 2017 to 2020 from Sentinel 2 and Sentinel 3 May 13, 2020 at 9:31
  • 1
    They are not much bigger than downloading them as CSV, they also do not lack the spatial information.
    – JonasV
    May 13, 2020 at 14:25

1 Answer 1

1

You can make an ee.List() of a Collection using toList(). Then make a client-side for loop to export a featureCollection of the pixel data of each image:

//Making a list of images so I can iterate over all images.
var S2_list = sen2_for_dates.toList(sen2_for_dates.size());

for (var i = 0; i < 10; i++) {
  var image = ee.Image(S2_list.get(i));
  Export.table.toDrive({
    description: 'exportTast'+i,
    collection: get_List(image), 
    fileNamePrefix: 'SEN2_',
    folder: 'AI_data/'+'try'+i,
    selectors: ['ndvi'],
    fileFormat: 'CSV'
    });
}

Possibly, it is best to hard code the number of images in the collection (I set it at 10), though you can also find it using sen2_for_dates.size().getInfo().

1
  • Perfect. Thank you @Kulik! May 12, 2020 at 19:29

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.