8

I need to download a bunch of Landsat images for my thesis. My problem seems simple but I don't have a clue about JavaScript and the documentation didn't help enough. I have filtered the collection to my region and time period and i want to export all images to Drive, seperately. Collection example:

var surfaceReflectanceL5 = ee.ImageCollection('LANDSAT/LT5_SR');
var dateSR5=surfaceReflectanceL5.filterDate('1984-01-01', '1985-01-01');
var prSR5=dateSR5.filter(ee.Filter.eq('wrs_path', 182))
                     .filter(ee.Filter.eq('wrs_row', 35)); 

The code for exporting a single image is:

Export.image.toDrive({
  image: image1 //example, var image1='Landsat/....'
  description: 'L51984_1',
  scale: 30,
});

How can I iterate through the collection to export all images? The use of map() function seems to be the answer.

prSR5.map(Export.image.toDrive({
  image: image,
  description: 'L51984',
  scale: 30,
}));

The question is how to set the image parameter to the correct image (i.e first the 1st image, then the 2nd etc, something like 'thisImage()' ) and the description to match image (i.e. 'L51984_1','L51984_2'...).

Many thanks in advance!!!

DiNik
  • 117
  • 1
  • 1
  • 7
  • If you're just downloading the images, I'd recommend using a more traditional source like [earthexplorer](https://earthexplorer.usgs.gov/) ... this will get you off of `JavaScript` and potentially be also faster and more convenient. – Val May 15 '17 at 10:21
  • Thanks for the answer. I tried ordering from earth explorer first but the order will take long to complete ( i assume a month as there are 200k images in backlog...) – DiNik May 16 '17 at 19:36

1 Answers1

16

I have created a function to perform a similar action. It is available in a bunch of gee tools I've created: https://github.com/fitoprincipe/geetools-code-editor

Here is the code:

/* 
 * Author: Rodrigo E. Principe
 * License: Apache 2.0

PURPOSE:
This function Exports all images from one Collection
PARAMETERS:
col = collection that contains the images (ImageCollection) (not optional)
folder = the folder where images will go (str) (not optional)
scale = the pixel's scale (int) (optional) (defaults to 1000) (for Landsat use 30)
type = data type of the exported image (str) (option: "float", "byte", "int", "double") (optional) (defaults to "float")
nimg = number of images of the collection (can be greater than the actual number) (int) (optional) (defaults to 500)
maxPixels = max number of pixels to include in the image (int) (optional) (defults to 1e10)
region = the region where images are on (Geometry.LinearRing or Geometry.Polygon) (optional) (defaults to the image footprint)
Be careful with the region parameter. If the collection has images 
in different regions I suggest not to set that parameter
EXAMPLE:
ExportCol(myLandsatCol, "Landsat_imgs", 30)
*/

var ExportCol = function(col, folder, scale, type,
                         nimg, maxPixels, region) {
    type = type || "float";
    nimg = nimg || 500;
    scale = scale || 1000;
    maxPixels = maxPixels || 1e10;

    var colList = col.toList(nimg);
    var n = colList.size().getInfo();

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

      var imgtype = {"float":img.toFloat(), 
                     "byte":img.toByte(), 
                     "int":img.toInt(),
                     "double":img.toDouble()
                    }

      Export.image.toDrive({
        image:imgtype[type],
        description: id,
        folder: folder,
        fileNamePrefix: id,
        region: region,
        scale: scale,
        maxPixels: maxPixels})
    }
  }

I haven't tried it a lot, but worked in a couple of test I made, example:

var batch = require('users/fitoprincipe/geetools:batch')

var col = ee.ImageCollection("LEDAPS/LE7_L1T_SR").filterDate("2002-01-01","2002-01-03");
batch.Download.ImageCollection.toDrive(col, "Folder", {scale:30});

If you have any issue you can comment here, but also you can post it on github.

Rodrigo E. Principe
  • 1,281
  • 16
  • 26
  • I know that it is probably too late, but thank you! – DiNik Oct 22 '19 at 07:54
  • 1
    if it worked for you, it's not late =) check this out: https://github.com/fitoprincipe/geetools-code-editor/wiki/Batch – Rodrigo E. Principe Oct 22 '19 at 18:33
  • I was back then in a hurry, and totally forgot that I had asked the question and did not see the answer unfortunately till yesterday (2 years after :O).. The images covering my area were only 2-3 so I did it manually. But I will definitely use your tool in the future! – DiNik Oct 23 '19 at 06:53
  • hi @RodrigoE.Principe , i tried copy pasting the test code you provided above but it doesn't seem to run on my google earth engine code editor :( i tried a few of the others you posted on different questions but none of them seemed to work. do you have any advice? – catlovingtaco Feb 13 '20 at 09:06
  • 1
    I have the same problem. Tried to download ERA5 data using the tool but did not work. Used something like this in the GEE editor: "batch.Download.ImageCollection.toDrive(ERA5data, 'ERA5', {region: polygon});" and also tried the python library using batch.Export.imagecollection.toDrive which also didn't work. Is there any documentation on the library? – avocado1 Feb 16 '20 at 16:49
  • An important point is that Export is a client side function. You cannot iterate through the collection server side inside an ImageCollection.map() for example. – intotecho Sep 15 '21 at 02:03