Announcement: All noncommercial projects registered to use Earth Engine before
April 15, 2025 must
verify noncommercial eligibility to maintain Earth Engine access.
Reducing a FeatureCollection
Stay organized with collections
Save and categorize content based on your preferences.
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:
Code Editor (JavaScript)
// Load watersheds from a data table and filter to the continental US.
var sheds = ee.FeatureCollection('USGS/WBD/2017/HUC06')
.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 difference between computed area and the area property.
var diff = area.subtract(ee.Number.parse(feature.get('areasqkm')));
// Return the feature with the squared difference set to the 'diff' property.
return feature.set('diff', diff.pow(2));
};
// Calculate RMSE for population of difference pairs.
var rmse = ee.Number(
// Map the difference function over the collection.
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);
Python setup
See the
Python Environment page for information on the Python API and using
geemap
for interactive development.
import ee
import geemap.core as geemap
Colab (Python)
# Load watersheds from a data table and filter to the continental US.
sheds = ee.FeatureCollection('USGS/WBD/2017/HUC06').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.
def area_diff(feature):
# Compute area in sq. km directly from the geometry.
area = feature.geometry().area().divide(1000 * 1000)
# Compute the difference between computed area and the area property.
diff = area.subtract(ee.Number.parse(feature.get('areasqkm')))
# Return the feature with the squared difference set to the 'diff' property.
return feature.set('diff', diff.pow(2))
# Calculate RMSE for population of difference pairs.
rmse = (
ee.Number(
# Map the difference function over the collection.
sheds.map(area_diff)
# 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.
display('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()
:
Code Editor (JavaScript)
// Load an image of daily precipitation in mm/day.
var precip = ee.Image(ee.ImageCollection('NASA/ORNL/DAYMET_V3').first());
// Load watersheds from a data table and filter to the continental US.
var sheds = ee.FeatureCollection('USGS/WBD/2017/HUC06')
.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())
.filter(ee.Filter.notNull(['prcp']));
// 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)
// Extract the names to a list.
.reduceColumns(ee.Reducer.toList(), ['name']).get('list');
// Print the resulting FeatureCollection.
print(highVolume);
Python setup
See the
Python Environment page for information on the Python API and using
geemap
for interactive development.
import ee
import geemap.core as geemap
Colab (Python)
# Load an image of daily precipitation in mm/day.
precip = ee.Image(ee.ImageCollection('NASA/ORNL/DAYMET_V3').first())
# Load watersheds from a data table and filter to the continental US.
sheds = ee.FeatureCollection('USGS/WBD/2017/HUC06').filterBounds(
ee.Geometry.Rectangle(-127.18, 19.39, -62.75, 51.29)
)
# Add the mean of each image as new properties of each feature.
with_precip = precip.reduceRegions(sheds, ee.Reducer.mean()).filter(
ee.Filter.notNull(['prcp'])
)
# This function computes total rainfall in cubic meters.
def prcp_volume(feature):
# Precipitation in mm/day -> meters -> sq. meters.
volume = (
ee.Number(feature.get('prcp'))
.divide(1000)
.multiply(feature.geometry().area())
)
return feature.set('volume', volume)
high_volume = (
# Map the function over the collection.
with_precip.map(prcp_volume)
# Sort descending and get only the 5 highest volume watersheds.
.sort('volume', False).limit(5)
# Extract the names to a list.
.reduceColumns(ee.Reducer.toList(), ['name']).get('list')
)
# Print the resulting FeatureCollection.
display(high_volume)
For more information about reducing feature collections, see
Statistics of FeatureCollection Columns and
Vector to Raster Conversion.
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2024-06-03 UTC.
[null,null,["Last updated 2024-06-03 UTC."],[[["\u003cp\u003e\u003ccode\u003efeatureCollection.reduceColumns()\u003c/code\u003e aggregates data in the properties of a \u003ccode\u003eFeatureCollection\u003c/code\u003e and returns a dictionary.\u003c/p\u003e\n"],["\u003cp\u003eUse \u003ccode\u003ereduceColumns()\u003c/code\u003e with a reducer such as \u003ccode\u003eee.Reducer.mean()\u003c/code\u003e to calculate statistics across features in a collection, like RMSE in the provided example.\u003c/p\u003e\n"],["\u003cp\u003e\u003ccode\u003efeatureCollection.reduceRegions()\u003c/code\u003e overlays features on imagery and calculates statistics for each feature based on underlying pixel values.\u003c/p\u003e\n"],["\u003cp\u003eCombine \u003ccode\u003ereduceRegions()\u003c/code\u003e with \u003ccode\u003emap()\u003c/code\u003e to compute new properties for each feature derived from the image data, like calculating precipitation volume per watershed in the example.\u003c/p\u003e\n"],["\u003cp\u003eAccess the resulting statistics from the dictionary returned by \u003ccode\u003ereduceColumns()\u003c/code\u003e using \u003ccode\u003edictionary.get()\u003c/code\u003e and cast it to the appropriate data type for further operations.\u003c/p\u003e\n"]]],[],null,["# Reducing a FeatureCollection\n\nTo aggregate data in the properties of a `FeatureCollection`, use\n`featureCollection.reduceColumns()`. For example, to check the area properties\nin the watersheds `FeatureCollection`, this code computes the Root Mean Square\nError (RMSE) relative to the Earth Engine computed area:\n\n### Code Editor (JavaScript)\n\n```javascript\n// Load watersheds from a data table and filter to the continental US.\nvar sheds = ee.FeatureCollection('USGS/WBD/2017/HUC06')\n .filterBounds(ee.Geometry.Rectangle(-127.18, 19.39, -62.75, 51.29));\n\n// This function computes the squared difference between an area property\n// and area computed directly from the feature's geometry.\nvar areaDiff = function(feature) {\n // Compute area in sq. km directly from the geometry.\n var area = feature.geometry().area().divide(1000 * 1000);\n // Compute the difference between computed area and the area property.\n var diff = area.subtract(ee.Number.parse(feature.get('areasqkm')));\n // Return the feature with the squared difference set to the 'diff' property.\n return feature.set('diff', diff.pow(2));\n};\n\n// Calculate RMSE for population of difference pairs.\nvar rmse = ee.Number(\n // Map the difference function over the collection.\n sheds.map(areaDiff)\n // Reduce to get the mean squared difference.\n .reduceColumns(ee.Reducer.mean(), ['diff'])\n .get('mean')\n)\n// Compute the square root of the mean square to get RMSE.\n.sqrt();\n\n// Print the result.\nprint('RMSE=', rmse);\n```\nPython setup\n\nSee the [Python Environment](/earth-engine/guides/python_install) page for information on the Python API and using\n`geemap` for interactive development. \n\n```python\nimport ee\nimport geemap.core as geemap\n```\n\n### Colab (Python)\n\n```python\n# Load watersheds from a data table and filter to the continental US.\nsheds = ee.FeatureCollection('USGS/WBD/2017/HUC06').filterBounds(\n ee.Geometry.Rectangle(-127.18, 19.39, -62.75, 51.29)\n)\n\n# This function computes the squared difference between an area property\n# and area computed directly from the feature's geometry.\ndef area_diff(feature):\n # Compute area in sq. km directly from the geometry.\n area = feature.geometry().area().divide(1000 * 1000)\n # Compute the difference between computed area and the area property.\n diff = area.subtract(ee.Number.parse(feature.get('areasqkm')))\n # Return the feature with the squared difference set to the 'diff' property.\n return feature.set('diff', diff.pow(2))\n\n# Calculate RMSE for population of difference pairs.\nrmse = (\n ee.Number(\n # Map the difference function over the collection.\n sheds.map(area_diff)\n # Reduce to get the mean squared difference.\n .reduceColumns(ee.Reducer.mean(), ['diff']).get('mean')\n )\n # Compute the square root of the mean square to get RMSE.\n .sqrt()\n)\n\n# Print the result.\ndisplay('RMSE=', rmse)\n```\n\nIn this example, note that the return value of `reduceColumns()` is a dictionary\nwith key `'mean'`. To get the mean, cast the result of\n`dictionary.get()` to a number with `ee.Number()` before trying to\ncall `sqrt()` on it. For more information about ancillary\ndata structures in Earth Engine, see [this\ntutorial](/earth-engine/tutorials/tutorial_js_01).\n\nTo overlay features on imagery, use `featureCollection.reduceRegions()`. For\nexample, to compute the volume of precipitation in continental US watersheds, use\n`reduceRegions()` followed by a `map()`:\n\n### Code Editor (JavaScript)\n\n```javascript\n// Load an image of daily precipitation in mm/day.\nvar precip = ee.Image(ee.ImageCollection('NASA/ORNL/DAYMET_V3').first());\n\n// Load watersheds from a data table and filter to the continental US.\nvar sheds = ee.FeatureCollection('USGS/WBD/2017/HUC06')\n .filterBounds(ee.Geometry.Rectangle(-127.18, 19.39, -62.75, 51.29));\n\n// Add the mean of each image as new properties of each feature.\nvar withPrecip = precip.reduceRegions(sheds, ee.Reducer.mean())\n .filter(ee.Filter.notNull(['prcp']));\n\n// This function computes total rainfall in cubic meters.\nvar prcpVolume = function(feature) {\n // Precipitation in mm/day -\u003e meters -\u003e sq. meters.\n var volume = ee.Number(feature.get('prcp'))\n .divide(1000).multiply(feature.geometry().area());\n return feature.set('volume', volume);\n};\n\nvar highVolume = withPrecip\n // Map the function over the collection.\n .map(prcpVolume)\n // Sort descending.\n .sort('volume', false)\n // Get only the 5 highest volume watersheds.\n .limit(5)\n // Extract the names to a list.\n .reduceColumns(ee.Reducer.toList(), ['name']).get('list');\n\n// Print the resulting FeatureCollection.\nprint(highVolume);\n```\nPython setup\n\nSee the [Python Environment](/earth-engine/guides/python_install) page for information on the Python API and using\n`geemap` for interactive development. \n\n```python\nimport ee\nimport geemap.core as geemap\n```\n\n### Colab (Python)\n\n```python\n# Load an image of daily precipitation in mm/day.\nprecip = ee.Image(ee.ImageCollection('NASA/ORNL/DAYMET_V3').first())\n\n# Load watersheds from a data table and filter to the continental US.\nsheds = ee.FeatureCollection('USGS/WBD/2017/HUC06').filterBounds(\n ee.Geometry.Rectangle(-127.18, 19.39, -62.75, 51.29)\n)\n\n# Add the mean of each image as new properties of each feature.\nwith_precip = precip.reduceRegions(sheds, ee.Reducer.mean()).filter(\n ee.Filter.notNull(['prcp'])\n)\n\n\n# This function computes total rainfall in cubic meters.\ndef prcp_volume(feature):\n # Precipitation in mm/day -\u003e meters -\u003e sq. meters.\n volume = (\n ee.Number(feature.get('prcp'))\n .divide(1000)\n .multiply(feature.geometry().area())\n )\n return feature.set('volume', volume)\n\nhigh_volume = (\n # Map the function over the collection.\n with_precip.map(prcp_volume)\n # Sort descending and get only the 5 highest volume watersheds.\n .sort('volume', False).limit(5)\n # Extract the names to a list.\n .reduceColumns(ee.Reducer.toList(), ['name']).get('list')\n)\n\n# Print the resulting FeatureCollection.\ndisplay(high_volume)\n```\n\nFor more information about reducing feature collections, see\n[Statistics of FeatureCollection Columns](/earth-engine/guides/reducers_reduce_columns) and\n[Vector to Raster Conversion](/earth-engine/guides/reducers_reduce_to_image)."]]