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 |
|---|---|---|
| NDRE | Normalized Difference Red Edge | An index that uses red edge and near-infrared bands to assess plant health. |
| Red Edge | Red Edge Bands | Specific wavelengths sensitive to chlorophyll content in plants. |
| Sentinel-2 | Sentinel-2 Mission | A European Earth observation mission providing high-resolution optical imagery. |
| GEE | Google Earth Engine | A cloud-based platform for geospatial data analysis. |
| Surface Reflectance | Surface Reflectance | The proportion of light reflected from the Earth's surface after atmospheric correction. |
| Crop Stress | Crop Stress | Environmental factors negatively impacting plant health and productivity. |
| ROI | Region of Interest | A specific geographic area selected for analysis. |
| Image Collection | Image Collection | A stack of images ordered by time in Google Earth Engine. |
| Band | Spectral Band | A specific range of wavelengths in the electromagnetic spectrum captured by a sensor. |
| Threshold | Threshold | A value used to separate different classes or features in an image. |
| Pixel | Pixel | The smallest unit of an image, representing a single value. |
| Cloud Masking | Cloud Masking | The process of removing cloud cover from satellite imagery. |
| NDVI | Normalized Difference Vegetation Index | A simple graphical indicator that assesses whether or not the target being observed contains live green vegetation. |
| DeepSeek | DeepSeek | A large language model used to interpret and analyze geospatial data. |
Crop stress can significantly impact agricultural yields. Remote sensing, particularly using Sentinel-2 data, offers a powerful way to monitor crop health over large areas. This course will guide you through using Sentinel-2 red edge bands and Google Earth Engine to detect crop stress in North Morocco, enhanced by DeepSeek for automated analysis.
COPERNICUS/S2_SRYou need two browser tabs open side by side throughout this course.
Course22_Crop Stress Detection with Sentinel-2 Red Edge 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 study area (North Morocco).
var roi = ee.Geometry.Polygon([[[-5.5, 32], [-3, 32], [-3, 35], [-5.5, 35]]]);
// Define the date range.
var startDate = '2023-06-01';
var endDate = '2023-08-31';
// Import Sentinel-2 SR data.
var s2 = ee.ImageCollection('COPERNICUS/S2_SR')
.filterBounds(roi)
.filterDate(startDate, endDate)
.filter(ee.Filter.lt('CLOUDY_PIXEL_PERCENTAGE', 20));
// Function to mask clouds using the QA60 band.
function maskS2clouds(image) {
var qa = image.select('QA60');
var cloudBitMask = 1 << 10;
var cirrusBitMask = 1 << 11;
var mask = qa.bitwiseAnd(cloudBitMask).eq(0)
.and(qa.bitwiseAnd(cirrusBitMask).eq(0));
return image.updateMask(mask).divide(10000); // Scale to 0-1
}
Add this block after the previous code. It continues the analysis with additional processing and visualization.
// Apply cloud masking and calculate NDRE.
var s2_masked = s2.map(maskS2clouds);
var ndre = s2_masked.map(function(image) {
return image.normalizedDifference(['B8A', 'B5']).rename('NDRE');
});
// Composite the NDRE images.
var ndre_composite = ndre.median();
// Define a threshold for crop stress (NDRE < 0.2).
var stress_threshold = 0.2;
// Create a binary mask for stressed areas.
var stressed_mask = ndre_composite.lt(stress_threshold);
// Visualize the results.
Map.centerObject(roi, 10);
Map.addLayer(ndre_composite, {min: 0, max: 0.7, palette: ['red', 'yellow', 'green']}, 'NDRE Composite');
Map.addLayer(stressed_mask.updateMask(stressed_mask), {palette: 'red'}, 'Stressed Areas');
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 stressed area mask to Google Drive.
Export.image.toDrive({
image: stressed_mask,
description: 'crop_stress_mask',
folder: 'GEE_Exports',
region: roi,
scale: 10,
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 | COPERNICUS/S2_SR |
| 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" |