公告:所有在
2025 年 4 月 15 日之前注册使用 Earth Engine 的非商业项目都必须
验证是否符合非商业性质的资格条件,才能继续使用 Earth Engine。
缩减 FeatureCollection
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
如需汇总 FeatureCollection
属性中的数据,请使用 featureCollection.reduceColumns()
。例如,若要检查流域 FeatureCollection
中的面积属性,以下代码会计算相对于 Earth Engine 计算的面积的均方根误差 (RMSE):
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 设置
如需了解 Python API 以及如何使用 geemap
进行交互式开发,请参阅
Python 环境页面。
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)
在此示例中,请注意 reduceColumns()
的返回值是一个键为 ‘mean’
的字典。如需获取平均值,请先使用 ee.Number()
将 dictionary.get()
的结果转换为数字,然后再尝试对其调用 sqrt()
。如需详细了解 Earth Engine 中的辅助数据结构,请参阅此教程。
如需在图像上叠加地图项,请使用 featureCollection.reduceRegions()
。例如,如需计算美国大陆流域的降水量,请使用 reduceRegions()
后跟 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 设置
如需了解 Python API 以及如何使用 geemap
进行交互式开发,请参阅
Python 环境页面。
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)
如需详细了解如何缩减地图项集,请参阅 FeatureCollection 列的统计信息和矢量到光栅转换。
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-07-25。
[null,null,["最后更新时间 (UTC):2025-07-25。"],[[["\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)."]]