What You'll Learn
- Define an Area of Interest (AOI) using geometry tools
- Use the reduceRegion() function to calculate zonal statistics
- Understand different reducers (mean, min, max, stdDev)
- Create and customize histograms with ui.Chart
- Handle maxPixels errors for large regions
Building On Previous Learning
This lab extends Lab 15 where you loaded and visualized SRTM elevation data. Now you'll extract meaningful statistics from that data.
Why This Matters
Zonal statistics are fundamental for quantitative analysis:
- Watershed analysis: Calculate average elevation, relief, etc.
- Land management: Summarize vegetation indices by parcel or county
- Climate studies: Extract mean temperature for regions
- Impact assessment: Compare before/after statistics
Before You Start
- Prerequisites: Complete Lab 15 and ensure your SRTM data is loaded.
- Estimated time: 60 minutes
- Materials: Earth Engine access, Lab 15 code, and a spreadsheet for recording statistics.
Key Terms
- Zonal Statistics
- Summary statistics (mean, sum, etc.) calculated for pixels within a defined zone or region.
- Reducer
- A GEE object that aggregates data (e.g., ee.Reducer.mean(), ee.Reducer.max()).
- reduceRegion()
- A method that applies a reducer to all pixels within a geometry and returns a dictionary of results.
- Histogram
- A chart showing the frequency distribution of values in a dataset.
Step 1: Load SRTM Data
Start with your Lab 15 code or use this starter:
// Load SRTM elevation data
var srtm = ee.Image("USGS/SRTMGL1_003");
// Center on Grand Canyon
Map.setCenter(-112.8598, 36.2841, 9);
// Display the elevation
Map.addLayer(srtm, {min: 0, max: 3000, palette: ['blue', 'green', 'yellow', 'red']}, 'Elevation');
Step 2: Define Your Area of Interest
Use the geometry tools to draw an AOI:
- Open the Geometry Tools panel in the Code Editor
- Click on the rectangle tool
- Draw a rectangle on the map
- Rename the geometry to 'aoi' in the imports section
What is an AOI? An Area of Interest is a specific geographic area that defines the focus of your analysis. All statistics will be calculated only for pixels within this boundary.
Step 3: Calculate Zonal Statistics
Use reduceRegion() with different reducers:
// Calculate maximum elevation
var max = srtm.reduceRegion({
reducer: ee.Reducer.max(),
geometry: aoi,
scale: 90
});
// Calculate mean elevation
var mean = srtm.reduceRegion({
reducer: ee.Reducer.mean(),
geometry: aoi,
scale: 90
});
// Calculate minimum elevation
var min = srtm.reduceRegion({
reducer: ee.Reducer.min(),
geometry: aoi,
scale: 90
});
// Calculate standard deviation
var stdd = srtm.reduceRegion({
reducer: ee.Reducer.stdDev(),
geometry: aoi,
scale: 90
});
// Print all statistics
print('Max:', max, 'Min:', min, 'Mean:', mean, 'StdDev:', stdd);
reduceRegion() Parameters
| Parameter | Description | Example |
|---|---|---|
reducer |
The aggregation method to use | ee.Reducer.mean() |
geometry |
The region to analyze | aoi (your drawn polygon) |
scale |
Resolution in meters | 90 |
maxPixels |
Maximum pixels to process (optional) | 1e13 |
Common Reducers
| Reducer | What It Calculates |
|---|---|
ee.Reducer.mean() |
Average value |
ee.Reducer.min() |
Minimum value |
ee.Reducer.max() |
Maximum value |
ee.Reducer.stdDev() |
Standard deviation |
ee.Reducer.sum() |
Sum of all values |
ee.Reducer.count() |
Number of pixels |
ee.Reducer.median() |
Median value |
Step 4: Create a Histogram
Visualize the distribution of elevation values:
// Create histogram
var histogram = ui.Chart.image.histogram({
image: srtm,
region: aoi,
scale: 90,
minBucketWidth: 10
});
// Customize the histogram appearance
histogram.setOptions({
title: 'Histogram of Elevation (meters)',
vAxis: {title: 'Frequency'},
hAxis: {title: 'Elevation (m)'},
legend: {position: 'none'},
colors: ['#1a73e8']
});
// Print the histogram to the console
print(histogram);
Check Your Understanding
- What does the scale parameter control in reduceRegion()?
- If your AOI contains 10,000 pixels and you use ee.Reducer.mean(), how many values will be returned?
- What would a histogram with two peaks (bimodal distribution) suggest about your landscape?
- How would you calculate the total relief (max - min) in your AOI?
Troubleshooting
Solution: Add maxPixels: 1e13 to your reduceRegion() call, or
increase the scale value to sample fewer pixels.
Solution: Check that your AOI is defined and has the correct variable name. Make sure you're using print(histogram) not Map.addLayer().
Solution: Your AOI may not overlap with the image, or the scale is too coarse. Try a smaller scale value or check your geometry.
Pro Tips
- Combine reducers: Use ee.Reducer.mean().combine() to calculate multiple stats in one call
- Use reduceRegions(): For multiple zones, use reduceRegions() with a FeatureCollection
- Export to CSV: Use Export.table.toDrive() to save statistics
- Best scale: Use the native resolution of your data when possible
Key Takeaways
- reduceRegion() calculates statistics for all pixels within a geometry
- Different reducers provide different summary statistics
- Histograms help visualize the distribution of values
- Use maxPixels to handle large regions or reduce scale
📋 Lab Submission
Subject: Lab 16 - Zonal Statistics - [Your Name]
Submit:
A shareable URL to your GEE code that includes:
- SRTM data loaded and displayed
- AOI defined with geometry tools
- All four statistics calculated (min, max, mean, stdDev)
- Histogram created and customized
- Comments explaining your code