I need help scripting in Google Earth Engine

  • Asked
  • Active
  • Viewed 11615 times
0

I'm new to scripting so I need some help.

So I used the following script:

var geometry : Polygon, 15 vertices
// define the var
var Catchment = /* color: 98ff00 */geometry;
var landcover = ee.Image('*LINK TO A LAND COVER IMAGE*').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'));

To get the following console output:

1: 13401
2: 58801
3: 338570
4: 1141
5: 1120
6: 81
7: 35860

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() ?*

And while we're at it, I have another question : *How can I change the labels in the legend to have "Agriculture", "Grass and Shrubland", "Forest", "Flooded", "Urban areas", "Bare areas", "Water", and "Permanent snow and ice" instead of "1", "2", "3", "4", "5", "6", "7", and "8"?*

Thank you very much!

0

Accepted answer

Dear Nahas,

For this kind of questions on Google Earth Engine you will probably find better feedback on the Google Earth Engine support forum

From the top of my head maybe it is better that you present the information using a histogram chart, something like this :

Slope on a plot

Or even how the percentage of days a plot has an NDVI over a threshold is shown in our standard GEE script :

Standard Collect Earth GEE script

Anyway, for this you will get better answers from the GEE support forum!

Your answer

Markdown text. Shortcuts: Ctrl+B bold, Ctrl+I italic, Ctrl+K link, Ctrl+E code, Ctrl+Shift+V paste as plain text. Tab indents list items and code; press Escape, then Tab, to leave the text box.

Insert an image

Say what the image shows, for people who can't see it.

Menu

Keyboard shortcuts

Anywhere

?
Show this list
/
Search
g h
Go to the home page
g q
Go to the questions
g t
Go to the tags
g u
Go to the users
a
Ask a question

Lists of questions

j k
Next or previous question
Enter
Open the question

Question pages

j k
Next or previous post

Shortcuts never apply while you type in a text field. Members can turn them off in their preferences.