https://colab.research.google.com/assets/colab-badge.svg

GEE IO#

Setup#

Ecoscope#

[ ]:
# !pip install ecoscope
[ ]:
import os
import sys
import zipfile

import shapely

import ecoscope

ecoscope.init()

Google Drive Setup#

[ ]:
output_dir = "Ecoscope-Outputs"

if "google.colab" in sys.modules:
    from google.colab import drive

    drive.mount("/content/drive/", force_remount=True)
    output_dir = os.path.join("/content/drive/MyDrive/", output_dir)

os.makedirs(output_dir, exist_ok=True)

Earth Engine#

[ ]:
import ee

try:
    EE_ACCOUNT = os.getenv("EE_ACCOUNT")
    EE_PRIVATE_KEY_DATA = os.getenv("EE_PRIVATE_KEY_DATA")
    if EE_ACCOUNT and EE_PRIVATE_KEY_DATA:
        ee.Initialize(credentials=ee.ServiceAccountCredentials(EE_ACCOUNT, key_data=EE_PRIVATE_KEY_DATA))
    else:
        ee.Initialize()

except ee.EEException:
    ee.Authenticate()
    ee.Initialize()

Define Region as an EarthEngine Geometry#

[ ]:
region_geo = ee.Feature(shapely.geometry.mapping(shapely.geometry.Point(5.178332, -1.200788).buffer(0.3))).geometry()

Define the Image to Sample#

[ ]:
img = ee.Image("USGS/SRTMGL1_003")

Define the Download Configuration#

Options are: description, folder, fileNamePrefix, dimensions, region, scale, crs, crsTransform, maxPixels, shardSize, fileDimensions, skipEmptyTiles, fileFormat, formatOptions

[ ]:
download_config = {
    "description": "dem",
    "folder": "Ecoscope-Outputs",
    "fileNamePrefix": "dem",
    "region": region_geo.getInfo()["coordinates"],
    "scale": 30,
    "fileFormat": "geotiff",
}

Method 1: Download Using a Google Drive Background Task#

[ ]:
task = ee.batch.Export.image.toDrive(img, **download_config)
task.start()

Check Task Status#

[ ]:
task.status().get("state")

Method 2: Direct Download (Size-Limited)#

[ ]:
img_zip_file = os.path.join(output_dir, "img_file.zip")

ecoscope.io.utils.download_file(
    url=img.getDownloadUrl(download_config),
    path=img_zip_file,
)

Unzip#

[ ]:
with zipfile.ZipFile(img_zip_file) as z:
    for name in z.namelist():
        z.extract(name, output_dir)