0

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)?

1 Answer 1

0

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).

4
  • So the approach should be first to generate a feature collection object and then insert it into the desired function. Can the ee.batch.Export contain multiple functions? for example, slope and NDVI from Landsat? Or should I create 2 different ee.batch.Export?
    – user88484
    Dec 10, 2023 at 9:35
  • It would help to understand what is your GEE_polygon (is it a multiPolygon?), but in my opinion it would be cleaner to have a FeatureCollection, where each feature is one polygon. The export task is in general to export images, tables, etc. I think you meant to ask if reduceRegions can handle multiple bands, and the answer is yes. The example above used only one band ("slope") but in general it can reduce all bands in your image. Dec 10, 2023 at 11:06
  • GEE_polygon is one polygon created with ee.Geometry.Polygon function. What I meant to ask is what would be the approach when I need statistics from several datasets per polygon in the FeatureCollection. For example, for each polygon I would like to get the average slope, the monthly average rain for 2023, and the average NDVI calculated from Landsat 8 for 2021-2023. Do I need to generate three ee.batch.Export, one for the slope, one for the rain, and one for the NDVI or can I somehow include all three in one ee.batch.Export job?
    – user88484
    Dec 10, 2023 at 11:44
  • What you need to do first is make an ee.Image that has average slope, monthly average rain, and average NDVI (three bands). Then use reduceRegions using that image and you will get a table with that data for each of your polygons. Then create one export job to export this table. If you need help with the first step (build the ee.Image that you need) , I recommend making a separate question. Dec 10, 2023 at 12:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.