2

I want to calculate the area for each classified color in earth engine. Here are my codes. here is my imports entries

var image = ee.Image(ee.ImageCollection('LANDSAT/LC8_L1T_TOA')  
.filterBounds(roi)
.filterDate('2015-01-01', '2015-12-31')
.sort('CLOUD_COVER')
.first());
print(image);  


Map.addLayer(image, {bands: ['B4', 'B3', 'B2'], max: 0.3}, 'image_L8');


var clip = image.clip(table);  //cliping image to shapefile


var newclassified = 
vegetation.merge(water).merge(agri_bare).merge(builtarea);
print(newclassified);
var bands = ['B2', 'B3', 'B4', 'B5', 'B6', 'B7', 'B10', 'B11'];


var training = clip.select(bands).sampleRegions({
collection: newclassified,
properties: ['landcover'],
scale: 30
}); 


var classifier = ee.Classifier.cart().train({
features: training,
classProperty: 'landcover',
inputProperties: bands
});

var classified = clip.select(bands).classify(classifier);  

Map.centerObject(newclassified, 11);
Map.addLayer(clip,
{bands: ['B4', 'B3', 'B2'], max: 0.4},
'Landsat image');

Map.addLayer(classified,
{min: 1, max: 4, palette: ['green', 'blue', 'yellow', 'red']},
'classification');

Now I want to calculate the area covered by green, blue, yellow and red palette. What code I required to do that??

2

To determine the area of each class in a region of interest, first define the region as a geometry:

var roi = ee.Geometry.Point([-113.5911, 37.0855]).buffer(100);

and a classified image (with categorical data values):

var class_image = ee.Image('USGS/NLCD/NLCD2011').select('landcover');

Then construct a composite image where the first band is the pixel's area and the second band has the class information. Then apply a grouped reducer to the composite image, in order to sum all the pixel areas within each class:

var class_areas = ee.Image.pixelArea().addBands(class_image)
  .reduceRegion({
    reducer: ee.Reducer.sum().group({
      groupField: 1,
      groupName: 'code',
    }),
    geometry: roi,
    scale: 1,  // sample the geometry at 1m intervals
    maxPixels: 1e10
  }).get('groups');

Finally print out the list of dictionaries containing the calculated areas for each class:

print(class_areas);

Here is a link to the complet script: https://code.earthengine.google.com/d1f498ae76ed51f305862b2f227c2442

1

To calculate area per class from the image that you classified, you may find the following codes useful. You have to add these codes at the end of your given code.

//Select the class from the classified image
var veg = classified.select('classification').eq(1);//vegetation has 1 value in your case

//Calculate the pixel area in square kilometer
var area_veg = veg.multiply(ee.Image.pixelArea()).divide(1000*1000);

//Reducing the statistics for your study area
var stat = area_veg.reduceRegion ({
  reducer: ee.Reducer.sum(),
  geometry: table,
  scale: 30,
  maxPixels: 1e9
});

//Get the sq km area for vegetation
print ('Vegetation Area (in sq.km)', stat);

You can repeat the same code for other classes. I am not sure how to create a list sequence to get areas for all the classes at the same time. I hope someone will answer that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.