Feature
в Earth Engine определяется как функция GeoJSON. В частности, Feature
— это объект со свойством geometry
хранящим объект Geometry
(или значение NULL), и свойством 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
Колаб (Питон)
# 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
Колаб (Питон)
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
Колаб (Питон)
# 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 и Importing Table Data или Exporting .
Каждый 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);
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)
Обратите внимание, что в предыдущем примере свойства можно задать либо с помощью пары ключ-значение, либо с помощью словаря. Также обратите внимание, что feature.set()
перезаписывает существующие свойства.