מיפוי על FeatureCollection
קל לארגן דפים בעזרת אוספים
אפשר לשמור ולסווג תוכן על סמך ההעדפות שלך.
כדי להחיל את אותה פעולה על כל Feature
ב-FeatureCollection
, משתמשים ב-featureCollection.map()
. לדוגמה, כדי להוסיף מאפיין שטח נוסף לכל מאפיין בקטגוריית שטחי המים FeatureCollection
, משתמשים בקוד הבא:
Code Editor (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 מפורט מידע על Python API ועל השימוש ב-geemap
לפיתוח אינטראקטיבי.
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())
בדוגמה הקודמת, שימו לב שהנכס החדש מוגדר על סמך חישוב עם הגיאומטריה של התכונה. אפשר להגדיר מאפיינים גם באמצעות חישוב שמערב מאפיינים קיימים.
אפשר ליצור FeatureCollection
חדש לגמרי באמצעות map()
.
בדוגמה הבאה מתבצעת המרה של תחומי הספיקה למרכזי מסה:
Code Editor (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 מפורט מידע על Python API ועל השימוש ב-geemap
לפיתוח אינטראקטיבי.
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
חשוב לזכור שרק קבוצת משנה של נכסים מועברת לתכונות באוסף החדש.
אלא אם צוין אחרת, התוכן של דף זה הוא ברישיון Creative Commons Attribution 4.0 ודוגמאות הקוד הן ברישיון Apache 2.0. לפרטים, ניתן לעיין במדיניות האתר Google Developers. Java הוא סימן מסחרי רשום של חברת Oracle ו/או של השותפים העצמאיים שלה.
עדכון אחרון: 2025-07-25 (שעון UTC).
[null,null,["עדכון אחרון: 2025-07-25 (שעון UTC)."],[[["\u003cp\u003e\u003ccode\u003efeatureCollection.map()\u003c/code\u003e applies an operation to each \u003ccode\u003eFeature\u003c/code\u003e within a \u003ccode\u003eFeatureCollection\u003c/code\u003e, enabling modifications or calculations on a feature-by-feature basis.\u003c/p\u003e\n"],["\u003cp\u003eThis function can be used to add new properties based on geometric calculations or existing property values, illustrated by calculating and adding an area property to each feature.\u003c/p\u003e\n"],["\u003cp\u003eIt is also possible to generate an entirely new \u003ccode\u003eFeatureCollection\u003c/code\u003e by applying a transformation function that creates new features, as demonstrated by converting polygons to their centroids.\u003c/p\u003e\n"],["\u003cp\u003eWhen creating new features within the \u003ccode\u003emap()\u003c/code\u003e function, selective property propagation is achievable, allowing control over which attributes are transferred to the new \u003ccode\u003eFeatureCollection\u003c/code\u003e.\u003c/p\u003e\n"]]],[],null,["# Mapping over a FeatureCollection\n\nTo apply the same operation to every `Feature` in a\n`FeatureCollection`, use `featureCollection.map()`. For example,\nto add another area attribute to every feature in a watersheds\n`FeatureCollection`, use:\n\n### Code Editor (JavaScript)\n\n```javascript\n// Load watersheds from a data table.\nvar sheds = ee.FeatureCollection('USGS/WBD/2017/HUC06');\n\n// This function computes the feature's geometry area and adds it as a property.\nvar addArea = function(feature) {\n return feature.set({areaHa: feature.geometry().area().divide(100 * 100)});\n};\n\n// Map the area getting function over the FeatureCollection.\nvar areaAdded = sheds.map(addArea);\n\n// Print the first feature from the collection with the added property.\nprint('First feature:', areaAdded.first());\n```\nPython setup\n\nSee the [Python Environment](/earth-engine/guides/python_install) page for information on the Python API and using\n`geemap` for interactive development. \n\n```python\nimport ee\nimport geemap.core as geemap\n```\n\n### Colab (Python)\n\n```python\n# Load watersheds from a data table.\nsheds = ee.FeatureCollection('USGS/WBD/2017/HUC06')\n\n# Map an area calculation function over the FeatureCollection.\narea_added = sheds.map(\n lambda feature: feature.set(\n {'areaHa': feature.geometry().area().divide(100 * 100)}\n )\n)\n\n# Print the first feature from the collection with the added property.\ndisplay('First feature:', area_added.first())\n```\n\nIn the previous example, note that a new property is set based on a computation with the\nfeature's geometry. Properties can also be set using a computation involving existing\nproperties.\n\nAn entirely new `FeatureCollection` can be generated with `map()`.\nThe following example converts the watersheds to centroids:\n\n### Code Editor (JavaScript)\n\n```javascript\n// This function creates a new feature from the centroid of the geometry.\nvar getCentroid = function(feature) {\n // Keep this list of properties.\n var keepProperties = ['name', 'huc6', 'tnmid', 'areasqkm'];\n // Get the centroid of the feature's geometry.\n var centroid = feature.geometry().centroid();\n // Return a new Feature, copying properties from the old Feature.\n return ee.Feature(centroid).copyProperties(feature, keepProperties);\n};\n\n// Map the centroid getting function over the features.\nvar centroids = sheds.map(getCentroid);\n\n// Display the results.\nMap.addLayer(centroids, {color: 'FF0000'}, 'centroids');\n```\nPython setup\n\nSee the [Python Environment](/earth-engine/guides/python_install) page for information on the Python API and using\n`geemap` for interactive development. \n\n```python\nimport ee\nimport geemap.core as geemap\n```\n\n### Colab (Python)\n\n```python\n# This function creates a new feature from the centroid of the geometry.\ndef get_centroid(feature):\n # Keep this list of properties.\n keep_properties = ['name', 'huc6', 'tnmid', 'areasqkm']\n # Get the centroid of the feature's geometry.\n centroid = feature.geometry().centroid()\n # Return a new Feature, copying properties from the old Feature.\n return ee.Feature(centroid).copyProperties(feature, keep_properties)\n\n# Map the centroid getting function over the features.\ncentroids = sheds.map(get_centroid)\n\n# Display the results.\nm = geemap.Map()\nm.set_center(-96.25, 40, 4)\nm.add_layer(centroids, {'color': 'FF0000'}, 'centroids')\nm\n```\n\nNote that only a subset of properties is propagated to the features in the new collection."]]