Google Earth Engine Python API – How to Execute Batch Tasks

google-earth-enginegoogle-earth-engine-python-api

In Google Earth Engine Compute Units (EECUs) There are two kinds of EECUs: “Batch” and “Online.” Batch EECUs are typically used for very large jobs (for example, exports), and online EECUs provide near-real-time responses in the Code Editor, apps, etc. (from here)

I would like to use Batch EECU with Python API. The usecase is this: I have many polygons and I would like to calculate the average slope for each polygon. To calculate the average slope of a single polygon (using Python API) I used:

average_slope = ee.Terrain.slope(ee.Image("USGS/SRTMGL1_003")).reduceRegion(
    reducer=ee.Reducer.mean(),
    geometry=GEE_polygon,
    scale=30
    ).getInfo()['slope']

To calculate the average slope of many polygons I use a loop and execute the above code for each polygon, however, doing that is considered "online EECU".

How can I perform this task using Batch EECU (in Python)?

Best Answer

You need to create (and start) a ee.batch.Export.to* task.

For your example, this would be something like:

slope_img = ee.Terrain.slope(ee.Image("USGS/SRTMGL1_003")).select("slope")
slope_fc = slope_img.reduceRegions(
collection=my_feature_collection,
reducer=ee.Reducer.mean(),
scale=30)

ee.batch.Export.table.toDrive(
collection = slope_fc,
description="my export task",
fileNamePrefix="mytable",
fileFormat="CSV"  # or geojson.. depending on what you want
# other options.. e.g. `selectors` might be a good idea if you don't want
# to include all the properties from your feature collection.
).start()

Note that I used reduceRegions instead of reduceRegion since you mentioned you have many geometries, so ideally these are a feature collection (my_feature_collection in the example above).

Related Question