Kyle Hemes
Kyle Hemes

Reputation: 3

In Google Earth Engine: Most efficiently reduceRegions over each image in ImageCollection, saving mean as a Feature property?

I have a FeatureCollection made up of many (100-200) polygons ('ftr_polygons'). I also have an ImageCollection made up of monthly median Landsat8 bands and indices ('byMonth'). I want to ReduceRegions and save a median (or mean) spatial average from each polygon in the FeatureCollection. End goal is to export to csv a timeseries of monthly mean bands/indices within each polygons over multiple years (2013-2019).

With the code below, I am able to do this for ~1 year, but any more than that, and I get an error: 'FeatureCollection (Error) Computation timed out’. Is there a better way to do this?

// define the function that will grab median (or mean) spatial reductions for each polygon, for each month
var extractdata = function(medianImage,ftr_polygons) { 
      var date_start = ee.Date(medianImage.get('system:time_start')).format("YYYY-MM"); // get date as string to append to each property
      // spatial MEDIAN
      ftr_polygons = medianImage.reduceRegions({ // create feature collection with new properties, bands for each month, uniquely named
        collection: ftr_polygons,
        reducer: ee.Reducer.median(), 
        scale: 30,
        tileScale: 1});  // tile scale 
      var ftr_polygons_propnames = ftr_polygons.first().propertyNames(); // get property names first
      var ftr_polygons_newnames = ftr_polygons_propnames.replace('NDVI_median', 
      ee.String('NDVI_median_').cat(date_start)); //replace property names with band+date
      ftr_polygons_newnames = ftr_polygons_newnames.replace('EVI_median',
      ee.String('EVI_median_').cat(date_start)); //replace property names with band+date
      ftr_polygons_newnames = ftr_polygons_newnames.replace('NIRv_median',
      ee.String('NIRv_median_').cat(date_start)) ; //replace property names with band+date
      ftr_polygons = ftr_polygons.map(function(f) {return f.select(ftr_polygons_propnames,ftr_polygons_newnames)}); 
return ftr_polygons;
};

// apply the function over ImageCollection byMonth, beginning with feature collection ftr_polygons
var ftr_polygons = ee.FeatureCollection(byMonth.iterate(extractdata,ftr_polygons));

// remove geometry on each feature before printing or exporting
var myproperties=function(feature){
  feature=ee.Feature(feature).setGeometry(null);
  return feature;
};
var ftr_polygon_export = ftr_polygon.map(myproperties)
print(ftr_polygon_export.limit(1), 'For export w monthly properties');

Maybe this answer: https://stackoverflow.com/a/48412324/12393507 alludes to a better way:

The same approach can be used with reduceRegions() as well, mapping over images and then over regions. However, you will have to map over the resulting features to set dates.

I would appreciate more info on this approach.

Thanks.

Upvotes: 0

Views: 2628

Answers (1)

JonasV
JonasV

Reputation: 1031

For computationally intensive operations that will run for a long time you should always export your results instead of visualizing/printing them.

For more info read through this section of the debugging page in the Earth Engine manual.

Upvotes: 0

Related Questions