Earth Engine 中的 Feature 定義為 GeoJSON 地圖項目。具體來說,Feature 是具有 geometry 屬性的物件,可儲存 Geometry 物件 (或空值) 和儲存其他屬性字典的 properties 屬性。
建立特徵物件
如要建立 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'});
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');
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);
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。其他幾何圖形可能儲存在其他屬性中。
Geometry 方法 (例如交集和緩衝區) 也存在於 Feature 上,方便您取得主要 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);
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() 會覆寫現有屬性。