[GIS] Using reduceRegions to calculate landcover

google-earth-engine

I am trying to use reduceRegions across a number of polygons to extract landcover classes and calculate the percentage frequency per polygon. I can do this fine with reduceregion but I am struggling with doing this across multiple polygons. The end result I am aiming for is the percent of the different land cover types in each polygon (my buffers around the points) that I can then extract to .csv.

I have read through the different help pages and debugging and I am aware I am probably missing something very simple / obvious. However I am very new to EarthEngine and JavaScript and I just can't see how I translate .reduceRegion solutions to reduceRegions.

Example script is below:

//Load in landcover map

    var globcov = ee.Image("ESA/GLOBCOVER_L4_200901_200912_V2_3");

    var landcover = globcov.select("landcover");
    print(landcover)
    //make a few random points using the add geometry

    var points = [
      ee.Feature(ee.Geometry.Point(-11.73, 9.3), {name: 'A'}),
      ee.Feature(ee.Geometry.Point(-10.530, 9.28104), {name: 'B'}),
      ee.Feature(ee.Geometry.Point(-11.530, 9.48104), {name: 'C'}),
      ee.Feature(ee.Geometry.Point(-10.830, 9.33104), {name: 'D'})
    ];

    var ex_points = ee.FeatureCollection(points);
    print(ex_points);

    //put a buffer round to make the different regions
    var buff = function(feature){
        return feature.buffer(5000);
    };
    var buff5000 = ex_points.map(buff)

    Map.addLayer(buff5000)

    //calculate the histogram with reduce.regions

    var varslc = landcover.reduceRegions({
      reducer:ee.Reducer.frequencyHistogram(),
      collection: buff5000,
      scale:1000,
    });

    print(ee.Feature(varslc),{},"varslc");

    //try to recover the histogram information - This is the first problem
    print('landcover frequency', varslc.get('landcover'));


    //The below code works on the output from .reduceRegion but not for .reduceRegions
    //I think the problem is the "get" command 
    //Get command always returns null
    //Not sure what I should reference to using .reduceRegions compared to .reduceRegion to get the correct values from the dictionary

    var dict = ee.Dictionary(varslc.get('value'));
    print(dict, {},"dict") //This is always null

    //Below calcuates the percent on a single polygon fine but I cant translate to the multiple polygon

    var sum = ee.Array(dict.values()).reduce(ee.Reducer.sum(),[0]).get([0]);
    var new_dict = dict.map(function(k,v) {
      return ee.Number(v).divide(sum).multiply(100);
    });

    print('Land Cover (%)',new_dict);

Best Answer

Your Reducer is working as expected, the error is in your print statement.

Change the last line to get your frequency histograms.

Either for all of the points:

// print the FeatureCollection with all 4 points
print(varslc)

Or just a single point

// print only the histogram of the first feature
print(varslc.first().get("histogram"));