功能概览

Earth Engine 中的 Feature 被定义为 GeoJSON 地图项。具体而言,Feature 是一个对象,其 geometry 属性存储 Geometry 对象(或 null),properties 属性存储其他属性的字典。

创建 Feature 对象

如需创建 Feature,请向构造函数提供 Geometry 和(可选)其他属性的字典。例如:

代码编辑器 (JavaScript)

// Create an ee.Geometry.
var polygon = ee.Geometry.Polygon([
  [[-35, -10], [35, -10], [35, 10], [-35, 10], [-35, -10]]
]);

// Create a Feature from the Geometry.
var polyFeature = ee.Feature(polygon, {foo: 42, bar: 'tart'});

Python 设置

如需了解 Python API 和如何使用 geemap 进行交互式开发,请访问 Python 环境页面。

import ee
import geemap.core as geemap

Colab (Python)

# Create an ee.Geometry.
polygon = ee.Geometry.Polygon(
    [[[-35, -10], [35, -10], [35, 10], [-35, 10], [-35, -10]]]
)

# Create a Feature from the Geometry.
poly_feature = ee.Feature(polygon, {'foo': 42, 'bar': 'tart'})

与 Geometry 一样,Feature 也可以打印或添加到地图中,以便进行检查和可视化:

代码编辑器 (JavaScript)

print(polyFeature);
Map.addLayer(polyFeature, {}, 'feature');

Python 设置

如需了解 Python API 和如何使用 geemap 进行交互式开发,请访问 Python 环境页面。

import ee
import geemap.core as geemap

Colab (Python)

display(poly_feature)
m = geemap.Map()
m.add_layer(poly_feature, {}, 'feature')
display(m)

Feature 不需要具有 Geometry,只需封装属性字典即可。例如:

代码编辑器 (JavaScript)

// Create a dictionary of properties, some of which may be computed values.
var dict = {foo: ee.Number(8).add(88), bar: 'nihao'};

// Create a null geometry feature with the dictionary of properties.
var nowhereFeature = ee.Feature(null, dict);

Python 设置

如需了解 Python API 和如何使用 geemap 进行交互式开发,请访问 Python 环境页面。

import ee
import geemap.core as geemap

Colab (Python)

# Create a dictionary of properties, some of which may be computed values.
dic = {'foo': ee.Number(8).add(88), 'bar': 'nihao'}

# Create a null geometry feature with the dictionary of properties.
nowhere_feature = ee.Feature(None, dic)

在此示例中,请注意,提供给 Feature 的字典包含一个计算值。以这种方式创建特征对于导出具有 Dictionary 结果(例如 image.reduceRegion())的长时间运行的计算非常有用。如需了解详情,请参阅 FeatureCollections 和导入表数据或导出指南。

每个 Feature 都有一个存储在 geometry 属性中的主要 Geometry。其他几何图形可以存储在其他属性中。 Feature 上也存在 Geometry 方法(例如交集和缓冲区),以便于获取主 Geometry、应用操作并将结果设置为新的主 Geometry。 结果将保留调用该方法的 Feature 的所有其他属性。此外,还有一些方法可用于获取和设置 Feature 的非几何属性。例如:

代码编辑器 (JavaScript)

// Make a feature and set some properties.
var feature = ee.Feature(ee.Geometry.Point([-122.22599, 37.17605]))
  .set('genus', 'Sequoia').set('species', 'sempervirens');

// Get a property from the feature.
var species = feature.get('species');
print(species);

// Set a new property.
feature = feature.set('presence', 1);

// Overwrite the old properties with a new dictionary.
var newDict = {genus: 'Brachyramphus', species: 'marmoratus'};
var feature = feature.set(newDict);

// Check the result.
print(feature);

Python 设置

如需了解 Python API 和如何使用 geemap 进行交互式开发,请访问 Python 环境页面。

import ee
import geemap.core as geemap

Colab (Python)

# Make a feature and set some properties.
feature = (
    ee.Feature(ee.Geometry.Point([-122.22599, 37.17605]))
    .set('genus', 'Sequoia')
    .set('species', 'sempervirens')
)

# Get a property from the feature.
species = feature.get('species')
display(species)

# Set a new property.
feature = feature.set('presence', 1)

# Overwrite the old properties with a new dictionary.
new_dic = {'genus': 'Brachyramphus', 'species': 'marmoratus'}
feature = feature.set(new_dic)

# Check the result.
display(feature)

在前面的示例中,请注意,可以使用键值对或字典来设置属性。另请注意,feature.set() 会覆盖现有属性。