Google Earth Engine (GEE) is a geospatial processing service that can process large remote sensing datasets in the cloud. The service has a JavaScript interface, allowing the use of built-in tools for remote sensing data processing.
> Go to https://earthengine.google.com/ from your browser (Google Chrome is recommended), sign up with a Google account, and apply for the Google Earth Engine access.
> Access https://code.earthengine.google.com/ or navigate to Platform > Code Editor
The interface of the GEE Code editor consists of 5 main panels (Search Bar, Scripts/ Docs/ Assets, Code Editor, Inspector/ Console/ Tasks, and Map Window); 2 pop up menus (Geometry Tools, Layers Panel); and the Run button to execute the code.
Please, remember the name of the panels, since it will be used throughout the practices.
To use the functions in GEE, you need to use a programming language called JavaScript. There are important concepts you have to know not only for Java Script but also in any programming language. Here you will learn the basic concept of programming.
Programming uses different data types: Numbers, Strings, Lists, and Booleans. Usually, you don’t need to worry about these data types in Microsoft Excel, but for programming, the data type needs to be defined. This rule also applies to the attribute table of a shapefile inside a database software.
Numbers: there are different types of numbers, but for now you can use any type (integers, doubles, float, etc.)
Strings: strings mean text. To define text, you’ll need to use quotes (single or double (‘’ or “ ”)) to define them (e.g. “hello world”, ‘Google Earth Engine, ‘JICA’)
Lists: a list is defined by brackets and it is separated by a comma ([a, b, c, …])
Booleans: Booleans can have two possible answers: true or false (e.g. do you want to show a map on the website? (true or false)
A variable is one of the most important concepts to understand programming. A variable can hold any type of data (number, strings, booleans, lists). In GEE a variable usually holds image, image collection, visual parameters, training samples, validation points, functions, etc.
> Go to the Code Editor in GEE and type var a = 1; print(a); at line 1 and line 3 respectively (just like the example below (don’t copy and paste))
> Click Run and you will be able to see the number 1 in the Console, which is the number saved inside the variable “a”
The program is executed in order (From top to bottom). First, line 1 is executed, then line 3. Line 2 is empty, so the program ignores and skips the line. You can use an empty line to make your code easier to read
> Modify the program like the figure and click Run
> you will be able to see the number 3 in the Console
> Modify the program like the figure and click Run
> You will be able to see the number 12 in the Console
> Next, to save the code, click in Save and change the name to “test”
Hint: Please think about the data type of a, b, and c. You could try the script from the figure below to understand the answer.
First, we need to reset the Code Editor
> Click on the arrow next to the Reset button > select Clear Script
> Go to the Code Editor and type the code below > click Run
In this practice, you have two variables (Red, Nir), you created the formula “Nir – Red” by using the subtract() function.
> Please type “subtract” in the search box under Docs. You will be able to see the target data type. The subtract function can be used for Array, Image, and Number types.
The program is executed from left to right. The dot is the connection to apply the objects and function. A target data type must be clear for a function.
> Go to the Code Editor and type the code below > click Run
> Go to Code Editor > add comments for each part using // (you can see it in green below) > click Run > Save > name: NDVI
Variable, data type, function, and comment
Based on a line number, in a line (if there is an empty line, how the part is executed?)
GEE has a huge library of datasets, including Landsat, Sentinel, MODIS, etc. To find the available dataset, please go to Earth Engine Data Catalog.
> Go to https://developers.google.com/earth-engine/datasets/ and check the available datasets (Landsat, Sentinel, MODIS, etc)
> Go to Landsat > Landsat 8 OLI/TIRS > Surface Reflectance and check the Earth Engine Snippet (this is the image collection ID to call inside the Code Editor)
> You can also check the details of the image collection on the tabs (spatial resolution, spectral resolution, temporal resolution, etc.)
> Check other image collections (e.g. Sentinel 2).
Reset the Code Editor.
>Click on the arrow next to the Reset button > select Clear script
To import a dataset into the Code Editor, you can type the desired dataset into the Search Bar (on the top)
> Go to the Search Bar > type Landsat 8 level 2 > click more > check the list and select USGS Landsat 8 Level 2, Collection 2, Tier 1 > click Import
The datasets in GEE are divided into Collections. A Collection contains all images from a particular satellite. For example, if we select USGS Landsat 8 Level 2, Collection 2, Tier 1 collection, we will find the atmospherically corrected surface reflectance data of the Landsat 8 satellite since 2013 (when the Landsat 8 satellite was launched), covering the whole Planet.
After importing it, you will see the following code in the import section of the Code Editor
So let’s understand what is written there:
The code means the variable imageCollection contains all the images of USGS Landsat 8 Level 2, Collection 2, Tier 1 since 2013 from the whole world.
We can change the variable name for easier identification. Change it to landsat8sr (This data is surface reflectance. So we use sr after landsat8)
> Click imageCollection > type landsat8sr
To visualize this collection in the Map Window (bottom pane)l:
> Go to the Code Editor > type Map.addLayer(landsat8sr);
> Zoom in to your target site (don’t forget to zoom in, otherwise will take a very long t time to load all the images) > click Run (wait around 3 minutes to load) > Save > name: data_selection > you will see a black map.
The function Map.addLayer() plots the data inside the parenthesis. In this case, you should be able to see the whole Landsat 8 Surface Reflectance images in your Map Window. It will take a long time to load since we are plotting the data of the whole world from 2013 into the map display! (you can also zoom to a specific area to view the results faster)
Check how many images are loaded.
> Click on the Inspector tab > click anywhere in the map > click Objects in the Inspector to expand the information > check the number of images loaded in the pointed area.
In this example, 423 images are loaded from 2013 to 2021. The images are all overlapped in order, the images from 2013 images are on top of the images from 2014.
As we saw before, the data inside these collections are massive and would take hours to load everything. For this reason, we can use filters. Through filters, we can select data from a specific area from a specific time and even specific bands, decreasing processing time by processing only the desired data.
Let’s start filtering our data! First, please create a variable called image, and inside this variable, we will input the filtered images from the image collection of landsat8sr.
To filter the data by date, we are going to use the .filterDate() function. You can set the start date and the end date to select the images inside this time range from the image collection
> Move the function Map.addLayer(landsat8sr) to line 4 by pushing the enter key several times
> Type the following code on lines 1 and 2
> At the Map.addLayer() function on line 4, change the variable name from landsat8sr to image > click Run
Change the color setting at the Layers Panel
> Select RGB: SR_B7, SR_B6, SR_B5 > select Stretch 98% > click Apply
After defining the image dates, let’s define our Area of Interest (AOI).
Let’s define a target site.
> Click on the rectangle icon on the Geometry Tools inside the Map Window > draw your AOI into the map display (Click on the upper left and drag to the lower right)
When you finish drawing, you will see a new variable called geometry on your code editor import area
Change the variable name to AOI
> Click geometry > type AOI
So now, we are going to filter the image collection according to the area using the function .filterBounds(). This function will select all images that intersect with the AOI polygon
> Remove the ; located at the end of line 2 > and type the following code on line 3 > click Run
You will be able to see only the images that intersects with the AOI
To see only the image, you can unselect the AOI in the Geometry Tools
To select the bands we want to use for our analysis, we need to create a list of desired bands. So, before the variable image, let’s create the variable bands containing the desired bands, in this case, bands 7, 6, 5, 4, 3, and 2. We need to use the prefix SR_ for this specific collection, it may change for other collections.
> Type the following code on line 1
You can find more information about bands inside the Earth Engine Data Catalog. We created the variable bands and insert a list of bands: SR_B7 (shortwave infrared 2), SR_B6 (shortwave infrared 1), SR_B5 (near-infrared), SR_B4 (red), SR_B3 (green), and SR_B2 (blue) to plot our data in our map display. Be aware that the order you create in your list will set the order of your final composite image.
After that, let’s select the bands of the images according to the variable bands by using the function .select()
Good, we selected bands SR_B7, SR_B6, SR_B5, SR_B4, SR_B3, and SR_B2 from the Landsat 8 collection.
Again, check the number of loaded images by the inspector.
> Click Inspector > click on a point in the image > click Objects
In this case, there are 11 images loaded and overlapped.
So far, we displayed the image collection in the Map Window, but to create a mosaic, we need to combine all these images into one single image. To do that, we need to reduce the image collection
Figure Without reducer (left), with reducer (right)
This time, we are going to use the median reducer, which means we get the median pixel values of all overlapped images to composite the final single image.
> Type .median(); after .select(bands) > click Run
So after reducing the whole collection into one composite image, we can clip and plot it into the map display using the .clip() function
>type .clip(AOI); after .median() > click Run
This time, we clipped the image with the same polygon we define to select the data area (AOI). You can create a different polygon if you want.
You can prepare an image for your analysis. In this case, mosaicking with cloud removal is included. Especially reducer is a very typical function of GEE.
In this practice you learned how to select and transform the data:
Please check the term and concept before you go
Variable, data type, function, comment
the direction of execution of a code (order)
filters
Image collection, reducer
Can you change the input image collection to sentine2 (Sentinel-2 MSI: MultiSpectral Instrument, Level-2A) based on the script?
var bands = ['SR_B7','SR_B6','SR_B5','SR_B4','SR_B3','SR_B2'];
var image = landsat8sr
.filterDate('2020-07-01', '2020-10-01')
.filterBounds(AOI)
.select(bands)
.median()
.clip(AOI);
Map.addLayer(image);