1

I am trying to compute the total forest loss based on the Hansen dataset in Google Earth Engine.

I would like to know the total number of pixels lost per polygon of a shapefile imported as a feature collection.

The code runs and the tasks appear on the task tab, but when I run it I get an error:

Error: Feature, argument 'metadata': Invalid type. Expected: Dictionary. Actual: FeatureCollection.

This is my code:

var table = ee.FeatureCollection("users/alejandrofeged/Colombian-map");
Map.addLayer(table,{},"Colombia")

var image = ee.Image("UMD/hansen/global_forest_change_2017_v1_5")
  .clipToCollection(table);

var lossImage = image.select(['loss']);
Map.addLayer(lossImage.updateMask(lossImage),
    {palette: ['FF0000']}, 'Loss');



Export.image.toDrive({
  image: lossImage,
  description: 'GFW-2000-2017',
  scale: 30,
  region: table,
  maxPixels: 8813288590
});


var means = lossImage.reduceRegions({
  collection: table,
  reducer: ee.Reducer.sum(),
  scale: 30,
});

// Print the first feature, to illustrate the result.
print(ee.Feature(means.first()));

var feature = ee.Feature(null, means);

// Wrap the Feature in a FeatureCollection for export.
var forestloss = ee.FeatureCollection([feature]);

// Export the FeatureCollection.
Export.table.toDrive({
  collection: forestloss,
  description: 'forestloss',
  fileFormat: 'CSV'
});
  • You've provided very little information about your problem (what is the error message), and no one can run your script because your assets are not shared. – Jon Aug 16 '18 at 21:57
  • apologies. I have made the asset public and published the error. – supercontra Aug 16 '18 at 22:44
  • We need the code here, not in some other site -- If the link breaks, the value of the question is destroyed. – Vince Aug 17 '18 at 0:55
0

The function Image.reduceRegions returns a FeatureCollection, so exporting the complete collection would be:

// Export the FeatureCollection.
Export.table.toDrive({
  collection: means,
  description: 'forestloss',
  fileFormat: 'CSV'
});

And exporting only the first Feature would be:

var first = ee.Feature(means.first());
var forestloss = ee.FeatureCollection([first]);

// Export the FeatureCollection.
Export.table.toDrive({
  collection: forestloss,
  description: 'forestloss',
  fileFormat: 'CSV'
});

Finally, exporting a selected feature by its position in the collection:

var feat = 4;
var feature = ee.Feature(means.toList(means.size()).get(feat))
var forestloss = ee.FeatureCollection([feature]);

// Export the FeatureCollection.
Export.table.toDrive({
  collection: forestloss,
  description: 'forestloss',
  fileFormat: 'CSV'
});
  • Any way to name the column of the output? – supercontra Aug 17 '18 at 17:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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