Panoramica delle funzionalità

Un Feature in Earth Engine è definito come un elemento GeoJSON. Nello specifico, un Feature è un oggetto con una proprietà geometry che memorizza un oggetto Geometry (o null) e una proprietà properties che memorizza un dizionario di altre proprietà.

Creazione di oggetti Feature

Per creare un Feature, fornisci al costruttore un Geometry e (facoltativo) un dizionario di altre proprietà. Ad esempio:

Editor di codice (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'});

Configurazione di Python

Per informazioni sull'API Python e sull'utilizzo di geemap per lo sviluppo interattivo, consulta la pagina Ambiente Python.

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'})

Come per un Geometry, un Feature può essere stampato o aggiunto alla mappa per ispezione e visualizzazione:

Editor di codice (JavaScript)

print(polyFeature);
Map.addLayer(polyFeature, {}, 'feature');

Configurazione di Python

Per informazioni sull'API Python e sull'utilizzo di geemap per lo sviluppo interattivo, consulta la pagina Ambiente Python.

import ee
import geemap.core as geemap

Colab (Python)

display(poly_feature)
m = geemap.Map()
m.add_layer(poly_feature, {}, 'feature')
display(m)

Un Feature non deve avere un Geometry e può semplicemente avvolgere un dizionario di proprietà. Ad esempio:

Editor di codice (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);

Configurazione di Python

Per informazioni sull'API Python e sull'utilizzo di geemap per lo sviluppo interattivo, consulta la pagina Ambiente Python.

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)

In questo esempio, tieni presente che il dizionario fornito a Feature contiene un valore calcolato. La creazione di elementi in questo modo è utile per esportare calcoli di lunga durata con un risultato Dictionary (ad es. image.reduceRegion()). Per maggiori dettagli, consulta le guide FeatureCollections e Importazione dei dati delle tabelle o Esportazione.

Ogni Feature ha un Geometry principale memorizzato nella proprietà geometry. Altre geometrie possono essere memorizzate in altre proprietà. Su Feature esistono anche metodi Geometry come intersezione e buffer per ottenere il Geometry principale, applicare l'operazione e impostare il risultato come nuovo Geometry principale. Il risultato manterrà tutte le altre proprietà dell'oggetto Feature su cui viene chiamato il metodo. Esistono anche metodi per ottenere e impostare le proprietà non geometriche di Feature. Ad esempio:

Editor di codice (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);

Configurazione di Python

Per informazioni sull'API Python e sull'utilizzo di geemap per lo sviluppo interattivo, consulta la pagina Ambiente Python.

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)

Nell'esempio precedente, tieni presente che le proprietà possono essere impostate con una coppia chiave-valore o con un dizionario. Tieni inoltre presente che feature.set() sovrascrive le proprietà esistenti.