Kunal Aneja
Kunal Aneja

Reputation: 31

Download Image from Google Earth Engine `ImageCollection` to drive

I need to download single or multiple images from the collection of ee. (preferably multiple but I can just put a single image code in a loop).

My main problem --> Download every month image from a start date to end date for a specific location (lat: "", long: "") with zoom 9

I am trying to download historical simple satellite data from the SKYSAT/GEN-A/PUBLIC/ORTHO/RGB. This gives an image like -->

https://developers.google.com/earth-engine/datasets/catalog/SKYSAT_GEN-A_PUBLIC_ORTHO_RGB#description

I am working on this in python. So this code will give me the collection but I can't download the entire collection, I need to select an image out of it.

import ee
# Load a Landsat 8 ImageCollection for a single path-row.
collection = (ee.ImageCollection('SKYSAT/GEN-A/PUBLIC/ORTHO/RGB').filterDate('2016-03-01', '2018-03-01'))
#pp.pprint('Collection: '+str(collection.getInfo())+'\n')

# Get the number of images.
count = collection.size()
print('Count: ', str(count.getInfo()))
image = ee.Image(collection.sort('CLOUD_COVER').first())

Here the image contains the <ee.image.Image at 0x23d6cf5dc70> property but I don't know how to download it.

Also, how do I specify I want it for a specific location(lat, long) with zoom 19.

Thanks for anything

Upvotes: 3

Views: 4021

Answers (2)

ablmmcu
ablmmcu

Reputation: 159

You can download your whole images in the image collection using geetools.

You can install with pip install geetools.

Then, change the area, dates, and folder name(if the folder does not exist, it will be created on your drive) and execute this code.

import geetools

StartDate= ee.Date.fromYMD(2018,1,16)
EndDate = ee.Date.fromYMD(2021,10,17)

Area = ee.Geometry.Polygon(
        [[[-97.93534101621628, 49.493287372441955],
          [-97.93534101621628, 49.49105034378085],
          [-97.93049158231736, 49.49105034378085],
          [-97.93049158231736, 49.493287372441955]]])

collection =(ee.ImageCollection('SKYSAT/GEN-A/PUBLIC/ORTHO/RGB')
          .filterDate(StartDate,EndDate)
          .filterBounds(Area)

data_type = 'float32'
name_pattern = '{system_date}'
date_pattern = 'yMMdd' # dd: day, MMM: month (JAN), y: year
scale = 10
folder_name = 'GEE_Images'

tasks = geetools.batch.Export.imagecollection.toDrive(
            collection=collection,
            folder=folder_name ,
            region=Area ,
            namePattern=name_pattern,
            scale=scale,
            dataType=data_type,
            datePattern=date_pattern,
            verbose=True,
            maxPixels=1e10
        )

Or;

You can use the Earth Engine library to export images.

nimg = collection.toList(collection.size().getInfo()).size().getInfo()            
                                  
for i in range(nimg):
    img = ee.Image(collection.toList(nimg).get(i))
    date = img.date().format('yyyy-MM-dd').getInfo()

    task = ee.batch.Export.image.toDrive(img.toFloat(), 
                                          description=date,
                                          folder='Folder_Name',
                                          fileNamePrefix= date,
                                          region = Area,
                                          dimensions = (256,256), 
                                          # fileFormat = 'TFRecord',
                                          maxPixels = 1e10)
    task.start()

There are two file formats; TFRecord and GeoTIFF. The default format is GeoTIFF. Also, you can extract images with specific dimensions as shown above. If you want to download images with a scale factor, just remove the dimension line and add a scale factor instead of it.

You can read this document for more information.

Upvotes: 2

CrossLord
CrossLord

Reputation: 612

Insert your region of analysis (geom) by constructing a bounding box. Then, use the code below to batch download the images.

// Example geometry. You could also insert points etc.
var geom = ee.Geometry.Polygon(
  [[[-116.8, 44.7],
    [-116.8, 42.6],
    [-110.6, 42.6],
    [-110.6, 44.7]]], None, False)

for (var i = 0; i < count ; i++) {
  var img = ee.Image(collection.toList(1, i).get(0));
  var geom = img.geometry().getInfo();
  Export.image(img, img.get('system:index').getInfo(), {crs: crs, scale: scale, region: geom});
}

Upvotes: 1

Related Questions