1

I am mapping over a function that returns images, more specifically changemaps made with the LandTrendr API, which are ee.Images (according to the LandTrendr for GEE homepage).

I end up with a list (see print output in code below) of images and want to use a for-loop to write them to my Google drive. (I am aware that for-loops are deprecated in GEE.)

However, no task is created and no error is thrown, leaving me puzzled to what is wrong/missing.

Here is a reproducible example. If you have not used LandTrendr before and want to run this example you have to visit this site first to add the API to your account.

var years = ee.List.sequence(1985, 1995, 5);
var aoi = ee.Geometry.Point(13.64, 55.9552).buffer(500);

var imageList = years.map(function(n) {

// define landtrendr parameters
var startYear = 1985;
var endYear =  1995;
var startDay = '06-20';
var endDay = '09-10';
var index = 'NDVI';
var maskThese = ['cloud', 'shadow', 'snow', 'water'];

var runParams = { 
  maxSegments:            6,
  spikeThreshold:         0.9,
  vertexCountOvershoot:   3,
  preventOneYearRecovery: false,
  recoveryThreshold:      0.9,
  pvalThreshold:          0.05,
  bestModelProportion:    0.75,
  minObservationsNeeded:  6
};

var strt = ee.Number(n)
var nd = ee.Number(n).add(5)

// define change parameters
var changeParams = {
  delta:  'loss',
  sort:   'greatest',
  year:   {checked:true, start: strt, end: nd},
  mag:    {checked:false, value:100,  operator:'>'},
  dur:    {checked:false, value:5,    operator:'<'},
  preval: {checked:false, value:300,  operator:'>'},
  mmu:    {checked:false, value:11},
};

// load the LandTrendr.js module
var ltgee = require('users/emaprlab/public:Modules/LandTrendr.js'); 

// add index to changeParams object
changeParams.index = index;

// run landtrendr
var ftvList = ['NDVI'];
var lt = ltgee.runLT(startYear, endYear, startDay, endDay, aoi, index, ftvList, runParams, maskThese);

// get the change map layers
var changeImg = ltgee.getChangeMap(lt, changeParams);

return changeImg
  
});

print(imageList) //Output below

//List (3 elements)
  //0: Image (6 bands)
    //type: Image
    //bands: List (6 elements)
      //0: "yod", signed int16, EPSG:4326
      //1: "mag", double, EPSG:4326
      //2: "dur", double, EPSG:4326
      //3: "preval", double, EPSG:4326
      //4: "rate", double, EPSG:4326
      //5: "dsnr", double, EPSG:4326
  //1: Image (6 bands)
  //2: Image (6 bands)


for (var i = 0; i<imageList.length; i++) {
  
  // get changemap from list
  var im = imageList.get(i)
  // Export band named 'mag' as GeoTiff
  Export.image.toDrive({
  image: im.select(['mag']),
  description: 'changemap_'+i,
  scale: 30,
  region: aoi,
  fileFormat: 'GeoTIFF',
  maxPixels: 3991919404
  
});

}

1 Answer 1

1

There are 2 bugs in your code.

  1. ee.List.length is a function. To execute that function you need to call it using ee.List.length()
  2. ee.List.get(i) returns the i'th element in an object form. If you want to call any function on the resulting object (like .select()) you need to force cast it to ee.Image() type like this var im = ee.Image(imageList.get(i))

After these 2 fixes, the code works fine. Link to corrected code.

Note: Depending on your computer's RAM, this code may hang. For batch exports, GEE recommends using their python API and the function ee.batch.Export() function.

0

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.