Google Earth Engine Tutorial: Supervised Classification with Sentinel-2A – Part 1
Credit: Youtube Channel “Terra Spatial, First part of tutorial on supervised classification using high-resolution Sentinel-2A imagery.”
You can see all the tutorials from here: Techgeo Academy.
Google Earth Engine Tutorial: Supervised Classification with Sentinel-2A – Part 1
Supervised classification in Google Earth Engine involves training a model using labeled sample data to categorize land cover or land use in satellite imagery. This tutorial focuses on using Sentinel-2A data, which provides high-resolution optical imagery (10m, 20m, and 60m resolutions) across 13 spectral bands. The steps outlined here will guide you through preparing the data, training the classifier, and applying it to a study area.
Step 1: Accessing Sentinel-2A Data
Start by importing the Sentinel-2A dataset using the ee.ImageCollection('COPERNICUS/S2_SR') function. Filter the collection by date, region, and cloud cover to ensure relevance and quality.
- Example code:
var s2 = ee.ImageCollection('COPERNICUS/S2_SR').filterDate('2020-01-01', '2020-12-31') - Subset the region:
.filter(ee.Filter.geometry(ee.Geometry.Rectangle([x1, y1, x2, y2]))) - Remove clouds:
.filter(ee.Filter.lt('CLOUDY_PIXEL_PERCENTAGE', 10))
Step 2: Preprocessing the Data
Preprocessing includes band selection, normalization, and cloud masking. Sentinel-2A data includes a QA60 band for cloud detection. Use this band to exclude cloudy pixels from the analysis.
Apply a function to mask clouds:
function maskS2Clouds(image) {
var cloudBitMask = 1 << 10;
var cirrusBitMask = 1 << 11;
var qa = image.select('QA60');
var cloud = qa.bitwiseAnd(cloudBitMask).eq(0);
var cirrus = qa.bitwiseAnd(cirrusBitMask).eq(0);
return image.updateMask(cloud).updateMask(cirrus);
}
Composite the images using median or mean to reduce noise and ensure a clear dataset.
Step 3: Creating Training Data
Use a manually defined region of interest (ROI) or upload a shapefile to create training samples. Define classes (e.g., water, forest, urban) and extract spectral signatures for each class.
var trainingData = ee.FeatureCollection([
ee.Feature(ee.Geometry.Rectangle([x1, y1, x2, y2]), {'class': 1}),
ee.Feature(ee.Geometry.Rectangle([x3, y3, x4, y4]), {'class': 2})
]);
Extract training samples from the image:
var training = s2.select(['B2', 'B3', 'B4', 'B8']).sampleRegions({
collection: trainingData,
properties: ['class'],
scale: 10
});
Step 4: Training the Classifier
Choose a classifier algorithm, such as Random Forest, SVM, or Decision Tree. Train the model using the training data.
var classifier = ee.Classifier.randomForest(10).train({
trainingData: training,
predictors: ['B2', 'B3', 'B4', 'B8'],
classProperty: 'class'
});
Validate the classifier with a test dataset to assess accuracy.
Step 5: Applying the Classifier to the Image
Apply the trained classifier to the entire image collection. This creates a land cover map based on the defined classes.
var classified = s2.select(['B2', 'B3', 'B4', 'B8']).classify(classifier);
Visualize the result by adding the classified image to the map. Use a color palette to distinguish classes.
FAQ
What is supervised classification in GEE?
Supervised classification requires labeled training data to teach the model how to distinguish between classes. It uses algorithms like Random Forest to generalize patterns from the training dataset and apply them to satellite imagery.
Why is Sentinel-2A chosen for this task?
Sentinel-2A offers high spatial resolution and multiple spectral bands, making it ideal for detailed land cover analysis. Its 5-day revisit time and free access enhance its utility for environmental monitoring.
What are common classifiers in GEE?
Random Forest, SVM, and Decision Tree are widely used. Each has strengths: Random Forest handles non-linear relationships, SVM is effective for small datasets, and Decision Tree is interpretable for simple classifications.
How can I handle cloud cover in Sentinel-2A data?
Use the QA60 band to mask clouds. The maskS2Clouds function in the example above can help exclude cloudy pixels, improving classification accuracy.
How do I improve classification accuracy?
Ensure high-quality training data with well-defined classes. Use more bands or spectral indices (e.g., NDVI) as predictors. Test and validate the classifier with an independent dataset.






