1

I have managed to solve my problem, see worked example below, leaving this question in case it's helpful for anyone. Though, as a new user I would be interested if anyone has any advice of a more efficient way of doing this:

I'm calculating the potential for carbon accumulation from forest regrowth in Key Biodiversity Areas within a specific spatial polygon (of migratory bird flyways). My goal is to have a CSV of the area of potential carbon accumulation from forest regrowth of all features in the feature collection.

I have used reduceRegion() on a single image (carbon) to successfully identify the area of a single feature, and now I want to use this over a feature collection of 4,256 features (kba_EA) and store the results in a dictionary including Area and SitRecID (unique KBA identifier).

My code: (https://code.earthengine.google.com/6f3aaa9266954ddc79f636436b2a70c2?asset=projects%2Fee-priority-landscapes%2Fassets%2FKBAsGlobal_09_2022)


// // // // //////////////////////////////////////////////////////////////////////////////////////////////////////////

// Convert kba area into m2 from hectares
var kbam2 = kba.map(function(f) {
    var Area_m2 = ee.Number(f.get("GISArea")).multiply(10000);
    return f.set({"SitRecID": f.get("SitRecID"),
                     "Area_m2": Area_m2});
});

// Limit kba area to EA polygon
var kba_EA = ee.FeatureCollection(kbam2).filterBounds(EA);

// Limit carbon image to EA polygon
var carbon_EA = ee.Image(carbon).clip(EA).select('b1');


// // // // /////////////////////////////////////////////// // // // // 

// Test using a single feature
var kba_EA_1 = kba_EA.limit(1);

// Calculate the pixel area in square kilometer
var area_carbon = carbon_EA.multiply(ee.Image.pixelArea()).divide(1000*1000);

// Sum the area of potential carbon accumulation in a single kba
var area = area_carbon.reduceRegion({
  reducer: ee.Reducer.sum(),
  geometry: kba_EA_1,
  crsTransform: carbon_EA.projection().getInfo().transform
  });
  
print(area);


// // // // /////////////////////////////////////////////// // // // // 

I then converted the image into an image collection and mapped the function across the feature collection, see code below which is a worked example for 3 features. 'Carbon Area', as the potential for carbon accumulation from forest regrowth in km2 is now saved as a property within each feature

// // // // /////////////////////////////////////////////// // // // // 

// Example using three features
var kba_EA_3 = kba_EA.limit(3);

// Function to convert pixel values to area in m2
var pixel_areas = function(image) {
  return image.multiply(ee.Image.pixelArea());
};

// Turn carbon image into an image collection
var carbon_stack = ee.ImageCollection([carbon_EA]);

// Map function across the crop_stack image collection
var carbon_stack_area = carbon_stack.map(pixel_areas);

//// Calculate total area of potential carbon accumulation for each kba in the feature collection
var results = carbon_stack_area.map(function(image) {
  return image.reduceRegions({
    collection: kba_EA_3,
    reducer: ee.Reducer.sum(),
  }).map(function(f) {
    var carbon_sum = ee.Number(f.get("sum")).divide(1000000);
    return f.set({"SitRecID": f.get("SitRecID"), "Carbon_Area": carbon_sum});
  });
});

// Flatten the results
var carbon_results = ee.FeatureCollection(results).flatten();

// Function to remove geometry from features 
var remove_geometry = function(feature) {
  return feature.setGeometry(null)};

// Map function to remove geomety from features over the whole feature collection
var carbon_results_table = carbon_results.map(remove_geometry);

print(carbon_results_table);

I'm a new user so I'm sure there is a better way to do this and I would be very interested to learn how to do so!

1 Answer 1

0

For avoiding following error message:

List (Error) User memory limit exceeded.

you can export your Feature Collection directly to Drive as CSV as follows:

var getAreaId = kba_EA.toList(kba_EA.size()).map(function (ele) {
  
  var area = area_carbon.reduceRegion({
    reducer: ee.Reducer.sum(),
    geometry: ee.Feature(ele).geometry(),
  });
  
  return ee.Feature(null).set('SitRecID', ee.Feature(ele).get('SitRecID'))
                         .set('area', area.get('b1')); 
});

// Export features, specifying corresponding names.
Export.table.toDrive({
  collection: ee.FeatureCollection(getAreaId),
  folder: "GEE_Folder", //my export folder
  description: 'getAreaId',  //file name
  fileFormat: "CSV"}); 

Complete code can be accessed here.

After running it in GEE code editor, I got a CSV of following picture with 4257 records in only 3 minutes.

enter image description here

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.