0

I am attempting to calculate of area of 4 different classes in my classified map. But I am getting error "Dictionary (Error) Classification request exceeds size limit."

Here is my Code:

//Load ROI 
var roi = AOI;

// Load the Sentinel-1 ImageCollection
var S1image = ee.ImageCollection('COPERNICUS/S1_GRD');

// Filter VH, IW
var vh = S1image
 // Filter to get Images with VV and VH dual polarization
 .filter(ee.Filter.listContains('transmitterReceiverPolarisation', 'VV'))
 .filter(ee.Filter.listContains('transmitterReceiverPolarisation', 'VH'))
 // Filter to get images collected in interferometric wide swath mode.
  .filter(ee.Filter.eq('instrumentMode', 'IW'))
   // reduce to VH polarization
  .select('VH','VV')
  .map(function(image) {
          var edge = image.lt(-30.0);
          var maskedImage = image.mask().and(edge.not());
          return image.updateMask(maskedImage);
        });


// Filter to orbitdirection Descending
// var asc = vh.filter(ee.Filter.eq('orbitProperties_pass', 'ASCENDING'));
var desc = vh.filter(ee.Filter.eq('orbitProperties_pass', 'DESCENDING'));

var image1 = ee.Filter.date('2017-10-31', '2017-12-01');
var image2 = ee.Filter.date('2017-11-30', '2018-01-01');
var image3 = ee.Filter.date('2017-12-31', '2018-02-01');
var image4 = ee.Filter.date('2018-01-31', '2018-03-01');
var image5 = ee.Filter.date('2018-02-28', '2018-04-01');
var image6 = ee.Filter.date('2018-03-31', '2018-05-01');

var descChange = ee.Image.cat(
        desc.filter(image1).mean(),
        desc.filter(image2).mean(),
        desc.filter(image3).mean(),
        desc.filter(image4).mean(),
        desc.filter(image5).mean(),
        desc.filter(image6).mean());

print(descChange)

Map.centerObject(roi, 6);        


//Call Sentinel-2 Image Collection

var collection = ee.ImageCollection('COPERNICUS/S2')
    .filterMetadata('CLOUDY_PIXEL_PERCENTAGE','less_than',20);
    Map.centerObject(AOI, 6);

var bands = ['B2', 'B3', 'B4', 'B8', 'B8A'];    

//Filter Image Monthwise 2017-18     
var imageA = collection.filterDate('2017-10-31', '2017-12-01').median().clip(AOI);
var imageB = collection.filterDate('2017-11-30', '2018-01-01').median().clip(AOI);
var imageC = collection.filterDate('2017-12-31', '2018-02-01').median().clip(AOI);
var imageD = collection.filterDate('2018-01-31', '2018-03-01').median().clip(AOI);
var imageE = collection.filterDate('2018-02-28', '2018-04-01').median().clip(AOI);
var imageF = collection.filterDate('2018-03-31', '2018-05-01').median().clip(AOI);

// //Display Image Monthwise
// Map.addLayer(imageA, {min:0, max:3000, bands: ["B4", "B3", "B2"]}, 'November');
// Map.addLayer(imageB, {min:0, max:3000, bands: ["B4", "B3", "B2"]}, 'December');
// Map.addLayer(imageC, {min:0, max:3000, bands: ["B4", "B3", "B2"]}, 'Jan');
// Map.addLayer(imageD, {min:0, max:3000, bands: ["B4", "B3", "B2"]}, 'Feb');
// Map.addLayer(imageE, {min:0, max:3000, bands: ["B4", "B3", "B2"]}, 'Mar');
// Map.addLayer(imageF, {min:0, max:3000, bands: ["B4", "B3", "B2"]}, 'April');

//Extract NDVI from each image monthwise
var ndviA = imageA.normalizedDifference(['B8','B4']).rename('NDVI').set('name','Nov2017');
var ndviB = imageB.normalizedDifference(['B8','B4']).rename('NDVI').set('name','Dec2017');
var ndviC = imageC.normalizedDifference(['B8','B4']).rename('NDVI').set('name','Jan2018');
var ndviD = imageD.normalizedDifference(['B8','B4']).rename('NDVI').set('name','Feb2018');
var ndviE = imageE.normalizedDifference(['B8','B4']).rename('NDVI').set('name','March2018');
var ndviF = imageF.normalizedDifference(['B8','B4']).rename('NDVI').set('name', 'April2018');

