Supervised classification in GEE

In this post, we will cover the use of machine learning algorithms to carry out supervised classification.

Source: Google earth engine developers

Supervised classification is enabled through the use of classifiers, which include: Random Forest, Naïve-Bayes, cart, and support vector machines. The procedure for supervised classification is as follows:

  • Selection of the image

The first step is choosing the image. For this blog, a Landsat 8 image is used. The time aspect of the image is set between January and December for the year 2018.

// Make a cloud-free Landsat 8 TOA composite (from raw imagery). 
var l8 = ee.ImageCollection('LANDSAT/LC08/C01/T1'); 
var image = ee.Algorithms.Landsat.simpleComposite({ collection: l8.filterDate('2018-01-01', '2018-12-31'), asFloat: true }); 
// Use these bands for prediction. 
var bands = ['B2', 'B3', 'B4', 'B5', 'B6', 'B7', 'B10', 'B11'];
  • Creation of the training areas

This study will have two training areas: urban which includes the built-up areas and vegetation, which includes grasslands and forests. Training areas are created using the geometry tool, and they are represented as a feature collection, whose property used for prediction is represented as a value, starting from zero. The training areas can be selected as points, lines or polygons. The feature collection used in this case is “landcover”. Once selected, the classes are imported and displayed on the code editor as shown below. The training areas are merged to create one feature class.

Geometry Tool

Merging of the training areas into a feature collection:

var geometries = vegetation.merge(urban);
  • Instantiating the classifier

image.sampleRegions() is used to get the predictors into the table and create a training dataset. From our study, our feature collection is represented as geometries, and the properties are represented as landcover.

// Get the values for all pixels in each polygon in the training. 
var training = image.sampleRegions({ 
// Get the sample from the polygons FeatureCollection. 
collection: geometries, 
// Keep this list of properties from the polygons. 
properties: ['landcover'], 
// Set the scale to get Landsat pixels in the polygons. 
scale: 30 
});

The classifier used in this study is support vector machines, however, there are other classifiers such as random forest, naïve-Bayes, CART among others available in the Google Earth Engine. The parameters of SVM are as indicated below:

// Create an SVM classifier with custom parameters. 
var classifier = ee.Classifier.libsvm({ kernelType: 'RBF', gamma: 0.5, cost: 10 });
  • Training the classifier
// Train the classifier. 
var trained = classifier.train(training, 'landcover', bands);
  • Classification of the image
// Classify the image. 
var classified = image.classify(trained);
  • Display of results

Center the map to the region of interest, and also set the parameters for display of the image, such as the legend colors Green represents vegetation and brown represents urban. Both the input image and the classified image are displayed.

// Display the classification result and the input image. 
Map.setCenter(34.75, 0.29); 
Map.addLayer(image, {bands: ['B4', 'B3', 'B2'], max: 0.5, gamma: 2}); 
Map.addLayer(geometries, {}, 'training polygons'); 
Map.addLayer(classified,{min: 0, max: 1, palette: [ 'green', 'brown']},'classification');
Output of the classification
The output of a classification

Link to the code: https://code.earthengine.google.com/748bdcbe6701fbd2e6c5defeba80b217

Full code:

// Make a cloud-free Landsat 8 TOA composite (from raw imagery).
var l8 = ee.ImageCollection('LANDSAT/LC08/C01/T1');
var image = ee.Algorithms.Landsat.simpleComposite({
  collection: l8.filterDate('2018-01-01', '2018-12-31'),
  asFloat: true
});
// Use these bands for prediction.
var bands = ['B2', 'B3', 'B4', 'B5', 'B6', 'B7', 'B10', 'B11'];
var geometries = vegetation.merge(urban);
// Get the values for all pixels in each polygon in the training.
var training = image.sampleRegions({
  // Get the sample from the polygons FeatureCollection.
  collection: geometries,
  // Keep this list of properties from the polygons.
  properties: ['landcover'],
  // Set the scale to get Landsat pixels in the polygons.
  scale: 30
});
// Create an SVM classifier with custom parameters.
var classifier = ee.Classifier.libsvm({
  kernelType: 'RBF',
  gamma: 0.5,
  cost: 10
});
// Train the classifier.
var trained = classifier.train(training, 'landcover', bands);

// Classify the image.
var classified = image.classify(trained);

// Display the classification result and the input image.
Map.setCenter(34.75, 0.29);
Map.addLayer(image, {bands: ['B4', 'B3', 'B2'], max: 0.5, gamma: 2});
Map.addLayer(geometries, {}, 'training polygons');
Map.addLayer(classified,
             {min: 0, max: 1, palette: ['green',brown’]},
             'classification');

For further discussion, leave a comment.

  • This method is not so accurate.Could you please teach us to do classification with use of DEM, Slope, NDVI, aspect etc.

Leave a Reply

Your email address will not be published. Required fields are marked *

Wanjohi Kibui

Passionate about harnessing the power of geospatial technology to create innovative solutions, I'm a GIS Consultant and Developer dedicated to building cutting-edge geospatial applications. With a keen eye for spatial analysis and a knack for problem-solving, I specialize in crafting solutions that seamlessly integrate technology and geography.

More Reading

Post navigation