My goal is to calculate mean EVI for a collection of polygons for each image in an image collection.
My collection of polygons is a GEE feature collection (called 'polys') and I'm using the Landsat 8 EVI image collection.
I've been able to create a script that executes this task for a single polygon in this feature collection, posted below and link here.
My question is how do I take this script and scale it up to execute the task for my whole collection of polygons? Script I'm working on is also below and here.
Everything runs smoothly up until step 4 (mapping a reduceRegion/(s) function across the image collection), where earth engine returns a 'computation timed out' or an 'earth engine memory capacity exceeded' error. I've tried breaking both the image collection and the feature collection into smaller chunks with no success. I wonder if exporting a task to my google drive could be a solution, but I'm unsure of how to write the correct code for this.
Scripts:
1) Mean EVI for a single polygon for each image in a collection (works):
// 1. Filter 'polys' feature collection by property 'Name'.
var NU009 = polys.filterMetadata('Name', 'equals', 'NU009');
// 2. Get Landsat 8 EVI collection and filter to NU009.
var l8 = ee.ImageCollection('LANDSAT/LC08/C01/T1_8DAY_EVI')
.filterBounds(NU009)
.filterDate('2014-01-01','2018-12-30');
// 3. Clip each Landsat image to the area covered by NU009.
var l8_clip = l8.map(function(image) {
return image.clip(NU009);
});
// 4. Calculate mean EVI for NU009 in each image.
//Define a function that calculates mean EVI for NU009.
//Returns an image collection where each image has a new property,
//'meanEVI'.
var reducer = function(image) {
var meanEVI = image.reduceRegion({
reducer: ee.Reducer.mean(),
geometry: NU009.geometry(),
scale: 30,
bestEffort: true
});
return image.set('meanEVI', meanEVI);
};
//Map the reducer function over the image collection.
var l8_reduce = l8_clip.map(reducer);
print(l8_reduce);
2) Mean EVI for all polygons for each image in a collection (computation times out):
// 1. Filter 'polys' by size.
// Define a function that calculates area of each polygon.
// New property 'GEE_Area' is in square meters.
var feature_area = function(feature) {
var area = feature.geometry().area();
return feature.set('GEE_Area', area);
};
polys = polys.map(feature_area);
// Filter polys by their GEE_Area.
// Must be greater than 30 square meters (900m) to be sufficiently
//represented by a Landsat pixel.
var polys_filtered = polys.filterMetadata('GEE_Area', 'not_less_than', 900);
// 2. Get Landsat EVI image collection.
var l8 = ee.ImageCollection('LANDSAT/LC08/C01/T1_8DAY_EVI')
.filterBounds(polys_filtered)
.filterDate('2014-01-01','2018-12-31');
// 3. Clip each Landsat image to the area covered by all polygons.
var l8_clip = l8.map(function(image) {
return image.clipToCollection(polys_filtered);
});
// 4. Calculate mean EVI for each polygon in each image.
// Define a function that calculates mean EVI for all polygons using
//'reduceRegion' method.
var reduceRegion = function(image) {
var meanEVI = image.reduceRegion({
reducer: ee.Reducer.mean(),
geometry: polys_filtered.geometry(),
scale: 30,
bestEffort: true
});
return image.set('meanEVI', meanEVI);
};
var l8_reduceRegion = l8_clip.map(reduceRegion);
print(l8_reduceRegion);
// Alternative to above function:
// Define a function that calculates mean EVI for all polygons using
//'reduceRegions' method.
var reduceRegions = function(image) {
var meanEVI = image.reduceRegions({
collection: polys_filtered,
reducer: ee.Reducer.mean(),
scale: 30});
return meanEVI;
};
var l8_reduceRegions = l8_clip.map(reduceRegions);
print(l8_reduceRegions);