mapping surface water

with Sentinel-2 using a random forest classifier

Step 1: import the area of interest

//Import Administrative data
var provinces = ee.FeatureCollection("projects/servir-mekong/admin/KHM_adm1")

// select province of interest
var siemreap  = provinces.filter(ee.Filter.eq("NAME_1", "Siemréab"));
// add layer to map

Step 2: import a training dataset

//Import Reference water training data from the asset
var referenceData = ee.FeatureCollection("projects/servir-mekong/undp/Training/water");

//Check water and non_water data information
var water= referenceData.filter(ee.Filter.eq("land_class",1));
var other = referenceData.filter(ee.Filter.eq("land_class",0));

//Print the information of the reference data
print("No. of water points:", water.size());
print("No. of other points:", other.size());

//Add reference data points into the map to visualize

Step 3: import the sentinel 2 image collection and filter for period and area of interest

//Import Sentinel-2 Image Collection
var s2Col = ee.ImageCollection('COPERNICUS/S2');
//Define start and end date
var startdate = ee.Date.fromYMD(2020,1,1);
var enddate = ee.Date.fromYMD(2020,12,31);

// filter image for date, study area and cloud coverage
var image = s2Col.filterBounds(siemreap)
// add sentinel-2 composite image to map
Map.addLayer(image,{min:0,max:3000,bands:"B11,B8,B4"},"Sen2 Image");

step 4: create sample and train the random forest classifier

//Create water sample over sentinel-2 composite
var trainingSample = image.sampleRegions({
properties: ["land_class"],
scale: 30,

//Select the bands to use in the classifier 
var bandNames = image.bandNames();

//Train random forest classifier using the training samples of water and set the output mode to probability 
var classifier = ee.Classifier.smileRandomForest(30).setOutputMode('PROBABILITY').train(trainingSample,"land_class",bandNames);

Step 5: run the classifier in classification mode

//Classify the composite with the trained Random Forest classifier
var classification = image.classify(classifier).multiply(100);
//Add the ouput surface water classification to ui map 
Map.addLayer(classification,{min:40,max:100,palette:"white,blue,darkblue"},"Surface Water");

Leave a Reply