Spring 2023 - Lab Lecture
Objective: The objective of this lab is to use Google Earth Engine to create an elevation model from SRTM and then use geometry to define an area of interest and run descriptive stats on the values there, including mean, median, max, min, and standard deviation with a histogram.
The specific steps involved in this lab will be:
- Import the SRTM data into Google Earth Engine (Done in Lab 15).
- Create a geometry object to define the area of interest (AOI).
- Calculate the mean, median, max, min, and standard deviation elevation values in the area of interest.
- Plot a histogram of the elevation values in the area of interest.
1- Import the SRTM data
This lab builds off of Lab 15 - Visualizing SRTM Data . You are welcome to open you verions of Lab 15 or use this one. https://code.earthengine.google.com/edb63ec580005384c9b70400c5caf2bd
Open Lab 15 and run it. This should show your the SRTM Data. We will use that data to run some stats on an Area of Interest.
2 - Define AOI
An area of interest (AOI) is a specific geographic area that is the focus of a remote sensing study. AOI can be defined by a variety of criteria, including location, size, shape, and land cover.
AOIs are used in remote sensing for a variety of purposes, including:
- Data collection: AOI can be used to define the area of interest for data collection, such as satellite imagery or ground surveys.
- Data analysis: AOI can be used to define the area of interest for data analysis, such as image classification or change detection.
- Model development: AOI can be used to define the area of interest for model development, such as land use models or climate models.
The geometry tools in Google Earth Engine can be used to draw an AOI in a variety of ways. One way is to use the rectangle tool. To do this, follow these steps:
- In the Google Earth Engine Code Editor, open the Geometry Tools panel.
- Click on the rectangle tool.
- On the map, click and drag to create a rectangle that defines the area of interest.
- Click on the "Apply" button.

- Rename the polygon to 'aoi'
Once you have drawn an AOI, you can use it in various ways in Google Earth Engine. For example, you will use it to run statistical analyses to make descriptive stats.
3 - Calculate Zonal Stats
To calculate the mean, median, max, min, and standard deviation of the elevation values in the AOI, you can use the following code:
var max = srtm.reduceRegion({
reducer: ee.Reducer.max(),
geometry: aoi,
scale: 90
});
var mean = srtm.reduceRegion({
reducer: ee.Reducer.mean(),
geometry: aoi,
scale: 90
});
var min = srtm.reduceRegion({
reducer: ee.Reducer.min(),
geometry: aoi,
scale: 90
});
var stdd = srtm.reduceRegion({
reducer: ee.Reducer.stdDev(),
geometry: aoi,
scale: 90
});
print('max:',max,'min:',min, 'mean: ',mean,'standard dev:',stdd);
These lines of code calculate the mean, median, max, min, and standard deviation of the elevation values in the AOI. The reduceRegion function takes four arguments. Let's look at the first one on max() to understand.
- The first argument is the image to be reduced. In this case, the image is the SRTM elevation data.
- The second argument is the reducer to be used. The reducer is a function that takes two arguments: a value and a reference to the current pixel. The reducer function must return a single value. In this case, the reducer is the
max()function which returns the maximum value in the neighborhood of the current pixel. - The third argument is the geometry of the AOI. In this case, the geometry is a polygon.
- The fourth argument is the scale of the reduction. The scale is the number of pixels that will be averaged together to calculate the value at each pixel. In this case, the scale is 90, meaning each pixel will represent an area of 90 x 90 pixels.
Trouble Shooting Note - MaxPixels: If you encounter an error about the maxpixels, it is likely because the reduceRegion function is trying to process more pixels than the default limit. You can increase the maxPixels parameter to a larger value to process more pixels.
Here's how you can modify your code:
Please adjust the 1e13 value according to your needs. Be aware that increasing this value will allow more pixels to be processed, but it may also increase the time it takes for the operation to complete.
4 - Print the Stats
To print the results, you can use the following code:
print('max:',max,'min:',min, 'mean: ',mean,'standard dev:',stdd);
You can look at the console after hitting run to see the results.
5 - Plot a histogram
Now, we will look at creating a histogram of the AOI. Look at the following code and add it to your project.
var histogram = ui.Chart.image.histogram({
image: srtm,
region: aoi,
scale: 90,
minBucketWidth: 10
});
histogram.setOptions({
title: 'Histogram of Elevation (meters)',
vAxis: {title: 'frequency'},
hAxis: {title: 'elevation in meters'},
legend: {position: 'none'},
colors: ['red'],
});
print(histogram);
This code creates a histogram of the elevation values in the AOI. A histogram is a graphical representation of the distribution of data. It is created by binning the data into a set of intervals and then counting the number of data points that fall into each interval. The intervals are called bins, and the height of each bar in the histogram represents the frequency of data points in that bin.
The ui.Chart.image.histogram function takes four arguments:
- The first argument is the image to be used to create the histogram. In this case, the image is the SRTM elevation data.
- The second argument is the geometry of the AOI. In this case, the geometry is a polygon.
- The third argument is the scale of the image. The scale is the number of pixels that will be averaged together to calculate the value at each pixel. In this case, the scale is 90, meaning each pixel will represent an area of 90 x 90 pixels.
- The fourth argument is the minimum width of a bin. The minimum width of a bin is the smallest number of pixels that can be used to create a bin. In this case, the minimum width of a bin is 10 pixels.
The setOptions function takes a set of options that control the appearance of the histogram. The options that are used in this code are:
- The
titleoption specifies the title of the histogram. In this case, the title is "Histogram of Elevation (meters)". - The
vAxisoption specifies the options for the vertical axis of the histogram. Thetitleoption specifies the title of the vertical axis. In this case, the title is "frequency". - The
hAxisoption specifies the options for the horizontal axis of the histogram. Thetitleoption specifies the title of the horizontal axis. In this case, the title is "elevation in meters". - The
legendoption specifies the options for the legend of the histogram. Thepositionoption specifies the position of the legend. In this case, the legend is not displayed. - The
colorsoption specifies the colors to fill the bars in the histogram. In this case, the only color used is red.
The print function prints the histogram to the console.
6 - Create a URL of your code and submit it to Canvas
Be sure that your comment you code and that it runs.
Lab Submission
Submit lab via email.
Subject: Lab 16 - Zonal Statistics - [Your Name]