Announcement: All noncommercial projects registered to use Earth Engine before April 15, 2025 must verify noncommercial eligibility to maintain Earth Engine access.
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:
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():
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["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)."]]