GEE Ex 1 CE Platform Final
GEE Ex 1 CE Platform Final
GEE Ex 1 CE Platform Final
Introduction
Google Earth Engine is available via a web-based JavaScript Application Program Interface (API) 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, we will begin learning about the Code Editor platform and explore some basic
programming concepts, in JavaScript. Learning of fundamentals of coding and JavaScript is required to
use Earth Engine.
Table of Contents
Part 1: Introduction to the Code Editor API ..................................................................................... 2
Part 2: Working with Images............................................................................................................ 7
Part 3: Explore Data Available in Earth Engine .............................................................................. 14
RSAC | Exercise 1: Hello World and Exploring Data Archive | April 2016 1
Part 1: Introduction to the Code Editor API
In this exercise, we will use the Google Earth Engine Application Program Interface (API), or Code Editor.
This interface offers significantly more flexibility than Explorer and the ability to create complex and
customized analysis workflows. In the Code Editor we will write JavaScript code to access and analyze
data and download results.
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 object. Briefly
explore what kinds of methods (functions) are available in GEE.
(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 listed in italicized print. (The example scripts include
3. Select the Normalized Difference script from the list of example scripts (stored within the
Image grouping). It will copy the script into your Code Editor panel.
4. The graphic below shows the script that should appear in the Code Editor panel (upper center
panel).
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 creator 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/arguments to that calculation. These two bands are the NIR and Red MODIS bands,
so the result of the calculation is a Normalized Difference Vegetation Index (NDVI) image.
This calculated NDVI image is what is being assigned to the ndvi variable.
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 parameters, the values inside the
brackets, are the longitude and latitude values for Kansas City, USA; the third value sets
the zoom level.
i. You should see a MODIS image and the resulting NDVI image appear in the map output
window at the bottom of your screen.
7. Visually examine the results in the map output window using the map viewer tools.
i. Click or mouse-hover on the Layers button in the upper right hand corner of the Map
output panel at the bottom of your screen (shown in the following graphic).
ii. Toggle the NDVI layer on and off by unchecking and checking the box next to the NDVI
Layer.
iii. Click and drag the slider-bar back and forth to adjust the transparency of the NDVI layer
in order to view the MODIS image beneath the NDVI image (see following image, with
visualization parameter box).
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) The individual MODIS band values for the displayed bands appear under the
MODIS image name.
(ii) The computed NDVI values.
Notes about JavaScript syntax: There’s a lot going on in just two lines. Take a closer look at the pieces of
this statement we’re loading into GEE (note, 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) Variables are declared in JavaScript using the keyword var. Variables can be numbers, strings, objects,
object collections, etc. Variables are used to store information for use later on in the script. In the case of
the statement above, you are naming the variable LC8_image and using it to refer to the raster dataset
you are interested in analyzing.
3) ee.Image() is a GEE function that tells GEE that you want to load an image as an object (and in this
case, save it as a variable called ‘LC8_image’). In JavaScript, functions start with a letter and have a pair
of parentheses at the end. Functions often include inputs or parameters, which tell the function what to
do, and are specified inside of the parentheses. In this case, the parameter you are specifying inside the
parenthesis is the image ID.
A generalized version of the statement above is: ee.Image(‘image_id’). The ‘image_id’ is the image that
you would like to load ('LANDSAT/LC8_L1T_TOA/LC81290502015036LGN00') and set as the variable
(LC8_image).
4) The syntax for specifying the image ID in this function (ee.Image) is to surround the string of
characters (the image ID, 'LANDSAT/LC8_L1T_TOA/LC81290502015036LGN00') in quotes. The image id is
in quotes because the collection and image name together is a string. Strings are sets of characters that
in this example, name the specific dataset.
a. Landsat image ids for individual Landsat scenes can be found at glovis.usgs.gov. You will work
with this more in Exercise 3.
5) JavaScript statements end with a semicolon.
1. Copy and paste the two lines of code (below) underneath the four lines you already have in
the GEE code editor window. Click Run.
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?
iii. Swipe the transparency lever (the sliding bar to the right of the layer name in the
preceding image). This will make the ‘Layer 1’ transparent, revealing the base map
underneath.
2. Review the documentation that appears (shown above). This provides information about the
use and arguments for this function.
i. Notice that some input options (such as vis) are italicized in the documentation. This
means that these are optional parameters that can be specified or left out of the
Map.addLayer statement. If you want to skip an optional parameter use “undefined” as a
place holder. See the statement below for an example.
Map.addLayer(LC8_image,undefined, 'Landsat8scene');
Note: What we are most interested in here are the optional Feature Visualization Parameters, vis. In
this case the documentation is somewhat sparse. You can learn a bit more by opening up the
documentation for the ee.data.getMapId method. Hint, find the documentation for this method by
expanding the ee.data group. You can also search the example scripts to see working examples.
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 (band 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 an array. Look at the statement below, it will do
the same thing as the statement above. Do you notice the difference in syntax, it’s rather
subtle?
4. Copy and paste the following statement in your code editor. Then click Run.
print(LC8_image);
5. Now in the Console tab, click on the arrow next to Image LANDSAT/… to display the image
properties. Then click on the arrow next to bands: to display the band properties. This will
reveal the first band (indexed at 0) is called “B1”, the second (indexed at 1) is called “B2”, etc.
Refer to following graphic for an example (note: the numbers in the image will not align
perfectly, since this picture represents a scene from the USA).
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. To read more about these advanced visualization options, visit the
Google Earth Engine Documentation at https://developers.google.com/earth-
engine/image_visualization#styled-layer-descriptors