To reduce properties of features in a FeatureCollection
, use
featureCollection.reduceColumns()
. Consider the following toy example:
Code Editor (JavaScript)
// Make a toy FeatureCollection. var aFeatureCollection = ee.FeatureCollection([ ee.Feature(null, {foo: 1, weight: 1}), ee.Feature(null, {foo: 2, weight: 2}), ee.Feature(null, {foo: 3, weight: 3}), ]); // Compute a weighted mean and display it. print(aFeatureCollection.reduceColumns({ reducer: ee.Reducer.mean(), selectors: ['foo'], weightSelectors: ['weight'] }));
Note that the inputs are weighted according to the specified weight
property. The result is therefore:
mean: 2.333333333333333
As a more complex example, consider a FeatureCollection
of US census blocks
with census data as attributes. The variables of interest are total population and
total housing units. You can get their sum(s) by supplying a summing reducer argument
to reduceColumns()
and printing the result:
Code Editor (JavaScript)
// Load US census data as a FeatureCollection. var census = ee.FeatureCollection('TIGER/2010/Blocks'); // Filter the collection to include only Benton County, OR. var benton = census.filter( ee.Filter.and( ee.Filter.eq('statefp10', '41'), ee.Filter.eq('countyfp10', '003') ) ); // Display Benton County census blocks. Map.setCenter(-123.27, 44.57, 13); Map.addLayer(benton); // Compute sums of the specified properties. var properties = ['pop10', 'housing10']; var sums = benton .filter(ee.Filter.notNull(properties)) .reduceColumns({ reducer: ee.Reducer.sum().repeat(2), selectors: properties }); // Print the resultant Dictionary. print(sums);
The output is a Dictionary
representing the aggregated property according
to the specified reducer:
sum: [85579,36245]
Note that the above example uses the notNull()
filter to include only
features with non-null entries for selected properties in the collection being reduced.
It is good practice to check for null entries to catch unexpected missing data and avoid
errors resulting from calculations that include null values.
Also note that unlike imageCollection.reduce()
, in which reducers are
automatically repeated for each band, reducers on a FeatureCollection
must be
explicitly repeated using repeat()
. Specifically, repeat the reducer m
times for m inputs. The following error may be thrown as a result of not repeating
the reducer: