# How can I have the percentage instead of the Frequency in Google Earth Engine? (Edited question)

EDIT : Since I had 2 questions in one, I edited it to only have one (second part here.)

EDIT 2 : I got a partial answer on another forum and made my script more universal

I have yet another question related to my last post.

I used the following script :

``````// Input layers
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.
// Print out the frequency of landcover occurrence for the polygon.
var frequency = landcover.reduceRegion({
reducer:ee.Reducer.frequencyHistogram(),
geometry:geometry,
scale:1000
});
print('landcover frequency', frequency.get('landcover'));

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);
});
print(new_dict);
``````

This outputs the land cover has a value between 0 and 1. I need to multiply by 100 to get a percentage.

• Your script does not include enough information to run it. In particular, could you modify it so that the variable "Catchment" is explicitly defined. Also the asset 'users/roynahas/ESACCI-LC-L4-LCCS-Map-300m-P5Y-2010-v161_RECLASS' is private. Could you replace it with a similar non-private asset? – Tyler Erickson Feb 26 '16 at 21:23
• @TylerErickson : The variable "Catchment" is defined by the variable "geometry" of an imported polygon (I posted a screen capture of my script). And the private asset is a land cover image I uploaded. It can be replaced by any other land cover image to run the script (like the GlobCover 2009). – Nahas Feb 26 '16 at 21:44
• @TylerErickson : Do you need more information? – Nahas Feb 29 '16 at 18:55
• I would suggest updating the script so that people do not have do modify it in order to get it to run (i.e. define a geometry in the script and use a publicly accessible landcover dataset like GlobCover). Make it easier for people to answer your question... help people help you. – Tyler Erickson Mar 2 '16 at 23:08

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.
// 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.