公告:所有在
2025 年 4 月 15 日之前注册使用 Earth Engine 的非商业项目都必须
验证是否符合非商业性质的资格条件,才能继续使用 Earth Engine。
ee.FeatureCollection.map
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
将算法映射到集合。
返回映射的集合。
用法 | 返回 |
---|
FeatureCollection.map(algorithm, dropNulls) | 集合 |
参数 | 类型 | 详细信息 |
---|
此:collection | 集合 | Collection 实例。 |
algorithm | 函数 | 要对集合中的图片或特征进行映射的操作。一个 JavaScript 函数,用于接收图片或特征并返回一个。该函数仅被调用一次,结果会作为说明捕获,因此它无法执行命令式操作或依赖外部状态。 |
dropNulls | 布尔值,可选 | 如果为 true,则允许映射的算法返回 null,并且将舍弃返回 null 的元素。 |
示例
代码编辑器 (JavaScript)
// FeatureCollection of power plants in Belgium.
var fc = ee.FeatureCollection('WRI/GPPD/power_plants')
.filter('country_lg == "Belgium"');
// Function to convert power plant capacity from megawatts to gigawatts and
// add the value as a new feature property.
var mwToGw = function(feature) {
var megawatt = feature.getNumber('capacitymw');
var gigawatt = megawatt.divide(1000);
return feature.set('capacitygw', gigawatt);
};
// Apply the function to each feature in the collection.
fc = fc.map(mwToGw);
print('Note the new "capacitygw" property in each feature', fc);
Python 设置
如需了解 Python API 和如何使用 geemap
进行交互式开发,请参阅
Python 环境页面。
import ee
import geemap.core as geemap
Colab (Python)
# FeatureCollection of power plants in Belgium.
fc = ee.FeatureCollection('WRI/GPPD/power_plants').filter(
'country_lg == "Belgium"')
# Function to convert power plant capacity from megawatts to gigawatts and
# add the value as a new feature property.
def mw_to_gw(feature):
megawatt = feature.getNumber('capacitymw')
gigawatt = megawatt.divide(1000)
return feature.set('capacitygw', gigawatt)
# Apply the function to each feature in the collection.
fc = fc.map(mw_to_gw)
print('Note the new "capacitygw" property in each feature:', fc.getInfo())
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-07-26。
[null,null,["最后更新时间 (UTC):2025-07-26。"],[[["\u003cp\u003e\u003ccode\u003emap()\u003c/code\u003e iterates over a FeatureCollection, applying a user-defined function to each feature.\u003c/p\u003e\n"],["\u003cp\u003eThe provided function transforms features and returns modified versions, creating a new FeatureCollection.\u003c/p\u003e\n"],["\u003cp\u003e\u003ccode\u003emap()\u003c/code\u003e allows adding/modifying properties of features within a FeatureCollection or performing calculations based on existing properties.\u003c/p\u003e\n"],["\u003cp\u003eAn optional \u003ccode\u003edropNulls\u003c/code\u003e parameter controls whether features resulting in null from the mapping function are retained in the output.\u003c/p\u003e\n"]]],["The `map` algorithm applies a user-defined function to each element within a collection (e.g., features or images). This function, which accepts an element as input and returns a modified one, is applied to each item of the collection. The `dropNulls` argument allows filtering null return values. The result is a new collection containing the modified elements. For example, converting power plant capacity units and adding them as a new property to the features.\n"],null,["# ee.FeatureCollection.map\n\n\u003cbr /\u003e\n\nMaps an algorithm over a collection.\n\n\u003cbr /\u003e\n\nReturns the mapped collection.\n\n| Usage | Returns |\n|---------------------------------------------------|------------|\n| FeatureCollection.map`(algorithm, `*dropNulls*`)` | Collection |\n\n| Argument | Type | Details |\n|--------------------|-------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| this: `collection` | Collection | The Collection instance. |\n| `algorithm` | Function | The operation to map over the images or features of the collection. A JavaScript function that receives an image or features and returns one. The function is called only once and the result is captured as a description, so it cannot perform imperative operations or rely on external state. |\n| `dropNulls` | Boolean, optional | If true, the mapped algorithm is allowed to return nulls, and the elements for which it returns nulls will be dropped. |\n\nExamples\n--------\n\n### Code Editor (JavaScript)\n\n```javascript\n// FeatureCollection of power plants in Belgium.\nvar fc = ee.FeatureCollection('WRI/GPPD/power_plants')\n .filter('country_lg == \"Belgium\"');\n\n// Function to convert power plant capacity from megawatts to gigawatts and\n// add the value as a new feature property.\nvar mwToGw = function(feature) {\n var megawatt = feature.getNumber('capacitymw');\n var gigawatt = megawatt.divide(1000);\n return feature.set('capacitygw', gigawatt);\n};\n\n// Apply the function to each feature in the collection.\nfc = fc.map(mwToGw);\n\nprint('Note the new \"capacitygw\" property in each feature', fc);\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# FeatureCollection of power plants in Belgium.\nfc = ee.FeatureCollection('WRI/GPPD/power_plants').filter(\n 'country_lg == \"Belgium\"')\n\n# Function to convert power plant capacity from megawatts to gigawatts and\n# add the value as a new feature property.\ndef mw_to_gw(feature):\n megawatt = feature.getNumber('capacitymw')\n gigawatt = megawatt.divide(1000)\n return feature.set('capacitygw', gigawatt)\n\n# Apply the function to each feature in the collection.\nfc = fc.map(mw_to_gw)\n\nprint('Note the new \"capacitygw\" property in each feature:', fc.getInfo())\n```"]]