Google Earth Engine – Showing Land Cover Class Area in Charts

google-earth-enginegoogle-earth-engine-javascript-apiland-classification

I've produced a classified map in GEE and trying to create a chart showing the area for each landcover class. The code I'm using is as follows,

var classNames = ['vegetation Covers', 'Water Bodies', 'Urban Areas', 'Barren Lands'];
var create_chart = function(classifiedImage, mumbai, classNames){ // for classNames, create a list of your classes as strings
  var options = {
    hAxis: {title: 'Land Cover Class'},
    vAxis: {title: 'Area in Kilometers'},
    title: 'Area in KM by Land Cover Class',
    series: { // set color for each class
      0: {color: 'green'}, // Vegetation covers
      1: {color: 'blue'}, // Water bodies
      2: {color: 'black'}, // Urban areas
      3: {color: 'yellow'}} // Barren lands
  };
  var areaChart = ui.Chart.image.byClass({
    image: ee.Image.pixelArea().addBands(classifiedImage),
    classBand: 'classification', 
    scale: 30,
    region: mumbai,
    reducer: ee.Reducer.sum()

  }).setSeriesNames(classNames)
  .setOptions(options)
  ;
  print(areaChart);
};

create_chart(classifiedImage, mumbai, classNames);

classifiedImage is the output image of the classification. mumbai is the area of interest.

The chart should show the landcover classes on the x-axis and the corresponding area in kilometers on the y-axis. But the chart produce by this code is showing the number of pixels on the y-axis.

enter image description here
I understand, I have not specified any rule for area calculation in the code. I have tried changing the code but always get errors. Can anyone suggest, how to get the desired output here (area in kilometers on the y-axis)?

I'm using the Landsat images here.

Best Answer

It's not showing the number of pixels, its showing the area in meters, since that's the units on pixelArea. Divide that image by 1,000,000 to get sqkm.

Related Question