Отображение через FeatureCollection,Отображение через FeatureCollection

Чтобы применить одну и ту же операцию к каждому Feature в FeatureCollection , используйте featureCollection.map() . Например, чтобы добавить еще один атрибут области к каждому объекту в FeatureCollection водоразделов, используйте:

Редактор кода (JavaScript)

// Load watersheds from a data table.
var sheds = ee.FeatureCollection('USGS/WBD/2017/HUC06');

// This function computes the feature's geometry area and adds it as a property.
var addArea = function(feature) {
  return feature.set({areaHa: feature.geometry().area().divide(100 * 100)});
};

// Map the area getting function over the FeatureCollection.
var areaAdded = sheds.map(addArea);

// Print the first feature from the collection with the added property.
print('First feature:', areaAdded.first());

Настройка Python

См. страницу «Среда Python» для получения информации об API Python и использовании geemap для интерактивной разработки.

import ee
import geemap.core as geemap

Колаб (Питон)

# Load watersheds from a data table.
sheds = ee.FeatureCollection('USGS/WBD/2017/HUC06')

# Map an area calculation function over the FeatureCollection.
area_added = sheds.map(
    lambda feature: feature.set(
        {'areaHa': feature.geometry().area().divide(100 * 100)}
    )
)

# Print the first feature from the collection with the added property.
display('First feature:', area_added.first())

Обратите внимание, что в предыдущем примере новое свойство устанавливается на основе вычислений с учетом геометрии объекта. Свойства также можно задать с помощью вычислений, включающих существующие свойства.

Совершенно новую FeatureCollection можно создать с помощью map() . В следующем примере водоразделы преобразуются в центроиды:

Редактор кода (JavaScript)

// This function creates a new feature from the centroid of the geometry.
var getCentroid = function(feature) {
  // Keep this list of properties.
  var keepProperties = ['name', 'huc6', 'tnmid', 'areasqkm'];
  // Get the centroid of the feature's geometry.
  var centroid = feature.geometry().centroid();
  // Return a new Feature, copying properties from the old Feature.
  return ee.Feature(centroid).copyProperties(feature, keepProperties);
};

// Map the centroid getting function over the features.
var centroids = sheds.map(getCentroid);

// Display the results.
Map.addLayer(centroids, {color: 'FF0000'}, 'centroids');

Настройка Python

См. страницу «Среда Python» для получения информации об API Python и использовании geemap для интерактивной разработки.

import ee
import geemap.core as geemap

Колаб (Питон)

# This function creates a new feature from the centroid of the geometry.
def get_centroid(feature):
  # Keep this list of properties.
  keep_properties = ['name', 'huc6', 'tnmid', 'areasqkm']
  # Get the centroid of the feature's geometry.
  centroid = feature.geometry().centroid()
  # Return a new Feature, copying properties from the old Feature.
  return ee.Feature(centroid).copyProperties(feature, keep_properties)

# Map the centroid getting function over the features.
centroids = sheds.map(get_centroid)

# Display the results.
m = geemap.Map()
m.set_center(-96.25, 40, 4)
m.add_layer(centroids, {'color': 'FF0000'}, 'centroids')
m

Обратите внимание, что на объекты в новой коллекции распространяется только подмножество свойств.

,

Чтобы применить одну и ту же операцию к каждому Feature в FeatureCollection , используйте featureCollection.map() . Например, чтобы добавить еще один атрибут области к каждому объекту в FeatureCollection водоразделов, используйте:

Редактор кода (JavaScript)

// Load watersheds from a data table.
var sheds = ee.FeatureCollection('USGS/WBD/2017/HUC06');

// This function computes the feature's geometry area and adds it as a property.
var addArea = function(feature) {
  return feature.set({areaHa: feature.geometry().area().divide(100 * 100)});
};

// Map the area getting function over the FeatureCollection.
var areaAdded = sheds.map(addArea);

// Print the first feature from the collection with the added property.
print('First feature:', areaAdded.first());

Настройка Python

См. страницу «Среда Python» для получения информации об API Python и использовании geemap для интерактивной разработки.

import ee
import geemap.core as geemap

Колаб (Питон)

# Load watersheds from a data table.
sheds = ee.FeatureCollection('USGS/WBD/2017/HUC06')

# Map an area calculation function over the FeatureCollection.
area_added = sheds.map(
    lambda feature: feature.set(
        {'areaHa': feature.geometry().area().divide(100 * 100)}
    )
)

# Print the first feature from the collection with the added property.
display('First feature:', area_added.first())

Обратите внимание, что в предыдущем примере новое свойство устанавливается на основе вычислений с учетом геометрии объекта. Свойства также можно задать с помощью вычислений, включающих существующие свойства.

Совершенно новую FeatureCollection можно создать с помощью map() . В следующем примере водоразделы преобразуются в центроиды:

Редактор кода (JavaScript)

// This function creates a new feature from the centroid of the geometry.
var getCentroid = function(feature) {
  // Keep this list of properties.
  var keepProperties = ['name', 'huc6', 'tnmid', 'areasqkm'];
  // Get the centroid of the feature's geometry.
  var centroid = feature.geometry().centroid();
  // Return a new Feature, copying properties from the old Feature.
  return ee.Feature(centroid).copyProperties(feature, keepProperties);
};

// Map the centroid getting function over the features.
var centroids = sheds.map(getCentroid);

// Display the results.
Map.addLayer(centroids, {color: 'FF0000'}, 'centroids');

Настройка Python

См. страницу «Среда Python» для получения информации об API Python и использовании geemap для интерактивной разработки.

import ee
import geemap.core as geemap

Колаб (Питон)

# This function creates a new feature from the centroid of the geometry.
def get_centroid(feature):
  # Keep this list of properties.
  keep_properties = ['name', 'huc6', 'tnmid', 'areasqkm']
  # Get the centroid of the feature's geometry.
  centroid = feature.geometry().centroid()
  # Return a new Feature, copying properties from the old Feature.
  return ee.Feature(centroid).copyProperties(feature, keep_properties)

# Map the centroid getting function over the features.
centroids = sheds.map(get_centroid)

# Display the results.
m = geemap.Map()
m.set_center(-96.25, 40, 4)
m.add_layer(centroids, {'color': 'FF0000'}, 'centroids')
m

Обратите внимание, что на объекты в новой коллекции распространяется только подмножество свойств.