// Loading Area of Interest ---- AOI-----Area of study var AOI = geometry; // Load the landsat 5 reflectance Image Collection. var image = ee.Algorithms.Landsat.simpleComposite({ collection: L5.filterDate('1988-01-01', '1988-12-31'), asFloat: true }); //var LandsatCollection = imageCollection2.filterDate('2018-01-01', '2018-12-31'); //**************************************************************// // Get an image and display in map window. //print('composite', composite) // Display the composite Map.addLayer(image.clip(AOI), {bands: ['B4', 'B3', 'B2'], min: 0, max: 0.3}, 'composite'); Map.addLayer (table); var newfc = table; //print(newfc, 'newfc'); // Select the bands for training var bands = ['B2', 'B3', 'B4']; // The name of the property on the points storing the class label. var classProperty = 'Landcover'; // Sample the input imagery to get a FeatureCollection of training data. // Split into trainin and testing features // Optionally, do some accuracy assessment. Fist, add a column of // random uniforms to the training dataset. var withRandom = newfc.randomColumn('random'); // We want to reserve some of the data for testing, to avoid overfitting the model. //var split = 0.7; var splittraining = 0.7; // Roughly 70% training, 30% testing. var splittesting = 0.3; // this is for random selection by the algorithim. //var trainingPartition = withRandom.filter(ee.Filter.lt('random', split)); //var testingPartition = withRandom.filter(ee.Filter.gte('random', split)); var trainingPartition = withRandom.filter(ee.Filter.lt('random',splittraining)); var testingPartition = withRandom.filter(ee.Filter.gte('random',splittesting)); var training = image.select(bands).sampleRegions({ collection: newfc, properties: ['Landcover'], scale: 30 }); // Train a CART classifier.//chnage ro Random Forest var classifier = ee.Classifier.smileRandomForest({ numberOfTrees: 1,//was less seed: 1 }) .train({ features: training, classProperty: classProperty, }); var classified = image.classify(classifier); var palette = [ 'ff78cb', // Croplands (0) Pink '6b8624', // Forestland (1) olive 'ff0000', // Grasslands (2) red 'FFFF00', // Settlement (3) yellow '888888', // waterbody (4) coral ]; Map.addLayer(classified.clip(AOI), {min: 0, max: 4, palette: palette}, 'Land Use Classification'); Map.centerObject(newfc); var testing = image.select(bands).sampleRegions({ collection: testingPartition, properties: ['Landcover'], scale: 30 }); //print('testing', testing) // Classify the test FeatureCollection. var test = testing.classify(classifier); /* // Compute pixel area in square meters per landcover type. var stats = ee.Image.pixelArea().addBands(classified).reduceRegion({ reducer: ee.Reducer.sum().group(1), geometry:AOI, maxPixels: 1e13, tileScale:2, scale: 1, crs: "EPSG:4326" }); */ //Select the class from the classified image var veg = classified.select('classification').eq(2);//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: AOI, scale: 30, maxPixels: 1e9 }); //Get the sq km area for vegetation //print ('Vegetation Area (in sq.km)', stat); //******************************************************************************************************************************************************* //VALIDATION & ACCURACY var confMatrix = classifier.confusionMatrix(); //print(confMatrix); //var testAccuracy = test.errorMatrix('Landcover', 'classification'); //print('Validation error matrix: ', testAccuracy); //print('Validation overall accuracy: ', testAccuracy.accuracy()); var testAccuracy = test.errorMatrix('Landcover', 'classification'); var OA = confMatrix.accuracy(); var CA = confMatrix.consumersAccuracy(); var Kappa = confMatrix.kappa(); var Order = confMatrix.order(); var PA = confMatrix.producersAccuracy(); print(confMatrix,'Confusion Matrix'); print(OA,'Overall Accuracy'); print(CA,'Consumers Accuracy'); print(Kappa,'Kappa'); print(Order,'Order'); print(PA,'Producers Accuracy'); print('Test error matrix: ', testAccuracy); print('Test overall accuracy: ', testAccuracy.accuracy()); print('Test Kappa: ', testAccuracy.kappa()); print('Test consumersAccuracy: ', testAccuracy.consumersAccuracy()); print('Test producersAccuracy: ', testAccuracy.producersAccuracy()); //rint('Area per class', stats); //******************************************************************************************************************************************************* //Exporting classifieed images - When satisfied with the accuracy. //Export.image(). Though this function take several optional parameters: //maxPixels – This restricts the numbers of pixels in the exported image. By default, this value is set to 100,000,000 pixels. You can set this argument to raise or lower the limit. “1e13” is 10 to the 13th power (1013) and the most GEE will let you export. //region – By default, the viewport of the Code Editor is exported but you can also specify a geometry to change the export extent. //crs – The coordinate reference system for the output image. This is specified using the EPSG code. You can look up the EPSG code for your desired spatial projections at http://spatialreference.org. //scale – The resolution in meters per pixel. The native resolution for the canopy height data set is 30 arc-seconds or approximately one kilometer. //Export the image //the scale change according to the image resolution //REFLECTANCE //exporting classifieed images Export.image.toDrive({ image: classified, description: 'Gedaref19882', scale: 30, region: geometry, maxPixels: 3E10 });