Commit 93e23f12 authored by Arachchige P.D.A.M.'s avatar Arachchige P.D.A.M.

land cover classification for gis mapping --> Google Earth Engine Code Editor JS API

parent 735c67a3
var image = L8.filterBounds(roi).filterMetadata('CLOUD_COVER','less_than',3).filterDate('2017-01-01','2022-12-31').map(addNDVI).median().clip(roi);
//var image2 = L7.filterBounds(roi_2).filterMetadata('CLOUD_COVER','less_than',1).filterDate('2019-01-01','2022-12-31')
//var image_3 = S2.filterBounds(roi_2).filterMetadata('CLOUD_COVER','less_than',1).filterDate('2020-01-01','2022-12-31');
//print(image);
Map.addLayer(image,imageVisParam,'True Color Composite');
Map.centerObject(roi,11.5);
// Collect training samples
// Merge training Samples
var training = Urban.merge(Forest).merge(BareLand).merge(Water).merge(Agriculture);
print(training);
var label = 'Class';
//when using external bands
function addNDVI(image){
var ndvi =image.normalizedDifference(['B5','B4']).rename('NDVI');
return image.addBands(ndvi);
}
var bands = ['B1','B2','B3','B4','B5','B6','B7','NDVI'];
var input = image.select(bands);
// overlay the points on images to get training
var train_image = input.sampleRegions({collection :training, properties:[label], scale:30});
var train_data = train_image.randomColumn();
var train_set = train_data.filter(ee.Filter.lessThan('random',0.8));
var test_set = train_data.filter(ee.Filter.greaterThanOrEquals('random',0.2));
// Classification model
var classifier = ee.Classifier.smileCart().train(train_set,label,bands);
// Classify the image
var classified = input.classify(classifier);
// Define a color palette
var colorPalette = ['FF3C33', '006837', '969696', '253494', 'FF8000'];
Map.addLayer(
classified.clip(roi),
{ palette: colorPalette, min: 0, max: 5 },
'CART Classification'
);
var train_accuracy = classifier.confusionMatrix();
print(train_accuracy);
// Get information about the trained classifier.
print('Results of trained classifier', classifier.explain());
// Get a confusion matrix and overall accuracy for the training sample.
var train_accuracy = classifier.confusionMatrix();
print('Training error matrix', train_accuracy);
print('Training overall accuracy', train_accuracy.accuracy());
var uniqueValues = classified.reduceRegion({
reducer: ee.Reducer.frequencyHistogram(),
geometry: roi,
scale: 30
});
print('Unique values:', uniqueValues.get('classification'));
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment