I’m using google earth engine to create a table showing monthly averages of band values, which are also averaged across some lakes. Here is an example of my code.
First, I pull down the sentinel 2 data and create a monthly composite. I pretty much used the code from this stack exchange post
//some made up geometry
var geometry =
/* color: #d63000 */
/* displayProperties: [
{
"type": "rectangle"
}
] */
ee.Geometry.Polygon(
[[[-113.0857421875, 41.602598130446786],
[-113.0857421875, 40.509091454509885],
[-111.6794921875, 40.509091454509885],
[-111.6794921875, 41.602598130446786]]], null, false);
//download sentinel-2 data most recent five years
var dataset = ee.ImageCollection('COPERNICUS/S2_SR_HARMONIZED')
.filterDate('2019-01-01', '2024-01-01')
.filterBounds(geometry)
//Monthly Images
var months = ee.List.sequence(1,12);
var years = ee.List.sequence(2019,2023);
// monthly composite
var monthlyImages = ee.ImageCollection.fromImages(
years.map(function (y) {
return months.map(function(m){
var w = dataset.select([ 'B2', 'B3', 'B4', 'B5', 'B6', 'B7', 'B8', 'B8A', 'B11', 'B12']).filter(ee.Filter.calendarRange(y, y, 'year'))
.filter(ee.Filter.calendarRange(m, m, 'month'))
.mean()
.set('system:time_start',ee.Date.fromYMD(y,m,1).millis());
return w.set('year', y)
.set('month', m)
.set('date', ee.Date.fromYMD(y,m,1))
.set('system:time_start',ee.Date.fromYMD(y,m,1).millis())
});
}).flatten());
print(monthlyImages)
Next, I want to go through my shape (in this example called geometry) and create an average value for every band per month.
so first I have a function for zonal statistics
//function to extract mean values
function zonalStats(ic, fc, params) {
// Initialize internal params dictionary.
var _params = {
reducer: ee.Reducer.mean(),
scale: null,
crs: null,
bands: null,
bandsRename: null,
imgProps: null,
imgPropsRename: null,
datetimeName: 'datetime',
datetimeFormat: 'YYYY-MM-dd HH:mm:ss'
};
// Replace initialized params with provided params.
if (params) {
for (var param in params) {
_params[param] = params[param] || _params[param];
}
}
// Set default parameters based on an image representative.
var imgRep = ic.first();
var nonSystemImgProps = ee.Feature(null)
.copyProperties(imgRep).propertyNames();
if (!_params.bands) _params.bands = imgRep.bandNames();
if (!_params.bandsRename) _params.bandsRename = _params.bands;
if (!_params.imgProps) _params.imgProps = nonSystemImgProps;
if (!_params.imgPropsRename) _params.imgPropsRename = _params.imgProps;
// Map the reduceRegions function over the image collection.
var results = ic.map(function(img) {
// Select bands (optionally rename), set a datetime & timestamp property.
img = ee.Image(img.select(_params.bands, _params.bandsRename))
.set(_params.datetimeName, img.date().format(_params.datetimeFormat))
.set('timestamp', img.get('system:time_start'));
// Define final image property dictionary to set in output features.
var propsFrom = ee.List(_params.imgProps)
.cat(ee.List([_params.datetimeName, 'timestamp']));
var propsTo = ee.List(_params.imgPropsRename)
.cat(ee.List([_params.datetimeName, 'timestamp']));
var imgProps = img.toDictionary(propsFrom).rename(propsFrom, propsTo);
// Subset points that intersect the given image.
var fcSub = fc.filterBounds(img.geometry());
// Reduce the image by regions.
return img.reduceRegions({
collection: fcSub,
reducer: _params.reducer,
scale: _params.scale,
crs: _params.crs
})
// Add metadata to each feature.
.map(function(f) {
return f.set(imgProps);
});
}).flatten().filter(ee.Filter.notNull(_params.bandsRename));
return results;
}
then I set my params and try to run it
// Define parameters for the zonalStats function.
var params = {
reducer: ee.Reducer.mean(),
scale: 20,
bands: [ 'B2', 'B3', 'B4', 'B5', 'B6', 'B7', 'B8', 'B8A', 'B11', 'B12'],
bandsRename: ['blue', 'green', 'red', 'red_edge1', 'red_edge2', 'red_edge3', 'NIR', 'red_edge4', 'SWIR_1', 'SWIR_2'],
datetimeName: 'date',
datetimeFormat: 'YYYY-MM-dd'
};
// Extract zonal statistics per point per image.
var SentinelStats = zonalStats(monthlyImages, geometry, params);
but I get this error
FeatureCollection (Error) Error in map(ID=0): Dictionary.rename: Duplicate key: [date].