Earth Engine 中的 Feature
定义为 GeoJSON 地图项。具体而言,Feature
是一种具有 geometry
属性(用于存储 Geometry
对象或 null)和 properties
属性(用于存储其他属性的字典)的对象。
创建 Feature 对象
如需创建 Feature
,请向构造函数提供 Geometry
和(可选)其他属性的字典。例如:
Code Editor (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'});
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
也可以打印或添加到地图中以供检查和直观呈现:
Code Editor (JavaScript)
print(polyFeature); Map.addLayer(polyFeature, {}, 'feature');
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
,而可以简单地封装属性字典。例如:
Code Editor (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);
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
的非几何图形属性的方法。例如:
Code Editor (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);
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()
会覆盖现有属性。