land cover classification

in Cambodia using machine learning

step 1: add the Landsat composite to the map and clip for the area of interest

var image = ee.Image("projects/servir-mekong/regionalComposites/2020").divide(10000);
var cambodia = ee.FeatureCollection("users/servirmekong/countries/KHM_adm1");
var KampongThum = cambodia.filter(ee.Filter.eq("NAME_1","Kâmpóng Thum")).geometry();

image = image.clip(KampongThum);

Map.addLayer(image,{min:0,max:0.6,bands:"swir1,nir,red"},"composite");

step 2: Create a new feature collection for water and add markers on water pixels

Step 2: do the same for forest

Step 3: do the same for rubber, urban, wetlands and agriculture

Step 4: combine the data into a single feature collection

var TrainingData = water.merge(forest).merge(rubber).merge(urban).merge(wetlands).merge(agriculture);

Step 5: sample the composite

var trainingSample = image.sampleRegions(TrainingData,["land_class"],30);

print(trainingSample)

Step 6: train the random forest model

var bandNames = image.bandNames();
print(bandNames)
var classifier = ee.Classifier.smileRandomForest(30).train(trainingSample,"land_class",bandNames);

Step 7: classify the image

var classification = image.classify(classifier,'Mode');

Map.addLayer(classification.clip(KampongThum),{min:0,max:5,palette:"aec3d4,152106,387242,cc0013,3bc3b2,8dc33b"},"landcover")

Step 8: print the variable importance using the code below

var dict = classifier.explain();
print('Explain:',dict);
 
var variable_importance = ee.Feature(null, ee.Dictionary(dict).get('importance'));
 
var chart =
ui.Chart.feature.byProperty(variable_importance)
.setChartType('ColumnChart')
.setOptions({
title: 'Random Forest Variable Importance',
legend: {position: 'none'},
hAxis: {title: 'Bands'},
vAxis: {title: 'Importance'}
});
 
print(chart);

Step 9: add the code below after the image import and study the variable importance again.

var covariates = require("users/servirmekong/mrc:covariate_module");

image = covariates.addJRCAndTopo(image);
image = covariates.addCovariates(image);

Leave a Reply