Supervised classification in the Google Earth Engine

Output of the classification

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

 

Imported Training Areas

 

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 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.

 

Recommended For You

About the Author: Wanjohi Kibui

A GIS user with interests in web and desktop systems development, FOSS advocate, trainer and mentor. Lives in Nairobi but finds adventure in travelling. The "Life in GIS" phrase is as a result of the many told and untold stories in the Geospatial realm. Drop a message today for more details and projects!!

2 Comments

  1. Hello, this is an introductory article on Google Earth Engine. For more information on Google Earth Engine and supervised classification, kindly use the link shown in the blog. Thanks.

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: