Mapping over a FeatureCollection

To apply the same operation to every Feature in a FeatureCollection, use featureCollection.map(). For example, to add another area attribute to every feature in a watersheds FeatureCollection, use:

// Load watersheds from a Fusion Table.
var sheds = ee.FeatureCollection('ft:1IXfrLpTHX4dtdj1LcNXjJADBB-d93rkdJ9acSEWK');

// 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());
    

In the previous example, note that a new property is set based on a computation with the feature’s geometry. Properties can also be set using a computation involving existing properties.

An entirely new FeatureCollection can be generated with map(). The following example converts the watersheds to centroids:

// 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 and print the first few.
Map.addLayer(centroids, {color: 'FF0000'}, 'centroids');
print('Centroids: ', centroids.limit(5));
    

Note that only a subset of properties is propagated to the features in the new collection.

Send feedback about...

Google Earth Engine API