Use reduceRegion in Google Earth Engine GEE to extract the maximum elevation from SRTM data within a defined rectangle— a spatial statistics tool.
Google Earth Engine (GEE) provides powerful tools for summarizing image data
over specific geographic areas. The reduceRegion
function is a
key tool for this, allowing you to calculate statistics like the mean,
maximum, or sum of pixel values within a defined region. This article will
guide you through a GEE script that demonstrates how to use
reduceRegion
to compute the maximum elevation value within a
rectangular area using SRTM elevation data.
What This Script Does
This Google Earth Engine script calculates the maximum elevation within a
user-defined rectangular region using the reduceRegion
function.
It loads an SRTM elevation image, defines a rectangular geometry, and then
uses reduceRegion
with the ee.Reducer.max()
reducer
to find the highest elevation value within that rectangle. The result is
printed to the console as a dictionary. The script also adds the image and the
rectangle to the map for visualization.
Google Earth Engine (GEE) Code Sample
// ..The input image to reduce, in this case an SRTM elevation map.
var image = ee.Image('CGIAR/SRTM90_V4');
Map.addLayer(image);
// The region to reduce within.
var poly = ee.Geometry.Rectangle([-109.05, 41, -102.05, 37]);
Map.addLayer(poly);
// Reduce the image within the given region, using a reducer that
// computes the max pixel value. We also specify the spatial
// resolution at which to perform the computation, in this case 200
// meters.
var max = image.reduceRegion({
reducer: ee.Reducer.max(),
geometry: poly,
scale: 200
});
// Print the result (a Dictionary) to the console.
print(max);
Step-by-Step Explanation of the Google Earth Engine Code:
-
Load the SRTM elevation image:
var image = ee.Image('CGIAR/SRTM90_V4');
This line loads the SRTM 90m Digital Elevation Model (DEM) into a variable named
image
. This image contains the elevation data that will be used for the reduction.Map.addLayer(image);
adds the image to the map for visualization. -
Define the region of interest:
var poly = ee.Geometry.Rectangle([-109.05, 41, -102.05, 37]);
This line defines a rectangular region of interest (ROI) using the
ee.Geometry.Rectangle
constructor. The coordinates[-109.05, 41, -102.05, 37]
specify the West, South, East, and North longitudes and latitudes of the rectangle, respectively.Map.addLayer(poly);
adds the rectangle to the map for visualization. -
Reduce the image:
var max = image.reduceRegion({ ... });
This is the core of the script. It calls the
reduceRegion
method on theimage
to calculate a statistic within the defined region.-
reducer: ee.Reducer.max()
: This specifies the reducer to use.ee.Reducer.max()
calculates the maximum pixel value within the region. Other reducers are available for calculating other statistics (e.g.,ee.Reducer.mean()
,ee.Reducer.sum()
,ee.Reducer.min()
). -
geometry: poly
: This specifies the geometry (the rectangle defined earlier) over which the reduction is performed. -
scale: 200
: This parameter defines the spatial resolution (in meters) at which the reduction is performed. In this case, the image is resampled to 200-meter pixels before the maximum value is calculated. This is important for performance and can affect the result.
-
-
Print the result:
print(max);
The
reduceRegion
function returns a dictionary containing the calculated maximum value. This line prints the dictionary to the console. The dictionary key will be the name of the band (in this case, 'elevation'), and the value will be the maximum elevation within the specified rectangle.
Datasets Used
-
SRTM 90m Digital Elevation Model: (CGIAR/SRTM90_V4)
- This dataset provides the elevation data used for the reduction.
Applications
The reduceRegion
function is highly versatile and can be used in
numerous applications, including:
- Calculating zonal statistics: Extracting statistics (e.g., mean elevation, average temperature, total rainfall) for administrative zones, watersheds, or other geographic regions.
- Summarizing land cover: Determining the proportion of different land cover types within a specific area.
- Analyzing image collections: Calculating statistics across a time series of images for a given region.
- Validating model results: Comparing model predictions to observed data aggregated over a region.
- Extracting pixel values: Getting the values of specific pixels within a defined geometry.
Visualization Example
The script displays the SRTM elevation image and the rectangular region of interest on the map. The actual result (the maximum elevation value) is printed to the console. To see the result, you would need to open the console in the Google Earth Engine Code Editor.
The results of reduceRegion are showing in the console.
Notes
-
The
scale
parameter is crucial. A smaller scale (higher resolution) will generally provide more accurate results but will take longer to compute. -
The
geometry
parameter can be anyee.Geometry
object. -
You can use different reducers (e.g.,
ee.Reducer.mean()
,ee.Reducer.min()
,ee.Reducer.sum()
,ee.Reducer.stdDev()
). -
If the input image has multiple bands, the
reduceRegion
function will calculate the statistic for each band. - For complex reductions, combine multiple reducers.
Related Articles :
Prepared by: Jamal Chaaouan | GEE Academy @ GeoJamal.com
COMMENTS