Lab 16 - Zonal Statistics

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:

  1. Import the SRTM data into Google Earth Engine (Done in Lab 15).
  2. Create a geometry object to define the area of interest (AOI).
  3. Calculate the mean, median, max, min, and standard deviation elevation values in the area of interest.
  4. 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:

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:

  1. In the Google Earth Engine Code Editor, open the Geometry Tools panel.
  2. Click on the rectangle tool.
  3. On the map, click and drag to create a rectangle that defines the area of interest.
  4. Click on the "Apply" button.
  5. Screenshot 2023-04-07 at 4.48.31 PM.png
  6. 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. 

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 setOptions function takes a set of options that control the appearance of the histogram. The options that are used in this code are:

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]