How to export Google Earth Engine images to local files using Python

Viewed 913

I'm producing a ge image using this code:

image = ee.ImageCollection(satellite) \
            .filterDate(startdate, enddate) \
            .filterBounds(ee.Geometry.Point(centroid[0], centroid[1])) \
            .select(bands) \
            .map(removeClouds) \
            .mean()
type(image)

<class 'ee.image.Image'>

How can I save the resulting image as a local GeoTIFF file, that is, a file stored in the desktop machine that's running the script?

2 Answers

You cannot download it directly to your local directory. As explained here, "Because Google Earth Engine data exports are asyncronous, the destinations are also cloud-based. Your options are Google Drive, Google Cloud Storage, or new assets in Earth Engine."

For extracting images from Google Drive, this answer can help you.

Not sure about ImageCollection, but you can download an Image to your local drive with:

import requests
import ee

img = ee.Image('COPERNICUS/S2_SR/20210109T185751_20210109T185931_T10SEG')
region = ee.Geometry.BBox(-122.0859, 37.0436, -122.0626, 37.0586)

# Multi-band GeoTIFF file.
url = img.getDownloadUrl({
    'bands': ['B3', 'B8', 'B11'],
    'region': region,
    'scale': 20,
    'format': 'GEO_TIFF'
})
response = requests.get(url)
with open('multi_band.tif', 'wb') as fd:
  fd.write(response.content)

Source: https://developers.google.com/earth-engine/apidocs/ee-image-getdownloadurl#colab-python

Related