功能總覽

Earth Engine 中的 Feature 定義為 GeoJSON 地圖項目。具體來說,Feature 是含有 geometry 屬性的物件,用於儲存 Geometry 物件 (或空值),以及儲存其他屬性的字典的 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 環境」頁面,瞭解 Python API 和如何使用 geemap 進行互動式開發。

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 環境」頁面,瞭解 Python API 和如何使用 geemap 進行互動式開發。

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 環境」頁面,瞭解 Python API 和如何使用 geemap 進行互動式開發。

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 環境」頁面,瞭解 Python API 和如何使用 geemap 進行互動式開發。

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() 會覆寫現有屬性。