// //Display NDVI 
// Map.addLayer(ndviA, { min: -1, max: 1, palette: ['E2FFE2', 'AFFFAF', '004500']}, 'NDVI Nov');
// Map.addLayer(ndviB, { min: -1, max: 1, palette: ['E2FFE2', 'AFFFAF', '004500']}, 'NDVI Dec');
// Map.addLayer(ndviC, { min: -1, max: 1, palette: ['E2FFE2', 'AFFFAF', '004500']}, 'NDVI Jan');
// Map.addLayer(ndviD, { min: -1, max: 1, palette: ['E2FFE2', 'AFFFAF', '004500']}, 'NDVI Feb');
// Map.addLayer(ndviE, { min: -1, max: 1, palette: ['E2FFE2', 'AFFFAF', '004500']}, 'NDVI Mar');
// Map.addLayer(ndviF, {min: -1, max: 1, palette: ['E2FFE2', 'AFFFAF', '004500']}, 'NDVI April');


//Store NDVI of each month in one collection
var ndvicollection = ee.ImageCollection([ndviA,ndviB,ndviC,ndviD,ndviE,ndviF])
print('RABI Season 2017-18 NDVI Collection', ndvicollection)


//DISPLAY S1 IMAGE
Map.addLayer(descChange.clip(roi), {min: -25, max: 5}, 'finalimage', true);

//For Sentinel 2 Band Stacking
var StackedNDVI = ee.Image(ndvicollection.iterate(function(imgIn, imgOut){
  var name = ee.String(imgIn.get('name'));
  return ee.Image(imgOut).addBands(imgIn.select(['NDVI']).rename(name));
},ee.Image())).slice(1);
print('NDVI_stack', StackedNDVI);
Map.addLayer(StackedNDVI,  {}, 'NDVI_Stack');

var s1s2image = descChange.addBands(StackedNDVI);
Map.addLayer(s1s2image.clip(AOI), {}, 'Combine Image');
print(s1s2image);

var samples = Vegetation.merge(Fallowarea).merge(Waterbodies).merge(Others)
var bands = ['VV', 'VH','VH_1','VV_1','VH_2','VV_2','VH_3','VV_3','VH_4','VV_4','VH_5','VV_5', 'Nov2017','Dec2017','Jan2018','Feb2018','March2018','April2018']
var points = s1s2image.select(bands).sampleRegions({
  collection: samples,
  properties: ['landcover'],
  scale: 50
}).randomColumn();

var training = points.filter(ee.Filter.lt('random', 0.7));
var validation = points.filter(ee.Filter.gte('random', 0.3));
// // Get a CART classifier and train it.
var classifier = ee.Classifier.randomForest().train({
  features: training, 
  classProperty: 'landcover', 
  inputProperties: bands
});

// // Classify the image.
var classified = s1s2image.select(bands).classify(classifier);
Map.addLayer(classified.clip(roi), {min:0, max:3,palette: ['29e938', 'ff4325', 'a9ffbf','0b4a8b']}, 'Classification-2017');

var trainAccuracy = classifier.confusionMatrix();
print('Training overall accuracy: ', trainAccuracy.accuracy());

// // Classify the validation data.
var validated = validation.classify(classifier);
// print(validated);

// Get a confusion matrix representing expected accuracy.
var testAccuracy = validated.errorMatrix('landcover', 'classification');
print('Validation error matrix: ', testAccuracy);
print('Validation overall accuracy: ', testAccuracy.accuracy());


print(classified.eq(0).multiply(ee.Image.pixelArea()).multiply(0.000247105e-6).reduceRegion({reducer:ee.Reducer.sum(),geometry:roi,scale:100,maxPixels:1e18}));
print(classified.eq(1).multiply(ee.Image.pixelArea()).multiply(0.000247105e-6).reduceRegion({reducer:ee.Reducer.sum(),geometry:roi,scale:100,maxPixels:1e18}));
print(classified.eq(2).multiply(ee.Image.pixelArea()).multiply(0.000247105e-6).reduceRegion({reducer:ee.Reducer.sum(),geometry:roi,scale:100,maxPixels:1e18}));
print(classified.eq(3).multiply(ee.Image.pixelArea()).multiply(0.000247105e-6).reduceRegion({reducer:ee.Reducer.sum(),geometry:roi,scale:100,maxPixels:1e18}));

Your Answer

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

Browse other questions tagged or ask your own question.