Statistics of FeatureCollection Columns

To reduce properties of features in a FeatureCollection, use featureCollection.reduceColumns(). Consider the following toy example:

// 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 counties 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:

// Load a collection of US counties with census data properties and display it.
var counties = ee.FeatureCollection('ft:1S4EB6319wWW2sWQDPhDvmSBIVrD3iEmCLYB7nMM');
Map.addLayer(counties);

// Compute sums of the specified properties and print the resultant Dictionary.
var sums = counties
  .filter(ee.Filter.and(
    ee.Filter.neq('Census 2000 Population', null),
    ee.Filter.neq('Census 2000 Housing Units', null)))
  .reduceColumns({
    reducer: ee.Reducer.sum().repeat(2),
    selectors: ['Census 2000 Population', 'Census 2000 Housing Units'],
});
print(sums);
    

The output is a Dictionary representing the aggregated property according to the specified reducer:

sum: [279162260,115048233]
    

Note that because feature collections may contain missing data (unlike images, which handle missing data with masks), the input needs to be pre-filtered to eliminate null values.

An error that looks something like the following may be thrown as a result of attributes with 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:

Enviar comentarios sobre…

Google Earth Engine API