Google Earth Engine Tutorial: Supervised Classification with Landsat 8 – Part 2
Credit: Youtube Channel “Terra Spatial, Second part completing the supervised classification process with accuracy assessment and refinement.”
You can see all the tutorials from here: Techgeo Academy.
Supervised classification in Google Earth Engine (GEE) involves training a model with labeled samples to categorize pixels in a satellite image. In Part 2 of this tutorial, we will focus on using Landsat 8 data to train a classifier and create classified maps. Below are the step-by-step instructions.
1. Setting Up the Training Data
To begin, you must create a training dataset by manually selecting regions of interest (ROIs) that represent the categories you want to map. For example, if classifying land cover types (forest, agriculture, urban, etc.), draw polygons or use existing shapefiles for each class. Use the geometry tool in GEE to define these areas. Once defined, assign labels to each ROI as a feature collection. This can be done via scripting or interactive tools like the GEE Code Editor’s Geometry Tools.
// Define training regions
var trainingData = ee.FeatureCollection('path/to/your/training/featurecollection');
// Split training data into training and validation sets
var trainingSet = trainingData.randomColumn();
var split = trainingSet.randomSplit([0.8, 0.2]); // 80% training, 20% validation
var training = split[0];
var validation = split[1];
2. Selecting a Classifier
GEE provides multiple classifiers, including Random Forest, SVM, and Logistic Regression. For this example, we’ll use the Random Forest classifier, which is effective for land cover mapping. The classifier is initialized with the desired parameters, such as the number of trees.
// Initialize the classifier
var classifier = ee.Classifier.smileRandomForest(100); // 100 trees
3. Training the Classifier
Train the model using the training set. Extract spectral values from the Landsat 8 image for each ROI and pass them to the classifier for training. This step generates a trained model that will later be applied to the entire image.
// Train the classifier
var trained = classifier.train({
features: training,
classProperty: 'landcover'
});
4. Applying the Classifier
Once the model is trained, use the classify method on the Landsat 8 image to generate the classified map. This applies the training results to all pixels in the image.
// Apply the classifier to the Landsat 8 image
var classified = image.select(['B1', 'B2', 'B3', 'B4', 'B5', 'B6', 'B7', 'B8', 'B9', 'B10', 'B11', 'B12']).classify(trained);
5. Visualizing and Evaluating the Results
Add the classified image to the map for visualization. Then assess accuracy using the validation set and a confusion matrix. This helps identify misclassified areas and refine the model.
// Visualize the classified image
Map.addLayer(classified, {palette: ['red', 'green', 'blue', 'yellow'], min: 0, max: 3}, 'Classified Map');
// Evaluate accuracy
var validationResults = validation.classify(trained).errorMatrix('landcover', 'classification');
print('Validation Accuracy Matrix:', validationResults);
6. Exporting the Classification Result
Finally, export the classified image to your Google Drive or other storage. This allows further analysis in GIS software or external tools.
// Export the classified image
Export.image.toDrive({
image: classified,
description: 'Landsat_Classification',
folder: 'GEE_Exports',
fileNamePrefix: 'landcover_classified',
region: areaOfInterest.geometry(),
scale: 30,
maxPixels: 1e10
});
By following these steps, you can use GEE for supervised classification tasks with Landsat 8 data. This workflow is scalable and applicable to various remote sensing projects.
FAQ
What is a confusion matrix in classification?
A confusion matrix is a table that evaluates a classifier’s accuracy by comparing predicted classes against actual classes. It shows true positives, false positives, and other metrics.
How do I collect training samples in GEE?
Use the GEE Code Editor’s visual tools (e.g., drawing polygons) or upload a shapefile. Convert the geometry to a FeatureCollection and assign unique labels to each class.
Why is accuracy assessment important?
Accuracy assessment ensures the model’s reliability. It identifies errors, such as misclassification of urban areas to water, and guides adjustments to improve results.
What if my classifier produces errors?
Refine the training data by adjusting or adding samples. Use additional bands or features (e.g., indices like NDVI) to enhance model performance.
Can I classify multiple images in GEE?
Yes. Chain the classification workflow for a collection of images or use a time series approach to analyze changes over time.






