Before starting, make sure you understand these terms. They appear throughout all code blocks in this course.
| Term | Full Name | What it means in simple words |
|---|---|---|
| Mangrove | Mangrove Forest | Salt-tolerant coastal woodland ecosystems. |
| Deforestation | Deforestation | The clearing of forests for other land uses. |
| Remote Sensing | Remote Sensing | Acquiring information about an object or area from a distance. |
| GEE | Google Earth Engine | A cloud-based platform for geospatial analysis. |
| GFC | Global Forest Change | A dataset mapping global forest cover change from 2000 to present. |
| Band | Spectral Band | A specific range of wavelengths in the electromagnetic spectrum. |
| Threshold | Threshold | A value used to separate data into different categories. |
| Classification | Image Classification | The process of assigning pixels to different categories based on their spectral characteristics. |
| Pixel | Picture Element | The smallest addressable element in an image. |
| Cloud Masking | Cloud Masking | The process of removing clouds from satellite imagery. |
| Time Series | Time Series Analysis | A sequence of data points indexed in time order. |
| DeepSeek | DeepSeek LLM | Large Language Model for code generation and analysis. |
| Image Collection | Image Collection | A stack or time series of images in GEE. |
| Region of Interest | ROI | A specific geographic area selected for analysis. |
Mangrove forests are vital ecosystems that provide numerous benefits. This course will guide you through the process of detecting mangrove loss using the Hansen Global Forest Change dataset in Google Earth Engine. You will learn how to use DeepSeek AI to refine your analysis and gain deeper insights into deforestation patterns in North Morocco.
UMD/hansen/global_forest_change_2022_v1_10You need two browser tabs open side by side throughout this course.
Course14_Mangrove Loss Detection with GFC and DeepSeek
The prompt is the core skill of this course. A good prompt includes the exact dataset ID, band names, study area coordinates, dates, preprocessing steps, and desired outputs.
Paste the following code into your GEE code editor and click Run. Read the beginner notes below to understand what each line does.
// Define the region of interest (North Morocco).
var roi = ee.Geometry.Rectangle([-5.5, 33.5, -2.0, 35.5]);
// Import the Hansen Global Forest Change dataset.
var gfc2022 = ee.Image('UMD/hansen/global_forest_change_2022_v1_10');
// Select the tree cover, loss year, and gain bands.
var treecover2000 = gfc2022.select('treecover2000');
var lossyear = gfc2022.select('lossyear');
var gain = gfc2022.select('gain');
// Filter the data to areas with tree cover greater than 30% in 2000.
var treecoverThreshold = 30;
var initialCover = treecover2000.gte(treecoverThreshold);
// Filter the lossyear data to the period between 2010 and 2020 (lossyear values are 0-indexed).
var lossStartYear = 10; // Represents 2010
var lossEndYear = 20; // Represents 2020
var loss = lossyear.gte(lossStartYear).and(lossyear.lte(lossEndYear)).and(initialCover);
Add this block after the previous code. It continues the analysis with additional processing and visualization.
// Create a visualization of the forest loss.
var lossVisualization = {
palette: ['FF0000'], // Red color for loss
min: 0,
max: 1
};
// Add the loss layer to the map.
Map.addLayer(loss.clip(roi), lossVisualization, 'Forest Loss 2010-2020');
// Calculate the area of forest loss in hectares.
var lossAreaImage = loss.multiply(ee.Image.pixelArea());
var lossArea = lossAreaImage.reduceRegion({
reducer: ee.Reducer.sum(),
geometry: roi,
scale: 30, // Resolution of the Hansen GFC data
maxPixels: 1e13
});
// Print the area of forest loss to the console.
print('Area of Forest Loss (Hectares):', lossArea.get('lossyear'));
Add this block at the end of your script to export the final map as a GeoTIFF to your Google Drive. After clicking Run, go to the Tasks tab in GEE and click Run next to each export task.
// Export the loss image to Google Drive.
Export.image.toDrive({
image: loss.clip(roi),
description: 'forest_loss_2010_2020',
folder: 'GEE_Exports',
scale: 30,
region: roi,
maxPixels: 1e13
});
Use DeepSeek to extend the script for each exercise below. Write a follow-up prompt describing what you want to add, then copy the generated code and paste it into your GEE script.
To get the best results from DeepSeek for GEE tasks, always include these elements in your prompts:
| Element | Example |
|---|---|
| Exact dataset ID | UMD/hansen/global_forest_change_2022_v1_10 |
| Band names | "select band 'B4' for red, 'B8' for NIR" |
| Study area | "rectangle [-6.5, 33.0, -1.5, 36.0] North Morocco" |
| Date range | "filter from 2020-01-01 to 2023-12-31" |
| Output format | "print area in hectares, add a legend, export to Drive" |
| Error fixing | "The code gives error X. Please fix it." |
| Iteration | "Add a time series chart for each year 2018-2023" |