Introduction
Google Earth Engine (GEE) is a powerful platform for analyzing remote sensing data. It provides access to an extensive library of datasets, including satellite imagery from Landsat and Sentinel missions, as well as a variety of climate and weather records. With decades of archived data and a wealth of user-contributed datasets, GEE enables rapid data processing and visualization, allowing users to easily locate and analyze imagery of interest.
In this tutorial, I’ll guide you through the process of performing a supervised classification using Google Earth Engine. Since GEE uses a JavaScript-based API, we’ll start by exploring some basic functions and gradually build up to custom functions that enhance our classification workflow. This tutorial assumes you have a basic understanding of both GEE and programming concepts.
Getting Started
Before we begin the classification process, let’s prepare our imagery and workspace in GEE.
Visit the Google Earth Engine Code Editor to access the API. Here are a few tips to help you set up efficiently:
- Link your Google Drive: Make sure you’re signed in so your outputs and scripts can be saved.
- Organize your files: Create a folder in the left-side panel to keep things tidy.
- Save frequently: Changes are saved when you run the code, but it’s good practice to save manually as well.
- Use multiple scripts: Break your project into smaller files for better organization. This helps reduce clutter and makes debugging easier.
- Screen space matters: If you’re working on a smaller screen, keeping things organized becomes even more important.
Accessing and Working with Image Collections in Google Earth Engine
Google Earth Engine offers a vast array of datasets, most of which are organized into image collections. Understanding how to access and work with these collections is essential for effectively using the platform.
An image collection consists of multiple satellite images from the same data source, captured at different times and locations. These collections allow users to analyze changes over time or compare different regions with consistency in data quality and format.
For this tutorial, we’ll be using Landsat 8 imagery, which provides high-quality multispectral data ideal for various remote sensing tasks. To explore other available datasets, you can browse the Google Earth Engine Data Catalog.
To get started with Landsat 8:
- Go to the Landsat 8 Level 2 dataset page in the catalog.
- Copy the provided code snippet to load the collection into your script.
This will serve as the foundation for our supervised classification work. In the next section, we’ll begin filtering and visualizing this dataset for our specific area of interest.
var dataset = ee.ImageCollection('LANDSAT/LC08/C02/T1_L2')
.filterDate('2021-05-01', '2021-06-01');
// Applies scaling factors.
function applyScaleFactors(image) {
var opticalBands = image.select('SR_B.').multiply(0.0000275).add(-0.2);
var thermalBands = image.select('ST_B.*').multiply(0.00341802).add(149.0);
return image.addBands(opticalBands, null, true)
.addBands(thermalBands, null, true);
}
dataset = dataset.map(applyScaleFactors);
var visualization = {
bands: ['SR_B4', 'SR_B3', 'SR_B2'],
min: 0.0,
max: 0.3,
};
Map.addLayer(dataset, visualization, 'True Color (432)');
};Creating Geometry in Google Earth Engine
The first step in any spatial analysis is defining the area of interest (AOI). In Google Earth Engine (GEE), this is typically done by creating a geometry, such as a polygon or rectangle, which you will use to filter and clip satellite imagery.
How to Create a Geometry in GEE
Follow these steps to create your AOI directly within the Code Editor interface:
- Open the map view by pulling up the bottom panel.
- Hover over the “Geometry Imports” tab in the top-left corner of the map.
- Click “+ new layer” at the bottom of that tab.
- Use the dropdown next to “Geometry Imports” to select either a rectangle or polygon (it defaults to a point).
- Click and draw your desired area on the map.
Once your geometry is drawn, rename it to something clear and meaningful, such as extent or AOI, as it will be referenced frequently throughout your code.
Note: In this tutorial, my geometry is named
extent, and you’ll see this used in the code examples.
Importing Predefined Geometry
In many cases, you may already have a predefined area boundary in the form of a vector (like a shapefile or CSV) or raster file. GEE allows you to import such files from your local computer:
- In the Code Editor, go to the “Assets” tab on the left panel.
- Click “New”, then select “Table upload”.
- Choose “Shapefiles” (if you’re uploading a
.shpfile) or CSV, depending on your data. - Upload all necessary files that accompany your shapefile (e.g.,
.shp,.shx,.dbf,.prj). - Give your asset a name and click “Import”.
Once uploaded, your geometry will appear in your script and can be used just like a drawn polygon to subset and analyze satellite imagery.
Displaying Images in Google Earth Engine
Visualizing images in Google Earth Engine (GEE) is straightforward, but there are a few key details to understand in order to make your imagery look correct—especially when working with satellite data like Landsat 8.
Although we haven’t covered how to load an image just yet, it’s important to grasp how image display works, since this step will be repeated throughout the tutorial.
Basic Display Command
To display an image in GEE, use the following line of code:
javascriptCopyEditMap.addLayer(image);
That’s it! However, when you first add an image, you might run into two common issues:
- The image appears too dark
By default, GEE sets the display range of pixel values between 0 and 1, which is usually too narrow for satellite imagery. - The colors look incorrect
GEE uses bands 1, 2, and 3 for Red, Green, and Blue by default. For Landsat 8, however, the correct visible bands are:- B4 (Red)
- B3 (Green)
- B2 (Blue)
Fixing Image Display
To adjust the appearance of the image:
- Click on the image layer in the Layers panel.
- Open the dropdown next to “Visualization Parameters”.
- Change the stretch to either 3σ (three standard deviations) or 100%. This doesn’t alter the actual data—just how it looks on the map.
- Update the band combination to:
- Red: B4
- Green: B3
- Blue: B2
This will provide a natural color visualization for Landsat 8 imagery.
Setting Bands in Code
You can also specify the band combination directly in your code like this:
javascriptCopyEditMap.addLayer(image, {bands: ['B4', 'B3', 'B2']});
This ensures the correct Red-Green-Blue visualization is applied automatically when the image loads. While GEE doesn’t allow you to set a custom stretch in code, you can manually adjust the value range like so:
javascriptCopyEditMap.addLayer(image, {
bands: ['B4', 'B3', 'B2'],
min: 0,
max: 3000 // Adjust based on your data
});
This way, your image is not only visible but also visually accurate.
Preparing Imagery in Google Earth Engine
Google Earth Engine (GEE) provides access to a vast array of remote sensing datasets. You can easily search for collections using the search bar within the Code Editor. For this tutorial, we’ll be working with the USGS Landsat 8 Collection 1 Tier 1 Raw Scenes—a high-quality dataset ideal for analysis.
Exploring the Dataset
To begin, it’s helpful to inspect the properties of the image collection or a specific image. This helps you better understand metadata like acquisition dates, band names, and bit values. You can do this by using the print() function:
javascriptCopyEditprint(image);
This will display the details in the Console tab on the right side of your screen.
To make the Landsat 8 dataset easily accessible in your script, define a variable like so:
javascriptCopyEditvar L8 = ee.ImageCollection('LANDSAT/LC08/C01/T1');
Creating a Cloud-Free Mosaic
Now that we’ve linked the L8 variable to the Landsat 8 image collection, we can filter and process the imagery. In this example, we’ll create a cloud-free mosaic of Fort McMurray during the month following the major wildfire in May 2016.
We’ll use two custom functions:
- A cloud mask function using the
BQAband to remove cloud-contaminated pixels. - A composite function to generate a mosaic from the filtered images.
The BQA (Bit Quality Assurance) band in Landsat 8 imagery allows us to identify and mask clouds with moderate or high confidence. Below is a conceptual overview of what we’ll do:
- Filter the image collection by date and location.
- Apply the cloud mask function to each image.
- Create a median composite or mosaic to generate a single, cloud-free image.
Here’s a simplified snippet to illustrate the setup:
// Cloud mask for L8 imagery
var L8CloudMask = function(collection) {
var qa = collection.select('BQA'); //selects the BQA (quality) band
var CloudOrShadow = qa.bitwiseAnd(1<<4) //chooses pixels where cloud is present
.and(qa.bitwiseAnd(1<<6)) //and pixels where cloud confidence ic greater than 34%
.or(qa.bitwiseAnd(1<<8)) //or pixels where cloud shadow confidence is greater than 34%
.or(qa.bitwiseAnd(1<<12)); //or pixels where cirrus confidence is greater than 34%
var masked = collection.mask(); //creates a mask
return collection.updateMask(CloudOrShadow.not()).updateMask(masked); //updates mask to remove any pixels as described above in 'CloudOrShadow'
};
Pro Tip: Visualize Each Step
As you process your imagery, try adding intermediate results to the map using Map.addLayer(). It’s a great way to verify that each step is working as intended and deepen your understanding of the data.
Also, don’t forget to explore all available bands in your imagery. Landsat 8 offers a wide range of useful bands beyond just the visible spectrum—many of which can provide powerful insights depending on your application.
Creating a Composite Image in Google Earth Engine
Now that we’ve applied the cloud mask to our imagery, it’s time to generate a composite. This step is straightforward thanks to GEE’s built-in simpleComposite function, which selects the clearest pixels from a collection to create a single best-possible image.
Composite Function
Here’s the function we’ll use to generate our composite:
javascriptCopyEdit// Function to create composite
var createComp = function(collection) {
var comp = ee.Algorithms.Landsat.simpleComposite({
collection: collection,
asFloat: true
});
return comp;
};
With this function in place, we can now filter the image collection, apply the cloud mask, generate the composite, and clip it to our area of interest.
Generating and Clipping the Final Image
javascriptCopyEdit// Select all images within June 2016 that touch the extent and have the least removed from the cloud mask
var after = L8
.filterDate('2016-06-01', '2016-06-30')
.filterBounds(extent)
.map(L8CloudMask); // Applies the cloud mask
// Create composite of images chosen above
var compAfter = createComp(after);
// Clip to extent
var clipAfter = compAfter.clip(extent);
Adding Custom Bands (Optional)
Once you’ve created your composite image, you may want to enhance it by adding custom bands depending on your analysis needs. For instance, in areas affected by wildfire, the Normalized Burn Ratio (NBR) is a useful index for identifying burned regions.
Google Earth Engine makes it easy to compute NBR using the normalizedDifference() function:
javascriptCopyEdit// UDF to create and add an NBR band
var calcNBR = function(image){
var NBR = image.normalizedDifference(['B5', 'B7']).rename("NBR"); // NBR is the normalized difference between NIR and SWIR
var newImage = image.addBands(NBR);
return newImage;
};
// Add a normalized burn ratio band into the image
var bands = ['B2', 'B3', 'B4', 'B5', 'B6', 'B7', 'NBR'];
var clipAfter = calcNBR(clipAfter).select(bands);
Tip: Before adding the NBR band, we define a list of bands we want to retain for classification. This helps filter out unnecessary bands like coastal aerosol or the panchromatic band, keeping the dataset clean and focused.
Final Visualization
After preparing a cloud-free mosaic, adding the NBR band, and adjusting the visual parameters to show SWIR, NIR, and Green as Red, Green, and Blue respectively (a standard false-color combination for burn analysis), your image should now effectively highlight burned areas.