[GIS] Export Google Earth Engine Landsat Image to Google Drive not working in Python API

google-earth-enginegoogle-earth-engine-python-apilandsat

I am trying to export a collection of Landsat images (but at this point I will settle for a single image) from Google Earth Engine to my Google Drive. I am using the Python API from my local terminal but I can not get the Export.image.toDrive (or ee.batch.Export.image.toDrive) to work. I even tried the code in the JavaScript code editor (in JavaScript, not Python) and it didn't work there either.

I have tried everything I can find on Google API Docs, StackOverflow, and GIS StackExchange – nothing has worked for me.

Eventually I need to iterate over an ImageCollection but to keep it simple for initial export here is my code for a single image:

#python
import datetime
import ee
import ee.mapclient

ee.Initialize()

#download a polygon to clip the collection of images down to
okla = (ee.FeatureCollection('TIGER/2018/States') #get oklahoma polygon to filter images by, this polygon is correct
    .filter(ee.Filter().eq('NAME','Oklahoma')))

#download image collection
collection = (ee.ImageCollection('LANDSAT/LE07/C01/T1_SR')
             .filterDate(datetime.datetime(2012, 1, 1),
                         datetime.datetime(2012, 1, 30)) #set dates to download (year,month,day)
            .filterBounds(okla)) #this is not a full clip

#condense collection to single image (do not want to do this in the future)
image1 = collection.mean() #takes the average of images in collection

image2 = image1.clipToCollection(okla) #this works and actually clips images to outline of Oklahoma

taskToexport = ee.batch.Export.image.toDrive(
        image = image2,             
        description = 'test1',
        )  

taskToexport.start()

Best Answer

Using your script, I ran taskToexport.status() to check on the status of the download request and an error was returned:

error_message: Unable to export unbounded image.

You need to provide an argument to the region parameter of the ee.batch.Export.image.toDrive function to bound the extent of the download:

taskToexport = ee.batch.Export.image.toDrive(
        image = image2,             
        region = okla.geometry().bounds().getInfo()['coordinates'],
        description = 'test1',
        )

taskToexport.start()

The region parameter is described as such:

A LinearRing, Polygon, or coordinates representing region to export. These may be specified as the Geometry objects or coordinates serialized as a string. If not specified, the region defaults to the viewport at the time of invocation.

Note that If not specified, the region defaults to the viewport at the time of invocation, but within the Python API there is no Map viewport, so an error occurs. Within the Python API you should always set the region parameter.

Related Question