I'm following this example to create zonal statistics for EVI images in Google Earth Engine. In my code (GEE code here) I have a FeatureCollection of points, that creates a 3km buffer for each point, and with it, finally, calculate the mean value for each polygon.
It looks like a very straightforward code but I'm having this headache to get it returning the mean value
When I print(reducer_mean)
it should return the mean values for each region. Instead it's retuning null:
properties: Object (5 properties)
0_0: null
1_1: null
2_2: null
I'm wondering if someone could help me to figure out where the inconsistency of the code are. Because I can manage to find it.
Snapshot of my code:
function create_Landsat_img( year, name ){
// settings for landsat 8
var imgCol = "LANDSAT/LC08/C01/T1_TOA";
var band_nir= "B5";
var band_red= "B4";
var band_blue= "B2";
var img = ee.ImageCollection(imgCol)
.filterBounds(ROI)
.filterDate(year+"-01-01", year+"-12-31")
.filterMetadata('CLOUD_COVER', 'less_than', 95)
.mean()
.clip(ROI);
// calculate the evi
var evi = img.expression("2.5 * ((nir - red) / (nir + 6 * red - 7.5 * blue + 1))",
{
nir : img.select(band_nir),
red : img.select(band_red),
blue: img.select(band_blue)
});
// add the evi to the map
Map.addLayer(evi,{min:0,max:0.85,palette:"blue,red,yellow,green,darkgreen"}, name);
return evi;
}
//--- START SCRIPT ---
//------------------------------------------------------------
// Create a function for Buffering a feature, using the buffer_size
// property of the feature.
var BufferFeature = function(f) {
f = ee.Feature(f);
var buffer_size = f.get('buffer_size');
return f.buffer(buffer_size);
};
var BufferFeaturesByDistance = function (fc, buffer_size) {
// Set a buffer_size property on a feature. Note that this function needs
// to be defined within the BufferFeaturesByDistance function, so it can
// make use of the buffer_size parameter.
var SetBufferSize = function(f) {
return f.set({'buffer_size': buffer_size});
};
return POI.map(SetBufferSize).map(BufferFeature);
};
var buffered = BufferFeaturesByDistance(POI, 3000); // 3km buffer
//------------------------------------------------------------
// Create EVI layers
var i;
var num_years_range = 3;
var images_list = [];
for(i = num_years_range; i >= 1; i--){
var year = 2022;
var yearImg = year-i;
year = yearImg.toString();
var nameLayer= "EVI_"+year;
var EVI_Layer = create_Landsat_img(year, nameLayer);
images_list.push( EVI_Layer );
}
//------------------------------------------------------------
var imgCol = ee.ImageCollection.fromImages(images_list);
print(imgCol);
var rename_band = function(img){
return img.select([0], [img.id()]);
};
// stack all the images into a single image.
var stacked_image = imgCol.map(rename_band).toBands(); //iterate(combine, stacked_image);
print(stacked_image);
// determine scale to perform reduceRegions.
var scale = imgCol.first().projection().nominalScale();
// calculate the timeseries for each feature.
var reducer_mean = ee.Image(stacked_image).reduceRegions({collection: buffered, reducer: ee.Reducer.mean(), scale: scale});
print(reducer_mean);
Export.table.toDrive({
collection: reducer_mean,
description: 'tableOutput',
fileFormat: 'KML',
folder: 'GoogleEarthEngine'
});
// Add to layer
Map.addLayer(buffered, {color:'green'}, 'buffered');
// Zooming to layer
Map.centerObject(POI);