Module2 Intro Google Earth Engine Exercise
Module2 Intro Google Earth Engine Exercise
Introduction
Google Earth Engine is a cloud-based geospatial processing platform. Earth Engine is available through
Python and JavaScript Application Program Interfaces (APIs). The JavaScript API is accessible via a web-
based Integrated Development Environment (IDE) called the Code Editor. This platform is where users
can write and execute scripts to share and repeat geospatial analysis and processing workflows. The
Code Editor offers access to the full power of Earth Engine. In this exercise, you will learn about the
Code Editor platform and explore some basic programming concepts, in JavaScript. Some basic coding
and JavaScript knowledge is required to use the Earth Engine Code Editor.
2. Use the graphic above to guide you and click through the tabs in the upper left hand panel,
the Scripts and Documentation Panel.
i. Under the Scripts tab, note the wide variety of preloaded example scripts that
demonstrate capabilities and offer code that you can use for your analyses. You can take
a look at these to start to learn about what kinds of things Earth Engine can do. After you
create and save a script later in the day, it will be available here in your Private repository.
ii. Under the Docs tab, there is a searchable list of documentation for the predefined GEE
object types and methods. Note these are grouped and organized by type. Briefly explore
what kinds of methods (functions) are available in GEE for different object types.
(a) Select one of interest and click on it to see the information window with a
description of the methods and associated arguments (required and optional). Any
optional arguments are italicized. (The example scripts include examples of many of
these methods, try searching for them using the scripts search bar.)
3. Using the graphic above click through the tabs in the upper right hand panel where the
Inspector, Console, and Tasks tabs are located.
i. We will use the Inspector (similar to the identify tool in ArcMap) to easily get information
about layers in the map at specified points (specified by clicking in the Map Panel).
ii. The Console is used to return messages as the scripts run and print information about the
data, intermediate products and results. It also records any diagnostic messages, such as
information about runtime errors.
Remote Sensing for Forest Cover Change Detection 3
iii. The Tasks tab is used to manage the exporting of data and results.
4. Click on the Help button in the upper right and select Feature Tour to learn more about each
component of the API.
i. Click through the options in the Feature tour to become more familiar with each
component of the Code Editor.
Notes about JavaScript syntax: There’s a lot going on in just two lines. Take a closer look at the pieces of
this statement you’re loading into GEE (refer to the Appendices to learn more about JavaScript syntax
and some basic programming concepts).
1) Double forward slashes, //, are comment characters in JavaScript. These prevent text on that line from
executing. These are useful for creating notes in your code.
2. To zoom out, decrease the second input to a number less than 8. To zoom in more, increase
the second input parameter (try 10). Modify your statement so it looks like the two lines
below and click Run. What happened?
3. In the panel in the upper left, switch from the Scripts to the Docs tab. Type
Map.centerObject() into the Docs search bar. What is the range of the zoom parameter?
ii. Use the Layers tool (on the right of the map output panel) to turn the image (Layer 1) on
or off (shown in following image).
1. Open the documentation for addLayer function in the Map group by clicking on the Docs tab
in the left panel in the Code Editor.
i. Expand the Map group and select Map.addLayer from the list (as illustrated below); or
ii. Search for Map.addLayer in the Filter methods… search bar.
// Add the image to the map and name the layer in the map window.
Map.addLayer(lc8_image, undefined, 'Landsat8scene');
There are a number of options available to adjust how images are displayed. The inputs that are most
commonly used to modify display settings include the following:
Bands: allows the user to specify which bands to render as red, green, and blue.
Min and max: sets the stretch range of the colors. The range is dependent on the data type. For example
unsigned 16-bit imagery has a total range of 0 to 65,536. This option lets you set the display to a subset
of that range.
Palette: specifies the color palette used to display information. You will see how to use this option later
in the tutorial.
Naming convention (in the Layers Legend): you can specify the name that appears in the layers legend
here as well. You named this layer ‘Landsat8scene’ in the code above.
Syntax: Most of these optional parameters are entered as a key-value pair in a dictionary object. The
syntax is:
{vis_param1: number, number, number
vis_param2: 'string, string, string',
// or an array of strings like this:
1. Modify the Map.addLayer() function to display the image as a false color composite and apply
a stretch to improve the display. Modify the Map.addLayer() statement from the previous
steps to look like the code below. The statement below includes the optional parameters for
which bands to display (bands 6, 5, and 4), specifies a stretch to improve the visualization, and
finally gives the image a display name.
2. Click Run and use the map tools to explore the result. Note that the name under the Layers
(legend) is now Landsat8scene.
Note: In the statement above, the names of the bands have been inserted for you already. If you wanted
to look them up yourself, you can use the print function (or the Inspector) to identify what the bands are
named (e.g., B6, B5, B4).
3. You can also specify the bands as strings in a list. Look at the statement below, it will do the
same thing as the statement above. Do you notice the difference in syntax?
4. Copy and paste the following statement in your code editor. Then click Run.
6. Click the Save button in the upper right of the Code Editor panel to save your example script
for future reference.
i. Name this script Visualize a Landsat 8 image.
Note: there are many more options for visualizing data in the map window, such as setting a mask or
mosaicking two data sets together.
What is a script?
A script is a series of instructions that a computer follows to execute some process. It is like a recipe – a
set of instructions that a cook follows one by one to create a dish. In the Code Editor platform, the
instructions are written as JavaScript statements. The computer uses these statements to tell Earth
Engine what information you are requesting.
5. Read the Normalized Difference script, line by line (or statement by statement), to see what it
is doing:
i. Lines 1 to 6 are notes, or comments, the developer included to describe the script. Line
comments are designated with the //, the double slashes at the beginning of the line.
Comments are ignored by the Code Editor when the script executes.
ii. Line 8 accomplishes two things. It declares a variable, called img. It then assigns a value to
this variable. The value is a MODIS image
ee.Image('MOD09GA/MOD09GA_005_2012_03_09').
iii. Line 9 does several things. It declares and assigns a value to a variable, called ndvi. It also
calls the Earth Engine NormalizedDifference method and applies it to the variable “img”
defined in the previous line. The bands “sur_refl_b02” and “sur_refl_b01” are specified as
inputs to that calculation (arguments to the method). These two bands are the NIR and
iv. Lines 10-12 declare a variable, palette, and assign to it an array that specifies a palette of
hexadecimal color codes for displaying the resulting NDVI image. The hexadecimal colors
range from white (FFFFFF) to browns (e.g., CE7E45) to yellows (e.g., FCD163) to greens
(e.g., 529400) to very dark (011301).
Note: You can read more about hexadecimal color codes here http://www.colorhexa.com/.
v. Line 14 centers the map to the area of interest. The arguments, the values inside the
brackets, are the longitude and latitude values for Kansas City, USA; the third value sets
the zoom level.
vi. Lines 15-17 add data to the map output window (lower panel). Two images are displayed
- the img variable, which points to the original MODIS image, and the ndvi variable, which
points to the normalized difference image created in line 9.
6. Click the Run button in the upper-right of the code editor to run the Normalized Difference
script.
8. Use the Inspector Panel to explore the values in the resulting NDVI image.
i. Click on the Inspector tab in the upper right-hand panel.
(a) Hover your cursor over the map. Notice that your cursor has become a cross.
ii. Click anywhere on the map and observe the values that appear in the window under the
Inspector tab.
(a) These are the pixel values at this location for:
(i) MODIS band values for the displayed bands appear under the MODIS image
name.
(ii) The computed NDVI values.
Introduction
The Code Editor offers access to the full power of Earth Engine; however, a basic understanding of the
fundamentals of coding and JavaScript is required. In this exercise, you will learn about JavaScript syntax
and several key Earth Engine spatial data concepts. The focus of this exercise is on properties and
methods, or functions, associated with single raster images in Earth Engine. However, you will also get a
brief introduction to other types of Earth Engine spatial objects. This exercise will get you writing a
simple JavaScript script. You will also learn about Fusion Tables.
// Create an NDVI image using bands the NIR and red bands (5 and 4).
var NDVI = lc8_image.normalizedDifference(['B5', 'B4']);
// Display the NDVI image with a grayscale stretch.
B. Mask clouds
1. Clear your script from Part 2 A, except for the lines below.
2. Run the script. Notice the clouds that are present on the eastern half of the image? Next you
will build a process to remove these from the imagery.
3. First, you will create a variable, cloud_thresh. This will store the cloud likelihood
threshold value. After you have drafted the script, you can easily change the value of this
variable. Change the value and re-run the script to investigate what an appropriate cloud
thresholding value is for the study region.
Next you will use an Earth Engine algorithm that will calculate a simple cloud-likelihood score using a
combination of brightness, temperature, and the Normalized Snow Index (NDSI). This likelihood is
represented on a scale of 0 to 100, where larger values indicate a greater likelihood of a pixel being
clouded. You can read more about it in the Docs tab (or refer to image below).
5. (Optional) Add the cloud layer to the map by copying the lines below and adding them to the
bottom of your script. Click Run.
i. What values do the cloudy areas get assigned? (hint: use the inspector to look up values at
different locations in the map)
ii. Do you notice the difference between the results of the two Map.addLayer()
statements? (hint: use the inspector tab and turn the layers on and off to compare)
iii. After you are done inspecting the cloudscore raster, remove (or comment) these
lines from your script.
1. Copy the lines below and paste them at the bottom of your script. This code uses the lt() method to
create a binary raster that assigns each pixel a value:
i. One (1) if the cloud likelihood, quality variable, is less than the cloud threshold value;
ii. Zero (0) if the cloud likelihood, quality variable, is greater than the cloud threshold value.
2. Run the code and inspect the values at different locations (hint: use the Inspector tab).
3. Copy the lines below and paste them at the bottom of your script.
i. You use the updateMask() method to remove the values that have a high cloud likelihood
value from the Landsat image. The updateMask() method removes pixels from the
Landsat image where the input image, cloudPixels, has a value of zero.
ii. Run the code and inspect the output.
Note: these examples are from the Earth Engine Documentation, available here:
https://developers.google.com/earth-engine/image_info
// Get an image.
var lc8_image = ee.Image('LANDSAT/LC8_L1T_TOA/LC81290502015036LGN00');
// Note that different bands can have different projections and scale.
var b8scale = lc8_image.select('B8').projection().nominalScale();
print('Band 8 scale: ', b8scale);
There are also Earth Engine Objects. These are objects that have meaning in Earth Engine, but not
general JavaScript applications. Examples of Earth Engine objects include images (e.g., a Landsat scene)
and image collections (a collection of Landsat scenes). Here, the focus is on Earth Engine objects and
their associated geoprocessing methods. However, it is important to distinguish between Earth Engine
objects and JavaScript objects so that you don’t mistakenly apply methods for one type of object to the
other.
Methods: functions that are specific to object types are called methods. They can retrieve data, sort
data, update values of an object’s properties, etc. For example, earlier you used the
NormalizedDifference which is an image-object method.
Image: raster data are represented as images objects in Earth Engine. An image object represents a
single raster image, such as a single Landsat scene collected on a given day, a Landsat median
composite, or a topographic dataset (DEM). Images are composed of zero or more bands, where each
band has a name, data type, pixel resolution and projection. Each image also has metadata stored as a
dictionary of properties. Read more here - https://developers.google.com/earth-engine/image_info
Image collection: a set of images. For example the Landsat 8 TOA Reflectance collection
(LANDSAT/LC8_L1T_TOA) includes all of the imagery Landsat 8 has collected since April 11, 2013,
orthorectified and corrected to Top of Atmosphere reflectance. Collections are useful for temporal
analysis, or creating cloud free composites that include imagery from several acquisitions.
You can create an object with a geometry in Earth Engine, a GeoJSON Feature, or a collection of
features. Shapefiles can be converted to Fusion Tables, then accessed in Earth Engine as a
FeatureCollection.
Introduction
The Code Editor offers access to the full power of Earth Engine; however, a basic understanding of the
fundamentals of coding and JavaScript is required. In this exercise, you will continue to learn about
JavaScript syntax and some new Earth Engine spatial data concepts. You will build on what you learned
in exercise two about image objects; however you will now turn your focus to working with collections
of images, or stacks of similar image objects. In this exercise, you will focus on basic concepts and
methods associated with image collections in Earth Engine.
Objectives
Practice writing and running code in the Earth Engine Code Editor to access raster image
collections, filter them temporally (by a date range) and spatially (e.g., by your study region),
work with geometries, and explore some image processing functions
In the Code Editor, you can work with the entire collection of images, or you can use filters to create
subsets of the ImageCollection (e.g., representing a specific study area or for a specified time period).
Read more here - https://developers.google.com/earth-engine/ic_info
2. To add this collection to the map, copy the code below and paste it into the code editor. Then
click Run to execute all the lines.
i. What is mapped here? The image collection has a number of images, but not all are
mapped. When you use the Map.addLayer function to add an image collection to the map
window, by default the most recent pixel is shown.
3. (Optional) If you add a print statement, you can determine how many images are in the
collection. Copy and paste the following lines into the bottom of your script.
i. However, since the image collection is rather large, an error message is returned in the
Console. The print Collection query aborted after accumulating the metadata for over
5,000 elements. Next you will filter the collection down by space and time parameters
and learn a better way to get a count of the number of images in the image collection.
ii. Comment out the print statement. You will return to this later.
// print(landsat8_collection);
2. This will allow you to draw a geometry that represents your study area of interest. Click in the
map display window to create a polygon around an area of interest to you (e.g., around
Bangkok or your home town). Remember you can toggle the Landsat image on and off to see
the base layer below. Double click to close the polygon.
3. After you have closed your polygon, there will be an Import record at the top of the code
editor panel. Refer to the red arrow and box in the image below.
4. Click on the word geometry. Change the name from geometry to studyArea.
5. Click on the arrow next to the var studyArea line to see the details of the geometry you
created (see preceding image for an example).
3. Click Run again. Now the image collection will be filtered to only include images that intersect
the polygon that you drew.
4. (Optional) modify the print statement to print the details of your new image collection,
landsat8_studyArea (make sure to move this statement below the creation of the
landsat8_studyArea variable). If your image collection has fewer than 5000 images,
information about the collection is printed to the console. The size of your image collection
varies depending on the size of the geometry you’ve digitized.
4. (Optional) Below are some additional methods you can use to explore your image collection.
These are taken from the following page in the user documentation:
https://developers.google.com/earth-engine/ic_info
Introduction to Reducers
A. Image Collection Reducers, median pixel value
This section focuses on an important type of object in Earth Engine, reducers. Reducers work on
image collections by calculating statistics, such as the mean value for each pixel. The output is an
Image object (single raster layer) that characterizes some quality of the complete image collection.
To learn more about reducers, visit the User Guide: https://developers.google.com/earth-
engine/reducers_image_collection.
1. First, simplify the script that you are working with. Modify the script in your Code Editor to
match the lines below (or start with a clear script window and copy the lines below into the
code editor – just make sure to redraw your study region). Then Run the script.
// Display the result and center the map on the study region.
Map.addLayer(median_landsat8_2015,
{ min: 0.05, max: 0.8, bands: 'B6, B5, B4'});
Map.centerObject(studyArea, 7);
4. Save your script, name it ‘Ex 3 Image Collection Part 8’. You will return to it later in the
exercise.
Functions Primer
As you develop your script, it can start getting quite long. To keep it organized and create more efficient
code, you can re-use pieces you’ve built by taking advantage of functions. Functions break apart your
code into separate pieces, wrap these pieces up, and give them a name that you can call to apply later
when needed. Functions are essentially modular pieces of re-useable code. For example, you might have
a function to get the data set(s) of interest, one to analyze them, and finally another to export them.
You can break the code up into these three parts – each wrapped up as a function.
Functions are also highly useful when you have a series of statements that you would like to repeat
many times in the code. For example, suppose you wanted to calculate the mean of the data or the
normalized difference vegetation index (NDVI) of a series of rasters. You could create a function that you
could call each time you wanted to execute either of these in your code, rather than have to re-write
those pieces each time.
A. Structure
Here’s how functions are built:
This is the basic structure of a function. If it sounds complicated, don’t worry, it will all make sense when
you write one yourself. Below you’re going to practice by making a simple calculate_sum function.
Note: Common convention declares and specifies (all) the functions first, then calls them later in the
script. It’s not required, but it makes for code that is easier to read.
1. Replace the lc8_image with the filtered image collection into the script. See full example
below.
// Store the Landsat 8 image collection in a variable.
var landsat8_collection = ee.ImageCollection('LANDSAT/LC8_L1T_TOA');
// Add the first masked image in the collection to the map window.
Map.addLayer(ee.Image(landsat8_SA_2015NoClouds.first()),
{min:0.05, max: 0.8, bands: 'B6, B5, B4'},
'first image with clouds masked');
In the previous exercises, you worked with data and generated new output entirely through the GEE
Code Editor. A major advantage of cloud computing is the ability to accomplish complex tasks with the
computational load and data storage shifted to the cloud; however, for many applications you will want
to export and save your results at some point. In this exercise, you will learn how to use the Code Editor
to export results that you can save, share, and use in your GIS analyses on your desktop.
Objectives
Learn how to export results and images,
Export data from a complete image processing script to get useful data.
1. Copy and paste the following statements into an empty code editor panel. Click Run to
explore the data set.
// Store the canopy height image as a variable, canopyHeight.
var canopyHeight = ee.Image("NASA/JPL/global_forest_canopy_height_2005");
B. Exporting Data
Exporting data from the Code Editor is possible through the export functions, which include export
options for images, tables, and videos. You will focus on Export.image.toDrive() to download your
imagery data sets. You can also export your images as an asset or to Google Cloud storage.
Export methods take several optional arguments so that you can control important characteristics of
your output data, such as the resolution and projection.
Note – In this example, you have specified a few of the optional arguments recognized by
Export.image(). Though this function takes several optional parameters, it is valuable to familiarize
yourself with these:
1. Run the script. After a moment, the Tasks tab in the upper right of the Code Editor should be
highlighted.
2. Click on the Tasks tab. Then click the blue Run button (shown below) to export the data to
your Google Drive.
3. Review the information in the Initiate export window that appears (below). Then click Run to
begin.
Note: This task will be exported to your Google Drive under your Google Account. This will be a 1000-
meter resolution GeoTiff image with the root name MyFirstExport.
4. After you click Run at the bottom of the Initiate export window to start the export, your
screen will show the export is processing. The task under the Code Editor Tasks tab should
now have a spinning GEE icon next to it. It can take some time to export the task. When it is
completed, this icon will disappear and the task name will turn blue. Look to see if yours
turned blue.
Note: If you try exporting Landsat scene without first sub-setting the image to get just the bands of
interest, you will get an error message.
Make sure that after you have placed them in the Google Drive trash that you also go in and empty your
trash to truly free up that space.
Introduction
In the previous exercises you briefly explored the generic NDVI function and ran a relatively simple script
to generate a cloud free composite. The following exercise will walk you through a customized script
that will create a cloud free composite that utilizes imagery from three Landsat sensors (5, 7, and 8) and
incorporates a sophisticated cloud and shadow masking algorithm. The provided script is relatively
complex, preventing an in depth exploration of the script given the limited time. The script enables you
to create a composite that can be used in subsequent sections of this change detection training. You can
refer to this script to download data specific to your project study region(s).
You can read more about the cloud and shadow masking process we use here:
http://www.leafasia.org/sites/default/files/public/resources/USAIDLEAF-RSAC-ForestLossReport-
November2015-Revised.pdf
Objectives
Create cloud-free Landsat images for two time periods within a specific study area (Krabi,
Thailand).
Download those study area composites for use in later exercises.
Create a cloud free composite for an entire country (in this case the Philippines).
/////////////////////////////////////////////////////
// User Editable Variables
/////////////////////////////////////////////////////
// Composite area
// Create a Feature Collection for the area of interest.
var country_name = ['Thailand'];
Remote Sensing for Forest Cover Change Detection 43
var countries =
ee.FeatureCollection('ft:1tdSwUL7MVpOauSgRzqVTOwdfy17KDbw-1d9omPw');
var country = countries.filter(ee.Filter.inList('Country',
country_name));
////////////////////////////////////////
// Composite time period
// startYear: First year to include imagery from
var startYear = 2000;
////////////////////////////////////////
// Composite parameters
// cloudThresh: If using the cloudScoreTDOMShift method-Threshold for cloud
// masking (lower number masks more clouds. Between 10 and 30 generally
// works best)
var cloudThresh = 20;
// zScoreThresh: Threshold for cloud shadow masking- lower number masks out
// less. Between -0.8 and -1.2 generally works well
var zScoreThresh = -0.8;
////////////////////////////////////////
// Export parameters
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
// End of user editable parameters. Do not edit anything below this
line.
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
C. Composite parameters
1. cloudThresh (line 47): controls the sensitivity of the cloud masking algorithm. Lower numbers
increase masking, higher numbers decrease masking.
2. dilatePixels (line 51): controls the number of pixels used to buffer clouds and cloud shadows.
Default value should work well for most areas.
3. cloudHeights (line 54): the height of clouds to use to project cloud shadows. Default value
should work well for most areas.
4. zScoreThresh (line 58): the threshold for cloud shadow masking, a lower number will mask
out fewer pixels. Default value should work well for most areas.
5. cloudHeights (line 62): the sum of infrared bands to include as shadows within the dark object
method, TDOM, and the shadow shift method. A lower number will mask out fewer pixels.
Default value should work well for most areas.
D. Export parameters
1. exportName (line 68): specifies the prefix both for exported imagery and labeling in the
viewer
Remote Sensing for Forest Cover Change Detection 46
2. crs (line 72): specifies the coordinate reference system as an EPSG number for the exported
imagery. Refer to this online resource for more information on how to look up the EPSG code
for your project. http://spatialreference.org/ref/epsg/
Export imagery
A. Run the Tasks to export the images to Google Drive
1. Run the script to be sure all your changes have been incorporated.
i. After the script runs, select the Tasks tab.
ii. Click RUN to initiate an export of the composite you just created.
2. Review the information in the Initiate export window that appears (below) for each
composite (note, the name below is for an island in the Philippines).
4. Once the export is complete (~10 minutes) the spinning icons will be replaced with a check
mark indicating the export is complete. This indicates that your composites have been
exported to your Google Drive.
Note – The above example highlights a task that will be exported to your Google Drive under your
Google Account. Each download will be a 30-meter resolution GeoTiff image. The composite for the
whole country of Thailand is approximately 1728 MB, this subset was 933 MB. Before you export a lot of
data, make sure you have room on your Google Drive! If necessary delete and empty the trash to make
more room.