公告:所有在
2025 年 4 月 15 日之前注册使用 Earth Engine 的非商业项目都必须
验证是否符合非商业性质的资格条件,才能继续使用 Earth Engine。
ee.FeatureCollection.flatten
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
使集合的集合扁平化。
用法 | 返回 |
---|
FeatureCollection.flatten() | FeatureCollection |
参数 | 类型 | 详细信息 |
---|
此:collection | FeatureCollection | 集合的输入集合。 |
示例
代码编辑器 (JavaScript)
// Counties in New Mexico, USA.
var counties = ee.FeatureCollection('TIGER/2018/Counties')
.filter('STATEFP == "35"');
// Monthly climate and climatic water balance surfaces for January 2020.
var climate = ee.ImageCollection('IDAHO_EPSCOR/TERRACLIMATE')
.filterDate('2020-01', '2020-02');
// Calculate mean climate variables for each county per climate surface
// time step. The result is a FeatureCollection of FeatureCollections.
var countiesClimate = climate.map(function(image) {
return image.reduceRegions({
collection: counties,
reducer: ee.Reducer.mean(),
scale: 5000,
crs: 'EPSG:4326'
});
});
// Note that a printed FeatureCollection of FeatureCollections is not
// recursively expanded, you cannot view metadata of the features within the
// nested collections until you isolate a single collection or flatten the
// collections.
print('FeatureCollection of FeatureCollections', countiesClimate);
print('Flattened FeatureCollection of FeatureCollections',
countiesClimate.flatten());
Python 设置
如需了解 Python API 和如何使用 geemap
进行交互式开发,请参阅
Python 环境页面。
import ee
import geemap.core as geemap
Colab (Python)
# Counties in New Mexico, USA.
counties = ee.FeatureCollection('TIGER/2018/Counties').filter('STATEFP == "35"')
# Monthly climate and climatic water balance surfaces for January 2020.
climate = ee.ImageCollection('IDAHO_EPSCOR/TERRACLIMATE').filterDate(
'2020-01', '2020-02')
# Calculate mean climate variables for each county per climate surface
# time step. The result is a FeatureCollection of FeatureCollections.
def reduce_mean(image):
return image.reduceRegions(**{
'collection': counties,
'reducer': ee.Reducer.mean(),
'scale': 5000,
'crs': 'EPSG:4326'
})
counties_climate = climate.map(reduce_mean)
# Note that a printed FeatureCollection of FeatureCollections is not
# recursively expanded, you cannot view metadata of the features within the
# nested collections until you isolate a single collection or flatten the
# collections.
print('FeatureCollection of FeatureCollections:', counties_climate.getInfo())
print('Flattened FeatureCollection of FeatureCollections:',
counties_climate.flatten().getInfo())
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-07-26。
[null,null,["最后更新时间 (UTC):2025-07-26。"],[[["\u003cp\u003e\u003ccode\u003eflatten()\u003c/code\u003e transforms a FeatureCollection of FeatureCollections into a single FeatureCollection.\u003c/p\u003e\n"],["\u003cp\u003eIt's used to simplify nested collections for easier analysis and data access.\u003c/p\u003e\n"],["\u003cp\u003eThis function is helpful when dealing with results from operations like \u003ccode\u003ereduceRegions()\u003c/code\u003e applied across image collections.\u003c/p\u003e\n"],["\u003cp\u003eThe output of \u003ccode\u003eflatten()\u003c/code\u003e is a FeatureCollection with all the features from the nested collections combined.\u003c/p\u003e\n"]]],["The `flatten()` method transforms a nested `FeatureCollection` of `FeatureCollections` into a single, flat `FeatureCollection`. It takes a `FeatureCollection` as input and returns a flattened `FeatureCollection`. This allows for the metadata of features within the nested collections to be viewed, which is not possible with unflattened collections. An example demonstrates calculating mean climate variables for counties per climate surface timestep and then flattening the resulting nested collection.\n"],null,["# ee.FeatureCollection.flatten\n\nFlattens collections of collections.\n\n\u003cbr /\u003e\n\n| Usage | Returns |\n|-------------------------------|-------------------|\n| FeatureCollection.flatten`()` | FeatureCollection |\n\n| Argument | Type | Details |\n|--------------------|-------------------|--------------------------------------|\n| this: `collection` | FeatureCollection | The input collection of collections. |\n\nExamples\n--------\n\n### Code Editor (JavaScript)\n\n```javascript\n// Counties in New Mexico, USA.\nvar counties = ee.FeatureCollection('TIGER/2018/Counties')\n .filter('STATEFP == \"35\"');\n\n// Monthly climate and climatic water balance surfaces for January 2020.\nvar climate = ee.ImageCollection('IDAHO_EPSCOR/TERRACLIMATE')\n .filterDate('2020-01', '2020-02');\n\n// Calculate mean climate variables for each county per climate surface\n// time step. The result is a FeatureCollection of FeatureCollections.\nvar countiesClimate = climate.map(function(image) {\n return image.reduceRegions({\n collection: counties,\n reducer: ee.Reducer.mean(),\n scale: 5000,\n crs: 'EPSG:4326'\n });\n});\n\n// Note that a printed FeatureCollection of FeatureCollections is not\n// recursively expanded, you cannot view metadata of the features within the\n// nested collections until you isolate a single collection or flatten the\n// collections.\nprint('FeatureCollection of FeatureCollections', countiesClimate);\n\nprint('Flattened FeatureCollection of FeatureCollections',\n countiesClimate.flatten());\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# Counties in New Mexico, USA.\ncounties = ee.FeatureCollection('TIGER/2018/Counties').filter('STATEFP == \"35\"')\n\n# Monthly climate and climatic water balance surfaces for January 2020.\nclimate = ee.ImageCollection('IDAHO_EPSCOR/TERRACLIMATE').filterDate(\n '2020-01', '2020-02')\n\n# Calculate mean climate variables for each county per climate surface\n# time step. The result is a FeatureCollection of FeatureCollections.\ndef reduce_mean(image):\n return image.reduceRegions(**{\n 'collection': counties,\n 'reducer': ee.Reducer.mean(),\n 'scale': 5000,\n 'crs': 'EPSG:4326'\n })\ncounties_climate = climate.map(reduce_mean)\n\n# Note that a printed FeatureCollection of FeatureCollections is not\n# recursively expanded, you cannot view metadata of the features within the\n# nested collections until you isolate a single collection or flatten the\n# collections.\nprint('FeatureCollection of FeatureCollections:', counties_climate.getInfo())\n\nprint('Flattened FeatureCollection of FeatureCollections:',\n counties_climate.flatten().getInfo())\n```"]]