2

I have a dictionary of % landcover from the 2016 NLCD. I am having trouble trying to make a bar chart to show the percentage of each land cover class in a given area (DEM vector). The dictionary is an object w/ 15 properties - I want the data in this object to be used for a bar chart.

///////////Load Landcover dataset////////////
var dataset = ee.Image('USGS/NLCD/NLCD2016')
  .clip(aoi) ;
print(dataset);
var landcover = dataset.select('landcover');
var landcoverVis = {
  min: 0.0,
  max: 95.0,
  palette: [
    '000000',
    '000000',
    '000000',
    '000000',
    '000000',
    '000000',
    '000000',
    '000000',
    '000000',
    '000000',
    '000000',
    '466b9f',
    'd1def8',
    '000000',
    '000000',
    '000000',
    '000000',
    '000000',
    '000000',
    '000000',
    '000000',
    'dec5c5',
    'd99282',
    'eb0000',
    'ab0000',
    '000000',
    '000000',
    '000000',
    '000000',
    '000000',
    '000000',
    'b3ac9f',
    '000000',
    '000000',
    '000000',
    '000000',
    '000000',
    '000000',
    '000000',
    '000000',
    '000000',
    '68ab5f',
    '1c5f2c',
    'b5c58f',
    '000000',
    '000000',
    '000000',
    '000000',
    '000000',
    '000000',
    '000000',
    'af963c',
    'ccb879',
    '000000',
    '000000',
    '000000',
    '000000',
    '000000',
    '000000',
    '000000',
    '000000',
    '000000',
    '000000',
    '000000',
    '000000',
    '000000',
    '000000',
    '000000',
    '000000',
    '000000',
    '000000',
    'dfdfc2',
    'd1d182',
    'a3cc51',
    '82ba9e',
    '000000',
    '000000',
    '000000',
    '000000',
    '000000',
    '000000',
    'dcd939',
    'ab6c28',
    '000000',
    '000000',
    '000000',
    '000000',
    '000000',
    '000000',
    '000000',
    'b8d9eb',
    '000000',
    '000000',
    '000000',
    '000000',
    '6c9fb8'
  ],
};

Map.addLayer(landcover, landcoverVis, 'Landcover');

//crop landcover by DEM_vector
var landcover_clip = landcover.clip(DEM_vector);
print(landcover_clip);

Map.addLayer(landcover_clip, {}, 'landcover_clip');

//Calculate percentage of landcover <5m
var frequency = landcover.reduceRegion({
  reducer:ee.Reducer.frequencyHistogram(),
  maxPixels:2e9,
  geometry:DEM_vector
});
print('Pixel frequency', frequency);

//turn frequency into percentage
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);

1 Answer 1

1

Maybe something like this?

var chart = ui.Chart.feature.byProperty(
  ee.Feature(null, new_dict)
)
print(chart)

https://code.earthengine.google.com/b7aeb5096103ba3fbb829af87ea0bfc9

2
  • wow im embarassed by how easy that was..lol thank you! Any idea on how to get landcover names instead of values on the xaxis?
    – Anna
    Commented Apr 21, 2020 at 15:14
  • Nothing to be embarrassed about - docs about EE charts is not that clear. It's mostly trial-and-error for me. Getting labels requires some massaging of your dictionary: code.earthengine.google.com/2e8646c6f7d591171e7f9fd578af14a6 Commented Apr 21, 2020 at 15:39

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.