Feature
trong Earth Engine được xác định là một đối tượng GeoJSON. Cụ thể, Feature
là một đối tượng có thuộc tính geometry
lưu trữ đối tượng Geometry
(hoặc rỗng) và thuộc tính properties
lưu trữ từ điển của các thuộc tính khác.
Tạo đối tượng Feature
Để tạo Feature
, hãy cung cấp cho hàm khởi tạo một Geometry
và (không bắt buộc) một từ điển gồm các thuộc tính khác. Ví dụ:
Trình soạn thảo mã (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'})
Cũng như Geometry
, bạn có thể in hoặc thêm Feature
vào bản đồ để kiểm tra và trực quan hoá:
Trình soạn thảo mã (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
không cần có Geometry
và có thể chỉ cần gói một từ điển thuộc tính. Ví dụ:
Trình soạn thảo mã (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)
Trong ví dụ này, hãy lưu ý rằng từ điển được cung cấp cho Feature
chứa một giá trị được tính toán. Việc tạo tính năng theo cách này rất hữu ích khi xuất các phép tính chạy trong thời gian dài với kết quả Dictionary
(ví dụ: image.reduceRegion()
). Hãy xem hướng dẫn về FeatureCollections và Nhập dữ liệu bảng hoặc Xuất để biết thông tin chi tiết.
Mỗi Feature
có một Geometry
chính được lưu trữ trong thuộc tính geometry
. Các hình học bổ sung có thể được lưu trữ trong các thuộc tính khác.
Các phương thức Geometry
như giao điểm và vùng đệm cũng tồn tại trên Feature
để thuận tiện cho việc lấy Geometry
chính, áp dụng toán tử và đặt kết quả làm Geometry
chính mới.
Kết quả sẽ giữ lại tất cả các thuộc tính khác của Feature
mà phương thức được gọi. Ngoài ra, còn có các phương thức để lấy và đặt các thuộc tính không phải hình học của Feature
. Ví dụ:
Trình soạn thảo mã (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)
Trong ví dụ trước, xin lưu ý rằng bạn có thể đặt thuộc tính bằng cặp khoá-giá trị hoặc bằng từ điển. Ngoài ra, xin lưu ý rằng feature.set()
sẽ ghi đè các thuộc tính hiện có.