GEE Tutorials

Google Earth Engine Tutorial: Supervised Classification with Sentinel-2A – Part 2

Credit: Youtube Channel “Terra Spatial, Second part completing the Sentinel-2A classification process with result validation.”

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

Google Earth Engine Tutorial: Supervised Classification with Sentinel-2A – Part 2

Supervised classification in Google Earth Engine (GEE) involves training a machine learning model using labeled training data and applying it to raster imagery. This tutorial continues from the initial steps of data loading and preprocessing, focusing on the classification workflow using Sentinel-2A data.

Step 1: Gather and Prepare Training Data

To perform supervised classification, you need a labeled dataset. This can be a shapefile or a FeatureCollection containing polygons for each land cover class. For demonstration, let’s assume you’ve created a FeatureCollection with class labels (e.g., “forest,” “urban,” “water”).


// Example: Create a FeatureCollection for training data
var trainingData = ee.FeatureCollection([
  ee.Feature(geometry1, {label: 'forest'}),
  ee.Feature(geometry2, {label: 'urban'}),
  ee.Feature(geometry3, {label: 'water'})
]);
  

Step 2: Extract Features from Sentinel-2A Data

Select the bands relevant to your classification task. Common bands for land cover analysis include B2 (blue), B3 (green), B4 (red), and B8 (near-infrared). Normalize values if necessary and clip the image to your study area.


// Load and filter Sentinel-2A data
var s2 = ee.ImageCollection('COPERNICUS/S2_SR')
  .filterDate('2021-01-01', '2021-12-31')
  .filter(ee.Filter.lt('CLOUDY_PIXEL_PERCENTAGE', 20));

// Select and normalize bands
var image = s2.select(['B2', 'B3', 'B4', 'B8']).first().divide(10000);
  

Step 3: Train the Classifier

Use GEE’s built-in classifiers (e.g., Random Forest, CART) to train your model. Split your training data into training and validation subsets, and assign the features and labels accordingly.


// Split training data into training and validation sets
var training = trainingData.sample({fraction: 0.8, seed: 1, idx: 'random'});
var validation = trainingData.sample({fraction: 0.2, seed: 1, idx: 'random'});

// Train the classifier
var classifier = ee.Classifier.randomForest(10, 10).train({
  features: training,
  classProperty: 'label',
  inputProperties: ['B2', 'B3', 'B4', 'B8']
});
  

Step 4: Apply the Classifier to the Image

Once the classifier is trained, apply it to the entire image to generate a classified map.


// Apply the classifier to the image
var classified = image.classify(classifier);
  

Step 5: Evaluate Model Accuracy

Use the validation data to assess the accuracy of your classification. Compute metrics like overall accuracy and a confusion matrix.


// Evaluate the classifier on validation data
var validationResults = validation.classify(classifier);
var confusionMatrix = validationResults.errorMatrix('label', 'classification');
print('Confusion Matrix:', confusionMatrix);
print('Overall Accuracy:', confusionMatrix.accuracy());
  

Step 6: Visualize the Classified Image

Add the classified image to the map with a palette for different classes (e.g., green for forest, blue for water).


// Set visualization parameters
var palette = ['green', 'red', 'blue']; // Example for forest, urban, water

// Display the classified image
Map.addLayer(classified, {palette: palette}, 'Classified Image');
  

Step 7: Export the Results

Export the classified image to your Google Drive or Cloud Storage for further analysis.


// Export the classified image
Export.image.toDrive({
  image: classified,
  description: 'classified_image',
  folder: 'GEE_Classification',
  fileNamePrefix: 'sentinel2_classification',
  region: geometry,
  crs: 'EPSG:4326',
  scale: 10,
  maxPixels: 1e10
});
  

FAQ

How do I choose the right classifier for my data?

GEE supports multiple classifiers, such as Random Forest, CART, and SVM. Consider the complexity of your data and the number of classes. Random Forest is robust for large datasets, while CART is simpler and faster.

What if my training data is imbalanced?

Imbalanced data can skew classification results. Use stratified sampling to ensure all classes are adequately represented or apply cost-sensitive learning by weighting classes during training.

How can I improve classification accuracy?

Include more diverse training samples, use higher-resolution data, or incorporate additional spectral bands and indices (e.g., NDVI, EVI). Preprocessing steps like cloud masking and normalization also enhance accuracy.

Can I use multiple satellite datasets for training?

Yes, you can combine data from different sources (e.g., Landsat and Sentinel-2) by merging FeatureCollections or using multi-temporal composites to enrich the feature space.

How do I handle spectral bands with varying resolutions?

Resample lower-resolution bands to match higher ones before analysis. Use .resample() or .reduceResolution() on the image to ensure consistency across all bands.

Similar Posts

Leave a Reply

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