1

I would like to do an analysis for a few countries. I want to write a loop that calculates the loss for a given slope for each country. I have a problem with geometry in reduceRegion function. It doesn't accept the geometry I provide. I already did the same analysis for a country(with peru.geometry()), and it worked. But now, I am getting the following error when I call countries from a list by .get(), List (Error) Image.reduceRegion, argument 'geometry': Invalid type. Expected type: Geometry. Actual type: Feature.

enter image description here

How can I get the geometry of a feature in GEE? and How can I add this geometry to the code? here is the link for my script: https://code.earthengine.google.com/3a7d3053935e83b15e216dbef2056522

1 Answer 1

1

You have to cast the ee.Feature() to a ee.Geometry(). From get(), the GEE does not now what kind of object is returned. Cast is to an ee.Feature() and then get the geometry from that feature using .geometry()

var loss =  function(d) {
    var loss1 = arealossinForest.addBands(lossyearinforest)
                  .updateMask(slclasses.get(1))
                  .reduceRegion({
                     reducer: ee.Reducer.sum().group({
                      groupField: 1
                        }),
                        geometry: ee.Feature(countries.get(d)).geometry(),
                        maxPixels: 1e9,
                        bestEffort: true});
             return loss1 };

A second option would be to make the variable countries an ee.FeatureCollection and use .reduceRegions():

var countries = ee.FeatureCollection([argentina, vietnam]);

var loss =  function(d) {
    var loss1 = arealossinForest.addBands(lossyearinforest)
                  .updateMask(slclasses.get(1))
                  .reduceRegions({
                     reducer: ee.Reducer.sum().group({
                      groupField: 1
                        }),
                        collection: countries,
                        tileScale: 8}); // higher number will speed up calculation
             return loss1 };

As you do not have the arguments bestEffort and maxPixels, it might not be preferred for your situation.

1
  • It worked, Thank you!! Commented Mar 17, 2021 at 12:57

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.