fitoprincipe / geetools-code-editor

A set of tools to use in Google Earth Engine Code Editor (JavaScript)
https://github.com/fitoprincipe/geetools-code-editor/wiki
MIT License
302 stars 110 forks source link

Issue with batch encountering error #12

Open mcgregorian1 opened 2 years ago

mcgregorian1 commented 2 years ago

Hi, I was trying to use batch.ImageCollection.toDrive and I received the error:

Cannot read property 'toDrive' of undefined

I tried running the code directly from source and it only worked when I changed the region parameter to null in Export.Image.toDrive and exported only img instead of imgType. Do you know why the error occurred?

Here is my code with the failure:

var xmin = 97.1286789065688
var xmax = 97.4294562866087
var ymin = 24.7742991886781
var ymax = 24.9930169846125

var polygon = ee.Geometry.Polygon([
  [xmin, ymin], [xmin, ymax],
  [xmax, ymax], [xmax, ymin]
]);

Map.addLayer(polygon);
Map.centerObject(polygon, 8);

var collection = ee.ImageCollection("LANDSAT/LC08/C02/T1_L2")
                .filterDate("2015-06-23", "2020-02-01")
                .filterBounds(polygon)

var clipped = collection.map(function(img){
  return img.clip(polygon)
})
//print(clipped)

var batch = require('users/fitoprincipe/geetools:batch');
batch.ImageCollection.toDrive(clipped, 'imagesL8app', {scale: 30, type: 'uint16'})

And here is the source code I used that worked:

var tools = require('users/fitoprincipe/geetools:tools');    
var downloadAll = function(collection, folder, options) {

    var defaults = {
      scale: 1000,
      maxPixels: 1e13,
      type: 'float',
      region: null,
      name: null,
    }

    var opt = tools.get_options(defaults, options)

    var n = collection.size().getInfo();

    var colList = collection.toList(n);

    var colID = opt.name || collection.getInfo()['id'] || ""
    colID = colID.replace('/','_')

    for (var i = 0; i < n; i++) {
      var img = ee.Image(colList.get(i));
      var id = img.id().getInfo() || colID+'_image_'+i.toString();
      var region = opt.region || img.geometry().bounds().getInfo()["coordinates"];

      //var imtype = IMAGE_TYPES(img, opt.type)

      Export.image.toDrive({
        image: img,
        description: id,
        folder: folder,
        //region: region,
        fileNamePrefix: id,
        scale: opt.scale,
        maxPixels: opt.maxPixels})
    }
  }
downloadAll(clipped, 'imagesL8app', {scale: 30, type: 'uint16'})
acwatt commented 12 months ago

The correct syntax is batch.Download.ImageCollection.toDrive. The error Cannot read property <property> of undefined generally means that you are trying to access a property (toDrive in this case) of an object that does not exist or is null. This is a sign that you should inspect the parent object further. In this case, the parent object batch.ImageCollection does not exist because batch has no property ImageCollection.

As a general note in EE javascript, when you require a javascript file, the object returned is a dictionary with the properties given by any exports that were assigned in the script. The batch script only has two exports: exports.getRegion and exports.Download.

You can access batch's exported objects with batch.<objectName>. In the batch script, the dictionary object Download is exported at the end of the script using exports.Download = Download.

The Download object is defined as a dictionary. Then, the ImageCollection.toDrive function is defined as a property of the Downloads dictionary using Download.ImageCollection.toDrive.