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 |
|---|---|---|
| NDVI | Normalized Difference Vegetation Index | A simple graphical indicator that measures the difference between near-infrared (which vegetation strongly reflects) and red light (which vegetation absorbs) |
| Landsat 8 | Landsat 8 Satellite | An Earth observation satellite launched in 2013, providing moderate-resolution imagery of the Earth's surface. |
| Surface Reflectance | Surface Reflectance | The fraction of incoming solar radiation that is reflected from the Earth's surface. |
| GEE | Google Earth Engine | A cloud-based platform for planetary-scale geospatial analysis. |
| Image Collection | Image Collection | A stack or time series of images in Google Earth Engine. |
| Band | Spectral Band | A specific range of wavelengths in the electromagnetic spectrum that a sensor is sensitive to. |
| Pixel | Picture Element | The smallest addressable element in an image. |
| Cloud Masking | Cloud Masking | The process of identifying and removing clouds from satellite imagery. |
| Urban Green Space | Urban Green Space | Areas covered with vegetation in urban environments, such as parks, gardens, and street trees. |
| Rif Mountains | Rif Mountains | A mountain range in northern Morocco. |
| Middle Atlas | Middle Atlas | A mountain range in Morocco, part of the Atlas Mountains system. |
| DeepSeek | DeepSeek Coder | An AI model specialized in code generation and completion, useful for scripting in GEE. |
| Composite | Image Composite | A single image created by combining multiple images, often used to reduce cloud cover. |
| Reducer | Reducer | A function in GEE that aggregates data, such as calculating the mean value over a region. |
This course introduces the fundamentals of analyzing urban green space using remote sensing data and Google Earth Engine (GEE). We'll focus on calculating the Normalized Difference Vegetation Index (NDVI) from Landsat 8 imagery to assess vegetation health in urban areas of North Morocco. Additionally, we'll explore how to leverage AI tools like DeepSeek to assist in code generation for GEE.
LANDSAT/LC08/C01/T1_SRYou need two browser tabs open side by side throughout this course.
Course17_Urban Green Space Analysis with NDVI 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 (Rif Mountains and Middle Atlas)
var roi = ee.Geometry.Polygon([
[-5.5, 35.0], [-3.5, 35.0], [-3.5, 33.0], [-5.5, 33.0]
]);
// Define the date range
var startDate = '2022-01-01';
var endDate = '2022-12-31';
// Load Landsat 8 surface reflectance data
var landsat8 = ee.ImageCollection('LANDSAT/LC08/C01/T1_SR')
.filterDate(startDate, endDate)
.filterBounds(roi);
Add this block after the previous code. It continues the analysis with additional processing and visualization.
// Function to mask clouds using the pixel_qa band
var maskClouds = function(image) {
var qa = image.select('pixel_qa');
var cloudBitMask = 1 << 5;
var cloudShadowBitMask = 1 << 3;
var mask = qa.bitwiseAnd(cloudBitMask).eq(0)
.and(qa.bitwiseAnd(cloudShadowBitMask).eq(0));
return image.updateMask(mask);
};
// Apply cloud masking
var landsat8_masked = landsat8.map(maskClouds);
// Function to calculate NDVI
var calculateNDVI = function(image) {
return image.normalizedDifference(['B5', 'B4']).rename('NDVI');
};
// Calculate NDVI for each image
var ndviCollection = landsat8_masked.map(calculateNDVI);
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.
// Composite the NDVI collection and export the mean NDVI image.
var ndvi_composite = ndviCollection.median();
Export.image.toDrive({
image: ndvi_composite,
description: 'NDVI_North_Morocco_2022',
folder: 'GEE_Exports',
region: roi,
scale: 30,
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 | LANDSAT/LC08/C01/T1_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" |