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