2

I am looking for a method to clip all the images in an ImageCollection through all the features of a vector (world borders), and then save the resulting tifs in my Google Drive.

So far I wrote this script, which does not work but it gives an idea of what I would like to do. A part for looping over the save with the corresponding filename is missing, as I am not sure how to write it.

var coll = ee.ImageCollection('NOAA/VIIRS/DNB/MONTHLY_V1/VCMSLCFG').select('avg_rad');
var geometry = ee.FeatureCollection("users/gg/gadm");

var loop = function(image, feature){
    var collection_roi = image.clip(feature);

  // missing part to save each output to drive

    }

var stacked = loop(coll, geometry);

Edit:

Using the following code I did manage to create two-argument loop. However, I end up with a list of imagecollections. I am not sure how to save on my drive all the images of each imagecollections in the list.

var coll = ee.ImageCollection('NOAA/VIIRS/DNB/MONTHLY_V1/VCMSLCFG').select('avg_rad');

var geometry = ee.FeatureCollection("users/gg/gadm");

// loop over features and each image of
var loop = function(image){
 var output = geometry.map(function(geom){
 // Clip the image to the polygon geometry
    var ic = image.clip(geom);
    return ic;
})
  return output
};

var outputd = coll.map(loop)

var listOfImages = outputd.toList(outputd.size());
  • 1
    For looping over an ImageCollection read developers.google.com/earth-engine/ic_mapping, and for exporting developers.google.com/earth-engine/exporting. You can read also gis.stackexchange.com/questions/248216/… – Rodrigo E. Principe Nov 8 '18 at 10:28
  • 1
    I did manage to create two-argument loop. I end up with a list of imagecollections. I am not sure how to save on my drive all the images of each imagecollections in the list. – Jackk Nov 8 '18 at 14:29
  • 1
    Ok, you can always update your question to show your progress – Rodrigo E. Principe Nov 8 '18 at 15:14
  • 1
    When performing such an export please keep in mind that this will create a single export task for each of the ~250 countries in gadm. There are limits to the amount of parallel batch tasks you can perform. You probably achieve the result faster by getting the complete VIIRS dataset and clipping the file locally. – Kersten Nov 8 '18 at 21:36
  • Thank you @Kersten, I forgot to tell him about that. I made the tests by limiting collections =S I think probably using the python API there could be a way to do it "at once" (automatically at least) – Rodrigo E. Principe Nov 9 '18 at 0:42
3

OK, now that I understand what you are trying to do, this is how I'd do it:

var coll = ee.ImageCollection('NOAA/VIIRS/DNB/MONTHLY_V1/VCMSLCFG').select('avg_rad')

var geometry = ee.FeatureCollection("users/gg/gadm");
var name_prop = 'country_na' // replace with the name of the property that holds the polygon name in 'users/gg/gadm'
var date_format = 'yyyyMMdd' // change if you want

// loop over features and each image of
var loop = function(image){
 var output = geometry.map(function(geom){
   // Clip the image to the polygon geometry
   var name = geom.get(name_prop)
   var ic = image.clip(geom);
   var date = ic.date().format(date_format)
   var finalname = ee.String(name).cat('_').cat(date)
   return ic.set('name', finalname).set('system:footprint', geom);
  })
  return output
};

var outputd = coll.map(loop).flatten()
var size = outputd.size().getInfo()
var outputlist = outputd.toList(size)

for (var i=0; i<size; i++) {
  var image = ee.Image(outputlist.get(i))
  var name = image.get('name').getInfo()
  Export.image.toDrive({
    image:image, 
    description:name, 
    folder: 'FOLDER', 
    fileNamePrefix: name, 
    region: image.geometry().getInfo(), 
    scale: 463 // ??
  })
}

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.