GEE Tutorials

Google Earth Engine Tutorial: Supervised Classification with Landsat 8 – Part 1

Credit: Youtube Channel “Terra Spatial, First part of tutorial on performing supervised classification of Landsat 8 imagery for land cover mapping.”

You can see all the tutorials from here: Techgeo Academy.

Supervised classification in Google Earth Engine (GEE) is a powerful method for categorizing land cover types based on predefined training samples. This tutorial will guide you through the process of classifying Landsat 8 imagery using a supervised approach. The steps include preparing the data, selecting training samples, training a classifier, and applying it to the entire image.

Data Preparation and Loading

To begin, load the Landsat 8 dataset and filter it to a specific area and time frame. Landsat 8 provides surface reflectance data with 11 spectral bands, making it suitable for land cover analysis. Use the following code to load and filter the image collection:

var dataset = ee.ImageCollection('LANDSAT/LC08/C02/T1_L2')  
  .filterDate('2020-01-01', '2020-12-31')  
  .filter(ee.Filter.eq('WRS_PATH', 21))  
  .filter(ee.Filter.eq('WRS_ROW', 45));  

Next, select a single image from the collection and visualize it. For example:

var image = dataset.select(['SR_B.1', 'SR_B.2', 'SR_B.3', 'SR_B.4', 'SR_B.5', 'SR_B.6', 'SR_B.7'])  
  .first();  
Map.addLayer(image, {min: 0, max: 0.3, bands: ['SR_B.4', 'SR_B.3', 'SR_B.2']}, 'Landsat 8 Image');  

Image Preprocessing

Preprocessing ensures the data is suitable for classification. Start by masking clouds using the QA band. Then, compute the Normalized Difference Vegetation Index (NDVI) for better vegetation differentiation:

var maskedImage = image.select('SR_B.1', 'SR_B.2', 'SR_B.3', 'SR_B.4', 'SR_B.5', 'SR_B.6', 'SR_B.7')  
  .updateMask(image.select('QA_PIXEL').bitwiseAnd(1).eq(0));  
var ndvi = maskedImage.normalizedDifference(['SR_B.5', 'SR_B.4']);  
Map.addLayer(ndvi, {min: -1, max: 1, palette: ['blue', 'green']}, 'NDVI');  

Selecting Training Data

Training data is critical for supervised classification. Use the GEE Code Editor to draw polygons around different land cover types (e.g., water, urban, vegetation). Each polygon represents a class label. Combine them into a feature collection:

var trainingData = ee.FeatureCollection([  
  ee.Feature(ee.Geometry.Rectangle([100.2, 0.5, 100.4, 0.7]), {class: 1}),  
  ee.Feature(ee.Geometry.Rectangle([100.1, 0.2, 100.3, 0.4]), {class: 2}),  
  ee.Feature(ee.Geometry.Rectangle([100.5, 0.6, 100.7, 0.8]), {class: 3})  
]);  

Randomly split the training data into a training set and a validation set to assess results later:

var trainingSet = trainingData.randomColumn('random').filter(ee.Filter.lt('random', 0.8));  
var validationSet = trainingData.randomColumn('random').filter(ee.Filter.gte('random', 0.8));  

Training the Classifier

Choose a classifier algorithm, such as the Random Forest classifier. Extract the training data from the image, train the model, and evaluate its performance:

var classifier = ee.Classifier.randomForest(10);  
var trainedClassifier = classifier.train({  
  features: trainingSet,  
  classProperty: 'class',  
  inputProperties: ['SR_B.1', 'SR_B.2', 'SR_B.3', 'SR_B.4', 'SR_B.5', 'SR_B.6', 'SR_B.7']  
});  
var accuracy = trainedClassifier.confusionMatrix();  
print('Training Accuracy:', accuracy);  

The trained classifier can now be applied to the entire image to generate a classified map:

var classified = maskedImage.classify(trainedClassifier);  
Map.addLayer(classified, {palette: ['blue', 'green', 'red'], min: 1, max: 3}, ' Classified Image');  

Classification and Visualization

After classification, you can export the result as a GeoTIFF or visualize it directly in the GEE map. For example:

Map.addLayer(classified.select('classification'), {palette: ['blue', 'green', 'red'], min: 1, max: 3}, 'Land Cover Types');  

Finally, validate the classification using the validation set:

var validation = validationSet.classify(trainedClassifier);  
var validationAccuracy = validation.errorMatrix('class', 'classification');  
print('Validation Accuracy:', validationAccuracy.accuracy());  

FAQ

  • How do I handle missing or unusable data in GEE?

    Use the updateMask() function to mask out invalid pixels, such as clouds, based on QA bands or other criteria.

  • What if my training samples are not balanced?

    Balance your training data by ensuring equal numbers of samples for each class, or use the weightProperty parameter in the classifier for reweighting.

  • How can I improve classification accuracy?

    Use additional spectral indices (e.g., NDVI, NDSI) or manually refine training data to better capture class variability.

  • What classifiers are available in GEE?

    GEE offers Random Forest, Naive Bayes, and Support Vector Machines (SVM), among others. Choose the one that suits your data and use case.

  • Can I export the classified image as a file?

    Yes, use the export.image() function to save the result in formats like GeoTIFF or PNG.

Similar Posts

Leave a Reply

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