Assignment 3: Forest Detection with Random Forest

Use a supervised machine learning model to classify forest and non-forest areas from Sentinel-2 imagery.

What is Supervised Classification?

Unlike unsupervised classification where the algorithm finds patterns on its own, supervised classification requires you to provide "training data"—examples of the classes you want to map. In this case, you will teach the algorithm what "forest" and "non-forest" look like by drawing points or polygons on the map. The **Random Forest** algorithm will learn the spectral signatures from your examples and then apply that knowledge to classify the entire image. This method is generally more accurate than unsupervised approaches.

Example of a GeoTIFF opened in GIS software

Instructions

1

Create Training Data

In the GEE Code Editor, use the geometry tools to create two new `FeatureCollection`s. Name one `forest` and the other `nonforest`. Digitize at least 15-20 points or small polygons for each class, spreading them across your Area of Interest (AOI).

2

Prepare Sentinel-2 Imagery

Filter the Sentinel-2 image collection by your AOI, a date range, and cloud cover. Create a single, clean composite image using the `.median()` reducer.

3

Sample, Split, and Train

Merge your `forest` and `nonforest` collections. Use `sampleRegions` to extract pixel values. Then, split your samples into a training set (70%) and a validation set (30%). Train a `smileRandomForest` classifier using the training set.

4

Classify and Assess Accuracy

Classify your composite image with the trained model. Then, use the validation set to create an `errorMatrix` and print the overall accuracy to the console. This tells you how well your model performed.

5

Visualize the Map

Add your final classified map to the viewer with a color palette to distinguish between forest and non-forest areas.

Starter Code Snippet

Use this code in the GEE Code Editor. You will need to create the `forest` and `nonforest` feature collections yourself using the map tools.

// IMPORTANT: Create two FeatureCollections named 'forest' and 'nonforest'
// using the geometry tools in the Code Editor before running this script.
// Add a property 'landcover' to each feature: 1 for forest, 0 for non-forest.

// Load Sentinel-2 image collection.
var s2 = ee.ImageCollection("COPERNICUS/S2");

// Define an area of interest (AOI).
var aoi = ee.Geometry.Polygon(
    [[[99.03, 13.38], [99.03, 12.55], [100.10, 12.55], [100.10, 13.38]]], null, false);

// Filter and preprocess the image collection.
var image = s2
  .filterBounds(aoi)
  .filterDate("2024-01-01", "2024-02-01")
  .filter(ee.Filter.lt("CLOUDY_PIXEL_PERCENTAGE", 5))
  .median()
  .clip(aoi);

// Prepare training data.
var trainingPoints = forest.merge(nonforest);

// Sample training data from the image.
var sample = image.sampleRegions({
  collection: trainingPoints,
  properties: ['landcover'],
  scale: 30
});

// Split data into training (70%) and validation (30%).
var sampleWithRandom = sample.randomColumn('random', 42);
var training = sampleWithRandom.filter(ee.Filter.lt('random', 0.7));
var validation = sampleWithRandom.filter(ee.Filter.gte('random', 0.7));

// Train a classifier.
var classifier = ee.Classifier.smileRandomForest(10).train({
  features: training,
  classProperty: 'landcover',
  inputProperties: image.bandNames()
});

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

// Assess accuracy.
var validationClassified = validation.classify(classifier);
var errorMatrix = validationClassified.errorMatrix('landcover', 'classification');
print('Error Matrix:', errorMatrix);
print('Overall Accuracy:', errorMatrix.accuracy());

// Define visualization parameters and display results.
var visParams = {min: 0, max: 1, palette: ["yellow", "green"]};
Map.centerObject(aoi, 8);
Map.addLayer(classified, visParams, "Classified Image");
Map.addLayer(trainingPoints, {color: 'red'}, "Training Points");
Start Assignment

Expected Result

Your final output will be a map showing forest areas in green and non-forest areas in yellow. In the console, you will see the error matrix and the overall accuracy of your classification, which should ideally be above 90% (0.90).

Example of a classified forest map