Master expressions in Google Earth Engine to perform pixel-wise calculations and build custom remote sensing analyses.
Google Earth Engine (GEE) is a powerful platform for processing and analyzing geospatial data at a planetary scale. A core component of GEE's functionality is the ability to use expressions. Expressions allow you to perform complex calculations on image pixels, enabling a wide range of remote sensing analyses. This article will guide you through the use of expressions in Google Earth Engine, providing a practical example of how they can be used to calculate a vegetation index.
What This Script Does in Google Earth Engine
This Google Earth Engine script demonstrates how to use the expression()
function to calculate the Enhanced Vegetation Index (EVI) from MODIS surface reflectance data. The expression()
function allows you to define a mathematical formula using band names as variables. The script loads a MODIS image, applies a scaling factor, and then uses an expression to compute EVI. This example showcases the flexibility and power of GEE's expression capabilities for performing custom calculations on image data.
GEE Code Sample
// Compute Enhanced Vegetation Index (EVI) over the MODIS MOD09GA product
// using an expression.
// Load a MODIS image and apply the scaling factor.
var img = ee.Image('MODIS/006/MOD09GA/2012_03_09').multiply(0.0001);
// Compute EVI using an expression. The second argument is a map from
// variable name to band name in the input image.
var evi = img.expression(
'2.5 * (nir - red) / (nir + 6 * red - 7.5 * blue + 1)',
{
red: img.select('sur_refl_b01'), // 620-670nm, RED
nir: img.select('sur_refl_b02'), // 841-876nm, NIR
blue: img.select('sur_refl_b03') // 459-479nm, BLUE
});
// Center the map.
Map.setCenter(-94.84497, 39.01918, 8);
// Display the input image and the EVI computed from it.
Map.addLayer(img.select(['sur_refl_b01', 'sur_refl_b04', 'sur_refl_b03']),
{min: 0, max: 0.2}, 'MODIS bands 1/4/3');
Map.addLayer(evi, {min: 0, max: 1}, 'EVI');
Step-by-Step Explanation: Using the Expression Function
Here's a breakdown of how the expression()
function is used in this code:
- Load the Image:
var img = ee.Image('MODIS/006/MOD09GA/2012_03_09').multiply(0.0001);
This line loads a MODIS image and applies a scaling factor. The result is stored in the
img
variable, which is anee.Image
object. This image will be used as the input for the expression. - Define the Expression:
'2.5 * (nir - red) / (nir + 6 * red - 7.5 * blue + 1)'
This is a string that represents the mathematical formula for calculating EVI. Note that instead of using band names directly, it uses the variables
red
,nir
, andblue
. These variables will be mapped to actual band values in the next step. - Map Variables to Bands:
{
red: img.select('sur_refl_b01'), // 620-670nm, RED
nir: img.select('sur_refl_b02'), // 841-876nm, NIR
blue: img.select('sur_refl_b03') // 459-479nm, BLUE
}This JavaScript object is the second argument to the
expression()
function. It creates a mapping between the variables used in the expression string (red
,nir
,blue
) and the corresponding bands in theimg
image. For example,red: img.select('sur_refl_b01')
tells GEE to get the values from the'sur_refl_b01'
band of theimg
image and use those values whenever the variablered
is encountered in the expression. - Calculate EVI:
var evi = img.expression(..., ...);
The
img.expression()
function takes the expression string and the variable mapping as input. It then performs the calculation for each pixel in theimg
image, using the specified bands. The result is a newee.Image
where each pixel contains the calculated EVI value. This new image is stored in theevi
variable.
Applications
Expressions in Google Earth Engine are incredibly versatile and can be applied to a wide range of applications, including:
- Vegetation Index Calculation: As demonstrated in the example, expressions can be used to calculate various vegetation indices (e.g., NDVI, EVI, SAVI) from multispectral imagery.
- Band Arithmetic: Perform basic arithmetic operations (addition, subtraction, multiplication, division) between different bands of an image.
- Image Transformation: Apply mathematical functions (e.g., logarithms, exponentials, trigonometric functions) to image pixel values.
- Unit Conversion: Convert pixel values from one unit to another (e.g., from digital numbers to physical units like reflectance or temperature).
- Spectral Analysis: Combine spectral bands to highlight specific features or materials.
- Custom Indices: Create custom indices tailored to specific applications or research questions.
- Data Masking: Use conditional statements within expressions to mask out unwanted pixels (e.g., clouds, water).
Visualization Example
To visualize the result of the expression (the EVI calculation):
- Run the provided script in the Google Earth Engine Code Editor.
- Observe the EVI layer overlaid on the map. The EVI values, derived from the expression, will be displayed with a color gradient representing vegetation health.
- Take a screenshot of the EVI visualization in the Code Editor.
- Upload this screenshot to your Blogger post, ensuring you add descriptive alt text.
Example of EVI visualization derived from a GEE expression.
Notes
- Scaling Factors: It's crucial to apply any necessary scaling factors to the input bands before using them in an expression, as shown in the example.
- Band Names: Pay close attention to band names and ensure they match the names used in the
img.select()
function. - Data Types: Be mindful of data types. Expressions typically work with numerical data.
- Error Handling: GEE will report errors in your expression if there are syntax errors or invalid operations.
- Complex Expressions: You can create very complex expressions with multiple operations and functions, but keep them organized for readability.
- EE Functions in Expressions: While many basic mathematical operators are available, some Earth Engine functions may not be directly usable within an expression.
📚 References
- Google Earth Engine Documentation - Image Expressions: https://developers.google.com/earth-engine/apidocs/ee-image-expression
- Google Earth Engine Scripts
Prepared by: Jamal Chaaouan | GEE Academy @ GeoJamal.com
COMMENTS