Earth Engine の Feature
は GeoJSON 対象物として定義されます。具体的には、Feature
は、Geometry
オブジェクト(または null)を格納する geometry
プロパティと、他のプロパティの辞書を格納する properties
プロパティを持つオブジェクトです。
Feature オブジェクトの作成
Feature
を作成するには、コンストラクタに Geometry
と、必要に応じて他のプロパティの辞書を指定します。次に例を示します。
// 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
# 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
は、検査と可視化のために印刷または地図に追加できます。
print(polyFeature); Map.addLayer(polyFeature, {}, 'feature');
import ee import geemap.core as geemap
display(poly_feature) m = geemap.Map() m.add_layer(poly_feature, {}, 'feature') display(m)
Feature
に Geometry
がなくてもかまいません。プロパティの辞書をラップするだけでもかまいません。次に例を示します。
// 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
# 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
プロパティに 1 つのプライマリ Geometry
が保存されています。追加のジオメトリは他のプロパティに格納できます。Geometry
メソッド(交差やバッファなど)は、プライマリ Geometry
の取得、オペレーションの適用、結果を新しいプライマリ Geometry
として設定するための便利なメソッドとして Feature
にも存在します。結果には、メソッドが呼び出された Feature
の他のすべてのプロパティが保持されます。また、Feature
のジオメトリ以外のプロパティを取得および設定するメソッドもあります。次に例を示します。
// 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
# 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)
前の例では、プロパティは Key-Value ペアまたは辞書で設定できます。また、feature.set()
は既存のプロパティを上書きします。