Google Earth Engine – Reducing featureCollection by Using Group in JavaScript API

google-earth-enginegoogle-earth-engine-javascript-api

Suppose I have a featurecollection, including two aa.

How can I reduce (maybe other functions) the featurecollection by calculate the mean value of aa?

Before

 var features = [
  ee.Feature(ee.Geometry.Point(11, 22), {name: 'aa',value:10}),
  ee.Feature(ee.Geometry.Point(33, 44), {name: 'bb',value:20}),
  ee.Feature(ee.Geometry.Point(11, 22), {name: 'aa',value:30}),
  ee.Feature(ee.Geometry.Point(55, 66), {name: 'cc',value:40})
];
var fromList = ee.FeatureCollection(features);

After

var features = [
  ee.Feature(ee.Geometry.Point(11, 22), {name: 'aa',value:20}), // (10+30)/2
  ee.Feature(ee.Geometry.Point(33, 44), {name: 'bb',value:20}),
  ee.Feature(ee.Geometry.Point(55, 66), {name: 'cc',value:40})
];

Best Answer

Basically you want to itirate over the unique identifier, name in your case. and filter all features that match it, compute the mean and overwrite it. This should do the trick:

var reduce = fromList.distinct('name')

var uniqueNames = reduce.reduceColumns(ee.Reducer.toList(), ['name']).values().get(0)

var iterate = ee.List(uniqueNames).map(function(n){
  var filt = fromList.filter(ee.Filter.eq('name', n)) 
  // mean
  var mean = filt.aggregate_mean('value')
  
  // overwrite
    return ee.Feature(filt.first().set('value', mean));
  
})

https://code.earthengine.google.com/2307664929ac4485abc1c8708c5e3d6f

Related Question