Here are two ways of doing this. The first is your method of masking with some simplified code. The second is using a grouped reducer:
var s1 = ee.ImageCollection("COPERNICUS/S1_GRD"),
S2 = ee.ImageCollection("COPERNICUS/S2"),
fc = ee.FeatureCollection("ft:1w_ciwxR9FKm0rzMH8oJ3MdyE-1U15PfiOFlGW5Ib"),
fc2 = ee.FeatureCollection("ft:18Xvf7jG2EI4mJMwj0oLV2u-05bAPiwJDaxSRy2ep");
var aoi = fc;
var bands = ['B2', 'B3', 'B4', 'B8', 'B11', 'B12'];
var S2 = ee.ImageCollection('COPERNICUS/S2')
.filterDate('2016-07-01', '2016-08-01')
.filterBounds(aoi);
// You may want to use median here if there's overlapping imagery.
var image = S2.mosaic();
var geometry = ee.Geometry.Rectangle([36.977296602,-15.364411987,37.057018981,-15.447436229]);
Map.centerObject(geometry);
var landcover_roi = image.clip(geometry);
Map.addLayer(landcover_roi, {'bands': 'B12,B8,B4','min': 0,'max': 4000}, 'Sentinel 2A SWIR,NIT,R', 0);
var training = landcover_roi.sampleRegions(fc2, ["info"], 30);
var classifier = ee.Classifier.cart()
.train(training, "info", image.bandNames());
var out = landcover_roi.classify(classifier);
Map.addLayer(out, {palette: [
'ff0000',// cleared red
'ffcc66',// cultivated
'387242',// forest
'99ff33',// grass
'e0e0d1',// rock
'e0e0d1'// rock grass
], min:1, max:6});
Map.addLayer(ee.Image().paint(fc, 1, 3));
var fc = fc2;
Map.addLayer(fc2);
var names = ['cleared', 'cultivated1', 'cultivated2', 'grass', 'rock', 'rockgrass'];
var total = out.eq([1, 2, 3, 4, 5, 6]).rename(names);
var count = total.multiply(ee.Image.pixelArea());
// The grouped reducer ignores the weights. Unweight here for comparison.
var area = count.reduceRegion(ee.Reducer.sum().unweighted(), geometry, 30);
print(area);
var stats = ee.Image.pixelArea().addBands(out).reduceRegion({
reducer: ee.Reducer.sum().group(1),
geometry: geometry,
scale: 30,
});
print(stats);
Best Answer
By applying
.eq(95)
you, as suggested, create a binary image. To avoid you have to apply amask()
. See the difference:https://code.earthengine.google.com/9b6a8c5086d0ac87ac5ef18092bd8971