I'm calculating a time series for a 7-year period over a grid tessellation AOI. My goal is to have a CSV of zonal statistics for all features in a feature collection from 3 different bands in an image collection.
To do this, I'm running reduceRegion() on an image collection using a combined reducer, over a feature collection of 55,000 grid cells. The image collection has 3 different image bands (NDBI, NDVI, UI). I want to copy the six resulting reduction values (2 for each of the 3 bands) to each feature as properties, then export those values as columns in a csv. However, I am having trouble actually copying the results of the reduction to the feature collection as properties.
My Script (https://code.earthengine.google.com/b58961f43a176ea520400c36a4940de1)
What I have tried:
- A nested
.map()
function that reducesRegion by each image in a collection, and then maps those results to the feature collection (line 125-136):
// combine the max, mean reducers
var reducers = ee.Reducer.mean().combine({
reducer2: ee.Reducer.count(),
sharedInputs: true
});
var scale = ee.Image(l8sr_bands.first()).projection().nominalScale();
// try copying image bands as a feature property?
// this works w/o error, but no bands are copied to feature properties. reducer values must be returned or copied back to featureCol, but not sure where.
var l8_data = table.map(function(feature) {
return l8sr_bands.map(function(image) {
return ee.Feature(feature)
image.reduceRegion({
reducer: reducers,
geometry: feature.geometry(),
bestEffort: true,
scale: scale
}).copyProperties(image).copyProperties(feature);
});
}).flatten();
print("output test 1: ", l8_data.first());
- Alternate code that copies the six reducer results (mean and sum for all 3 bands) as properties to the images (line 149-162). This does not do exactly what I want, because it produces results as properties of an image collection. I want to map these properties to the feature collection.
var reducers = ee.Reducer.mean().combine({
reducer2: ee.Reducer.count(),
sharedInputs: true
});
var scale = ee.Image(l8sr_bands.first()).projection().nominalScale();
var getStats = (function(img) {
var image = ee.Image(img);
var reducers = ee.Reducer.mean().combine({
reducer2: ee.Reducer.count(),
sharedInputs: true
});
var stats = image.reduceRegion({
reducer: reducers,
geometry: table.geometry(),
bestEffort: true,
scale: scale
});
return image.setMulti(stats);
})
// map all stats onto the feature collection while copying the image properties as feature properties
// result = sum and mean for each band as image properties. how do I map this to the feature collection?
var stats_fc = table.map(function(feature) {
return l8sr_bands.map(getStats);
}).flatten();
print("output test 2: ", stats_fc.first());
I believe I'm missing a return
or feature.set()
statement somewhere to copy the reducer values back to the features, but I'm not sure where to do this. I'm new to GEE and JS, so I'm sure it's something simple.
I am aware that there are similar questions already on GIS SE, such as this question and ReduceRegions not writting to feature properties in GEE, as well as the developer's forum, but posted solutions have not worked for me.