I have a FeatureCollection
where I would like to take the mean of multiples properties for each feature.
I did not find so far an elegant way to do this, currently I am using using the map
function, mapping over each feature a number operation like: var1.add(var).divide(2)
.
Is there a more elegant way to aggregate over properties in a featureCollection? The problem with my solution is that I can't handle null values, which unfortunately happen often in my dataset. Indeed, using the add()
function will fail in that case, see example below.
Code: https://code.earthengine.google.com/9944b02bc720859307811571fcc3cb0e
var features = ee.FeatureCollection([
ee.Feature(null, {var1: 1, var2: 2}),
ee.Feature(null, {var1: 2, var2: 3}),
ee.Feature(null, {var1: 1, var2: null})]);
print(features)
/// mean
var getMean = function(feature) {
var var1 = ee.Number(feature.get('var1'));
var var2 = ee.Number(feature.get('var2'));
return feature.set({mean: var1.add(var2).divide(5)});
}
// Works:
var test_agg = features.limit(2).map(getMean);
print(test_agg)
// Does not work:
var test_agg2 = features.map(getMean);
print(test_agg2)