FeatureCollection
의 모든 Feature
에 동일한 작업을 적용하려면 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());
import ee import geemap.core as geemap
Colab (Python)
# 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())
이전 예에서 새 속성은 지형지물의 도형을 사용한 계산을 기반으로 설정됩니다. 기존 속성과 관련된 계산을 사용하여 속성을 설정할 수도 있습니다.
map()
를 사용하여 완전히 새로운 FeatureCollection
를 생성할 수 있습니다.
다음 예에서는 유역을 중심점으로 변환합니다.
코드 편집기 (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');
import ee import geemap.core as geemap
Colab (Python)
# 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
속성의 하위 집합만 새 컬렉션의 지형지물에 전파됩니다.