Reducing a FeatureCollection

To aggregate data in the properties of a FeatureCollection, use featureCollection.reduceColumns(). For example, to check the area properties in the watersheds FeatureCollection, this code computes the Root Mean Square Error (RMSE) relative to the Earth Engine computed area:

// Load watersheds from a Fusion Table and filter to the continental US.
var sheds = ee.FeatureCollection('ft:1IXfrLpTHX4dtdj1LcNXjJADBB-d93rkdJ9acSEWK')
  .filterBounds(ee.Geometry.Rectangle(-127.18, 19.39, -62.75, 51.29));

// This function computes the squared difference between an area property
// and area computed directly from the feature's geometry.
var areaDiff = function(feature) {
  // Compute area in sq. km. directly from the geometry.
  var area = feature.geometry().area().divide(1000 * 1000);
  // Compute the differece between computed area and the area property.
  var diff = area.subtract(feature.get('AreaSqKm'));
  // Return the feature with the squared difference set to the 'diff' property.
  return feature.set('diff', diff.pow(2));
};

// Map the difference function over the collection.
var rmse = ee.Number(sheds
    .map(areaDiff)
    // Reduce to get the mean squared difference.
    .reduceColumns(ee.Reducer.mean(), ['diff'])
    .get('mean'))
    // Compute the square root of the mean square to get RMSE.
    .sqrt();

// Print the result.
print('RMSE=', rmse);
    

In this example, note that the return value of reduceColumns() is a dictionary with key ‘mean’. To get the mean, cast the result of dictionary.get() to a number with ee.Number() before trying to call sqrt() on it. For more information about ancillary data structures in Earth Engine, see this tutorial.

To overlay features on imagery, use featureCollection.reduceRegions(). For example, to compute the volume of precipitation in continental US watersheds, use reduceRegions() followed by a map():

// Load an image of daily precipitation in mm/day.
var precip = ee.Image(ee.ImageCollection('NASA/ORNL/DAYMET').first());

// Load watersheds from a Fusion Table and filter to the continental US.
var sheds = ee.FeatureCollection('ft:1IXfrLpTHX4dtdj1LcNXjJADBB-d93rkdJ9acSEWK')
  .filterBounds(ee.Geometry.Rectangle(-127.18, 19.39, -62.75, 51.29));

// Add the mean of each image as new properties of each feature.
var withPrecip = precip.reduceRegions(sheds, ee.Reducer.mean());

// This function computes total rainfall in cubic meters.
var prcpVolume = function(feature) {
  // Precipitation in mm/day -> meters -> sq. meters.
  var volume = ee.Number(feature.get('prcp'))
    .divide(1000).multiply(feature.geometry().area());
  return feature.set('volume', volume);
};

var highVolume = withPrecip
  // Map the function over the collection.
  .map(prcpVolume)
  // Sort descending.
  .sort('volume', false)
  // Get only the 5 highest volume watersheds.
  .limit(5);

// Print the resulting FeatureCollection
print(highVolume);
    

For more information about reducing feature collections, see Statistics of FeatureCollection Columns and Vector to Raster Conversion.

发送以下问题的反馈:

此网页
Google Earth Engine API