Search code examples
google-earth-engine

How can I have the percentage instead of the Frequency in Google Earth Engine?


I asked this question on GIS StackExchange, but no luck so far. I think that maybe it belongs here.

I used the following script :

// define the var
var Catchment = /* color: 98ff00 */geometry;
var landcover = ee.Image('users/roynahas/ESACCI-LC-L4-LCCS-Map-300m-P5Y-2010-v161_RECLASS').select('b1');
// Clip the image to the polygon geometry and add it to the map
var landcover_clip = landcover.clip(Catchment);
var sld_intervals =
'<RasterSymbolizer>' +
 '<ColorMap  type="intervals" extended="false" >' +
 '<ColorMapEntry color="#FFFF00" quantity="1" label="Agriculture"/>' +
 '<ColorMapEntry color="#00FF00" quantity="2" label="Grassland and Shrubland"/>' +
 '<ColorMapEntry color="#008000" quantity="3" label="Forest"/>' +
 '<ColorMapEntry color="#00FFFF" quantity="4" label="Flooded"/>' +
 '<ColorMapEntry color="#FF00FF" quantity="5" label="Urban areas"/>' +
 '<ColorMapEntry color="#808080" quantity="6" label="Bare areas"/>' +
 '<ColorMapEntry color="#0000FF" quantity="7" label="Water"/>' +
 '<ColorMapEntry color="#FFFFFF" quantity="8" label="Permanent snow and ice"/>' +
 '</ColorMap>' +
'</RasterSymbolizer>';
Map.addLayer(landcover_clip.sldStyle(sld_intervals), {}, 'IGBP classification styled');
// Print out the frequency of landcover occurrence for the polygon.
var frequency = landcover.reduceRegion({
  reducer:ee.Reducer.frequencyHistogram(),
  geometry:Catchment,
  scale:300
});
print('landcover frequency', frequency.get('b1'));

enter image description here

To get the following console output:

enter image description here

So my question is : How can I have a percentage instead of a frequency? or in other words : Is there a percentage equivalent to ee.Reducer.frequencyHistogram() ?


Solution

  • I got the answer on another forum. Here it is:

    // Import variables
    var globcover = ee.Image("ESA/GLOBCOVER_L4_200901_200912_V2_3"),
        geometry = /* color: d63000 */ee.Geometry.Polygon(
            [[[-71.466064453125, 48.763431137917955],
              [-71.378173828125, 48.89000369970676],
              [-72.674560546875, 49.38952445158216],
              [-73.179931640625, 49.106241774469055],
              [-73.575439453125, 48.27588152743497],
              [-72.83935546875, 48.10743118848039],
              [-71.4935302734375, 48.17341248658084],
              [-71.455078125, 48.56024979174331],
              [-71.5374755859375, 48.68370757165362]]]);
    // Extract the landcover band
    var landcover = globcover.select('landcover');
    // Clip the image to the polygon geometry
    var landcover_roi = landcover.clip(geometry);
    // Add a map layer of the landcover clipped to the polygon.
    Map.addLayer(landcover_roi);
    // Print out the frequency of landcover occurrence for the polygon.
    var frequency = landcover.reduceRegion({
      reducer:ee.Reducer.frequencyHistogram(),
      geometry:geometry,
      scale:1000
    });
    var dict = ee.Dictionary(frequency.get('landcover'));
    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);
    

    Of course, the inputs (the land cover image and polygon layer) can be different.