Earth Engine の Feature は、GeoJSON Feature として定義されます。具体的には、Feature は、Geometry オブジェクト(または null)を格納する 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'});
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 プロパティに格納された 1 つのプライマリ Geometry があります。追加のジオメトリは、他のプロパティに保存される場合があります。
intersection や buffer などの Geometry メソッドは、プライマリ Geometry の取得、オペレーションの適用、新しいプライマリ Geometry としての結果の設定を容易にするために、Feature にも存在します。結果には、メソッドが呼び出された 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)
上記の例では、プロパティは Key-Value ペアまたは辞書で設定できます。また、feature.set() は既存のプロパティを上書きします。