0

I have a list of FeatureCollections that I want to export all at once (instead of writing out the export module multiple times) using Javascript API. I have seen the posts by @Rodrigo E Principe such as here and others like this one showing the use of normal JS loops (admittedly the second link is for exporting to asset).

I am trying to do the exact same thing as the first link, just exporting a FeatureCollection instead of an ImageCollection. However, when I run the code I get no tasks to run nor am I getting any errors. I don't understand what's happening.

Here is my code:

// Make a list of Features.
var features = [
  ee.Feature(ee.Geometry.Point(97.58, 24.36), {name: 'sample1'}),
  ee.Feature(ee.Geometry.Point(99.45, 20.26), {name: 'sample2'}),
  ee.Feature(ee.Geometry.Point(93.24, 21.42), {name: 'sample3'}),
  ee.Feature(ee.Geometry.Point(96.13, 24.12), {name: 'sample4'}),
  ee.Feature(ee.Geometry.Point(96.88, 22.48), {name: 'sample5'})
];

// Create a FeatureCollection from the list and print it.
var points = ee.FeatureCollection(features);
print(points);

var l8sr = ee.ImageCollection('LANDSAT/LC08/C01/T1_SR')
                  .filterDate('2015-06-23', '2020-01-31')
                  .filterBounds(points);
print("Landsat", l8sr)


var index = ee.List.sequence(0,4)
var coords = points.geometry().coordinates()
print('coords', coords)

// do this for each index
var test1 = index.map(function(q){
    var sub = l8sr.filterBounds(ee.Geometry.Point(coords.get(q)))
    return sub
});

//run test for first one
var firstcoll = ee.ImageCollection(test1.get(0))
var img = firstcoll.first();
var bands = img.bandNames()

//get the value for each band for every image
var loop = index.map(function(ind){
  var getvals = function(img1){
    var date = img1.date().format('yyyy-MM-dd')
    var value = img1.select(bands)
   .reduceRegion(ee.Reducer.first(), ee.Geometry.Point(coords.get(ind)))
   return ee.Feature(ee.Geometry.Point(coords.get(ind)), {value: value, date: date})
  };
var newft = firstcoll.map(getvals);
return(newft);
});

print('loopresult', loop)
print('loop ex', ee.FeatureCollection(loop.get(0)))

// loop on client side
for (var i = 0; i<loop.length;i++) {
  var toexport = ee.FeatureCollection(loop.get(i));
  var nam = "test_point_" + i

      // Export
Export.table.toDrive(
  {collection: toexport,
  description: nam,
  folder: "dissertation",
  fileNamePrefix: nam,
  selectors: ['B1','B2','B3','B4','B5','B6','B7','B10','B11',
  'pixel_qa', 'radsat_qa', 'sr_aerosol']
  });
}
2

You need to get the loop variables on the client-side. Thus, use getInfo() or evaluate() in this line (51):

for (var i = 0; i<loop.getInfo().length;i++) 

or probably a bit more efficient

for (var i = 0; i<loop.size().getInfo();i++) 

or with evaluate:

loop.size().evaluate(function(sizeCol){
  // loop on client side
  for (var i = 0; i<sizeCol;i++) {

   ........
}

If you now the number of featureCollections you want to export prior, you could also just hard code that value. Also, you might want to set the variable value as a dictionary to the feature collection. See line 41: link, or with evaluate link2

| improve this answer | |
  • perfect, thank you! This is what I was looking for. Can I ask, how did you share the links like that to the script? – waiiana Apr 17 at 14:52
  • the Get Link button in the upper center – Kuik Apr 18 at 11:01

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.