Exercise 3
DATA PROCESSING
IN GEE
Outline of Exercise_______________________________________________
3.1 Introduction 3.5 Some Commonly Used Data and
Codes
Expected Learning Skills
3.6 Exercises: To Be Submitted
3.2 Requirements
3.7 Exercises: Explore Yourself
3.3 Writing Codes for Data
Preprocessing in GEE 3.8 References
3.4 Case Study: NDVI Analysis
Contributed by Dr. Rohit Kumar, New Delhi and Dr. Neeti, Azim Premji University,
Bangaluru 1
3.1 INTRODUCTION
You have learnt that Google Earth Engine (GEE) is a cloud-based geospatial analysis
platform that enables users to process and analyse vast amounts of satellite imagery and
geospatial datasets efficiently. GEE integrates a robust catalog of satellite imagery, climate
data, and other geospatial layers with powerful computing resources, allowing researchers
and analysts to derive meaningful insights at regional, national, or global scales. One of
GEE's key strengths lies in its ability to handle complex geospatial computations without
requiring users to download large datasets. This capability makes it particularly useful for
applications such as environmental monitoring, land use and land cover classification,
agricultural health monitoring, and disaster management.
In the previous exercise, you have created your login and become familiar with GEE data
catalog and code editor.
In this exercise, you will learn to import a data, visualise the same in QGIS, write codes to
preprocess images, and export to your system.
Expected Learning Skills______________________________
After successfully completing this exercise, you should be able to:
Write code for data preprocessing in Google Earth Engine;
perform NDVI analysis; and
write codes and perform for various processing in GEE.
3.2 REQUIREMENTS
Software: You will be using GEE in this exercise. You will also need QGIS to visualise the
imported data.
Data: The data used in this exercise is to be explored within GEE.
You will require internet connection for working with GEE.
3.3 WRITING CODES FOR DATA PREPROCESSING IN GEE
In this section, you will learn to write code for image preprocessing tasks such as image
enhancement and also including data visualisation, importing and exporting.
You know that the GEE platform provides a JavaScript and Python API, enabling users to
write custom scripts for diverse geospatial tasks. Typical data processing in GEE includes
filtering and visualising satellite imagery, performing radiometric corrections, deriving
spectral indices, conducting temporal and spatial analyses, and exporting results in user-
defined formats. With its extensive documentation and active community support, GEE is
an invaluable tool for professionals and researchers working in the geospatial domain.
This guide provides a comprehensive workflow for data processing in GEE, starting with
importing data, preprocessing, and analysis, followed by visualisation and exporting
Contributed by Dr. Rohit Kumar, New Delhi and Dr. Neeti, Azim Premji University, Bangaluru
2
results. Whether you are monitoring vegetation health, analysing soil erosion, or studying
urban growth, GEE offers a scalable and efficient solution to address geospatial
challenges.. Google Earth Engine (GEE) is a powerful cloud-based platform for geospatial
analysis.
Here is an outline of data processing steps typically involved in a GEE workflow:
1. Importing and Preprocessing Data
Datasets: Import datasets from GEE's catalog (e.g., Sentinel-2, Landsat, MODIS) or
upload custom data.
Region of Interest (ROI): Define your study area using geometries or shapefiles.
Filtering: Apply filters based on date, location, and cloud cover.
Code: // Define the region of interest (ROI)
var roi = ee.Geometry.Polygon([
[[77.0, 34.0], [77.5, 34.0], [77.5, 34.5], [77.0, 34.5], [77.0, 34.0]]
]);
// Load and filter Sentinel-2 data
var sentinel2 = ee.ImageCollection('COPERNICUS/S2')
.filterBounds(roi)
.filterDate('2023-01-01', '2023-12-31')
.filter(ee.Filter.lt('CLOUDY_PIXEL_PERCENTAGE', 20));
2. Data Visualisation
Add layers to the map for visual interpretation.
Adjust visualisation parameters such as bands, min/max values, and color palettes.
Code: // Visualization parameters
var visParams = {
bands: ['B4', 'B3', 'B2'], // RGB bands
min: 0,
max: 3000,
gamma: 1.4
Contributed by Dr. Rohit Kumar, New Delhi and Dr. Neeti, Azim Premji University, Bangaluru
3
};
// Display the image on the map
Map.centerObject(roi, 10);
Map.addLayer(sentinel2.median(), visParams, 'Sentinel-2 RGB');
3. Image Enhancement
Cloud Masking: Remove cloud pixels using algorithms like QA60 or custom masks.
Indices Calculation: Derive vegetation indices (e.g., NDVI, EVI) or water indices.
Code: // Cloud masking function
function maskClouds(image) {
var cloudBitMask = ee.Number(2).pow(10).int();
var cirrusBitMask = ee.Number(2).pow(11).int();
var qa = image.select('QA60');
var mask = qa.bitwiseAnd(cloudBitMask).eq(0)
.and(qa.bitwiseAnd(cirrusBitMask).eq(0));
return image.updateMask(mask).divide(10000);
// Apply cloud masking
var maskedCollection = sentinel2.map(maskClouds);
// NDVI calculation
var addNDVI = function(image) {
var ndvi = image.normalizedDifference(['B8', 'B4']).rename('NDVI');
return image.addBands(ndvi);
};
// Apply NDVI calculation
var ndviCollection = maskedCollection.map(addNDVI);
// Display NDVI
Contributed by Dr. Rohit Kumar, New Delhi and Dr. Neeti, Azim Premji University, Bangaluru
4
Map.addLayer(ndviCollection.median().select('NDVI'), {min: 0, max: 1, palette: ['blue',
'white', 'green']}, 'NDVI');
4. Exporting Data
Export processed data to Google Drive, Earth Engine Assets, or Cloud Storage.
Code: // Export NDVI image to Google Drive
Export.image.toDrive({
image: ndviCollection.median().select('NDVI'),
description: 'NDVI_Ladakh_2023',
scale: 30,
region: roi,
fileFormat: 'GeoTIFF'
});
3.4 CASE STUDY: NDVI ANALYSIS
For better understanding of the GEE and writing of codes, let us perform a case study
related to NDVI analysis.
Perform the following steps for the same:
A. Search satellite data (In this case you are using Sentinel 2 data)
Now click on import
Contributed by Dr. Rohit Kumar, New Delhi and Dr. Neeti, Azim Premji University, Bangaluru
5
It will appear like this in the script interface, You can rename it (ex S2)
The short name will help user to write code easily
Contributed by Dr. Rohit Kumar, New Delhi and Dr. Neeti, Azim Premji University, Bangaluru
6
B. Now import shapefile (vector file)
Go to Assets, click on New, then table upload option will be visible where user can upload
shapefile. In this case we are importing shapefile of Morena district. You can use shape file
of any area that you may have.
Click on shapefile and locate the folder where you have saved your AOI/study area
shapefile and select .shp, ,shx, .dbf, .prj.
Then Upload
Contributed by Dr. Rohit Kumar, New Delhi and Dr. Neeti, Azim Premji University, Bangaluru
7
It will take 1-2 minutes to upload shapefile, You can see it progress by clicking on Task.
Task interface will show you that your shapefile is uploading with time.
After uploading it will appear in Assets, if it is not appearing then click refresh icon (just
below Docs).
Contributed by Dr. Rohit Kumar, New Delhi and Dr. Neeti, Azim Premji University, Bangaluru
8
Click On Morena, then click on import
Finally, it will appear on the script interface, where the user can choose to rename it if
desired. In this example, we are renaming it as Ch1.
Contributed by Dr. Rohit Kumar, New Delhi and Dr. Neeti, Azim Premji University, Bangaluru
9
C. Now filter the data by date, cloud, clip and visualise the data in different
interface
Code:
Map.addLayer(ch1);
Map.centerObject(ch1);
var fc = s2.filterBounds(ch1).filterDate('2024-04-01','2024-05-
15').filterMetadata('CLOUDY_PIXEL_PERCENTAGE','less_than',20);
print(fc);
var img = ee.Image(fc.mosaic().clip(ch1));
Map.addLayer(img);
Contributed by Dr. Rohit Kumar, New Delhi and Dr. Neeti, Azim Premji University, Bangaluru
10
D. Now write a code for NDVI
In this code, we employ a straightforward technique to calculate the Normalised Difference
Vegetation Index (NDVI), a widely used indicator for assessing vegetation health and
density. NDVI is computed using the formula:
NDVI = (NIR - RED) / (NIR + RED)
Here, A and B represent the Near-Infrared (NIR) and Red bands of the satellite imagery,
respectively. For Sentinel-2 satellite data, the B8 band corresponds to the NIR band, while
the B4 band represents the Red band. By applying this formula, we can easily derive NDVI
values, which range from -1 to +1.
Higher NDVI values (closer to +1): Indicate healthy and dense vegetation.
Lower NDVI values (closer to -1): Suggest bare soil, built-up areas, or water bodies.
Code:
// Use the normalizedDifference(A, B) to compute (A - B) / (A + B)
var ndvi = img.normalizedDifference(["B8", "B4"]);
print (ndvi)
Map.addLayer(ndvi);
Note: After writing code, press on RUN in script interface.
E. Visualisation in Colour.
User can also visualise the NDVI in colour ramp
Contributed by Dr. Rohit Kumar, New Delhi and Dr. Neeti, Azim Premji University, Bangaluru
11
Code:
Map.addLayer((ndvi),{palette:"a3a3a3,e9ff34,53db46,6699CC, #0000A0, #FF0000"});
F. Now Add Histogram and Chart of the NDVI
This code will analysis and prepare chart and histogram.
Code:
//histogram
var image = ee.Image('ndvi')
.select('B[1]');
var options = {
title: 'histogram, bands 1',
fontSize: 20,
hAxis: {title: 'DN'},
vAxis: {title: 'count of DN'},
series: {
1: {color: 'blue'}}};
// Make the histogram, set the options.
Contributed by Dr. Rohit Kumar, New Delhi and Dr. Neeti, Azim Premji University, Bangaluru
12
var histogram = ui.Chart.image.histogram(ndvi,ch1, 30)
//.setSeriesNames(['blue', 'green', 'red', 'NIR'])
//.setOptions(options);
// Display the histogram.
print(histogram);
Click on Chart or icon over chart in console interface and user can visualise or download
the histogram or chart.
G. Export the Result
Now, you can easily download the NDVI you have calculated.
Contributed by Dr. Rohit Kumar, New Delhi and Dr. Neeti, Azim Premji University, Bangaluru
13
The following code enables the export of NDVI data directly to Google Drive, allowing for
convenient access and further analysis. Once the data is saved in your Google Drive, you
can download it as a GeoTIFF file and use it for detailed studies, visualisation, or
integration with other geospatial tools. This streamlined process ensures that your NDVI
data is ready for offline analysis and interpretation.
Code:
// Export the image, specifying scale and region.
Export.image.toDrive({
image: ndvi.float(),
description: '2019',
scale: 30,
region: ch1,
folder: 'GEE',
});
Remember that the download option will appear in the Tasks interface. Simply click on
RUN, rename the file as desired, and adjust the resolution to suit your requirements. Once
the parameters are set, click RUN again to initiate the export process. This ensures that
your data is customised and ready for download based on your preferences.
Contributed by Dr. Rohit Kumar, New Delhi and Dr. Neeti, Azim Premji University, Bangaluru
14
H. Visualisation In QGIS
After downloading, follow these steps to open and visualise the NDVI GeoTIFF file in
QGIS:
Open QGIS: Launch the QGIS application on your system.
Load the NDVI File:
Go to the top menu and click on Layer > Add Layer > Add Raster Layer.
Browse and select the downloaded NDVI GeoTIFF file, then click Open.
Apply Symbology:
In the Layers panel, right-click on the NDVI layer and select Properties.
Navigate to the Symbology tab.
Change the Render Type to Singleband pseudocolor.
Set the Min and Max values for NDVI (typically -1 to 1).
Choose a color ramp suitable for vegetation analysis, such as 'Greens', 'Viridis', or a
custom gradient where:
Negative values (e.g., water or barren areas) are represented by cool colors (e.g.,
blue).
Positive values (healthy vegetation) are represented by warm colors (e.g., green).
Visualise the Data:
Click Apply and OK to see the changes.
Contributed by Dr. Rohit Kumar, New Delhi and Dr. Neeti, Azim Premji University, Bangaluru
15
Zoom into the map to analyse the spatial variation of NDVI values. The output may
look like the figure here.
3.5 SOME COMMONLY USED DATA AND CODES
In the section of this exercise, you will learn about some commonly used data and codes
in GEE.
A. DEM
// Load the ALOS Global Digital Surface Model (ALOS DSM) dataset
var alosDem = ee.Image('JAXA/ALOS/AW3D30_V1_1');
Map.addLayer(ROI, {}, 'ROI');
var demROI = alosDem.select('MED');
var demClip = demROI.clip(ROI);
Map.centerObject(ROI, 6);
Map.addLayer(demClip, {min: 0, max: 5000}, 'ALOS DEM');
Export.image.toDrive({
image:demClip,
description: 'ALOS_DEM',
scale:90,
maxPixels:1e10,
region: ROI});
Contributed by Dr. Rohit Kumar, New Delhi and Dr. Neeti, Azim Premji University, Bangaluru
16
B. ESA LULC
Code:
// Download ESA Land Cover
// 1. Import ESA Data
var esa = ee.ImageCollection("ESA/WorldCover/v100").first();
var visualization = {bands: ['Map']};
Map.addLayer(esa, visualization, 'ESA Land Cover');
Map.centerObject(esa);
// 2. Add country boundary layer
var countries = ee.FeatureCollection('USDOS/LSIB_SIMPLE/2017');
var roi = countries.filter(ee.Filter.eq('country_na', 'India'));
Map.addLayer(roi, {}, 'India', false);
// 3. Clip with roi(Region of Interest)
var esa_clip = esa.clip(roi1);
Map.addLayer(esa_clip, visualization, 'Bangladesh ESA Land Cover');
Map.centerObject(esa_clip);
// 4. Export to Drive
Export.image.toDrive({
image: esa_clip,
description: "ESA_LULC_ROI",
scale: 30,
region: roi1,
Contributed by Dr. Rohit Kumar, New Delhi and Dr. Neeti, Azim Premji University, Bangaluru
17
maxPixels: 1e13
});
// Export flooded area as shapefile (for further analysis in e.g. QGIS)
// Convert flood raster to polygons
var lulc_vec = esa_clip.reduceToVectors({
scale: 30,
geometryType:'polygon',
geometry: roi1,
eightConnected: false,
bestEffort:true,
tileScale:2,
});
// Export flood polygons as shape-file
Export.table.toDrive({
collection:lulc_vec,
description:'lulc_extent_vector',
fileFormat:'SHP',
fileNamePrefix:'lulc_vec'
});
C. Cloud Removal
Map.addLayer(ch1);
Map.centerObject(ch1);
var fc = s1.filterBounds(ch1).filterDate('2024-08-01','2024-08-
15').filterMetadata('CLOUD_COVER','less_than',100);
Contributed by Dr. Rohit Kumar, New Delhi and Dr. Neeti, Azim Premji University, Bangaluru
18
print(fc);
var img = ee.Image(fc.mosaic().clip(ch1));
Map.addLayer(img);
// This function masks cloudy pixels.
var cloudMask = function(Image) {
var clouds = ee.Algorithms.Image.simpleCloudScore(s1).select(['cloud']);
return image.updateMask(clouds.lt(25));
};
print(cloudMask);
D. Soil Moisture
var dataset = ee.ImageCollection('NASA/SMAP/SPL4SMGP/007')
.filter(ee.Filter.date('2023-12-16', '2023-12-31')).filterBounds(geometry)
.median();;
var soilMositureSurface = dataset.select('sm_surface');
var soilMositureSurfaceVis = {
min: 0.0,
max: 0.5,
palette: ['0300ff', '418504', 'efff07', 'efff07', 'ff0303'],
};
Map.setCenter(-6.746, 46.529, 2);
Map.addLayer(soilMositureSurface, soilMositureSurfaceVis, 'Soil Mositure');
// Export the image, specifying the CRS, transform, and region.
Contributed by Dr. Rohit Kumar, New Delhi and Dr. Neeti, Azim Premji University, Bangaluru
19
// Export the image, specifying the CRS, transform, and region.
// Create a geometry representing an export region.
Export.image.toDrive({
image: soilMositureSurface,
description: 'SM_April_1fn_2023',
region: geometry
});
In this exercise, you have explored the process of using Google Earth Engine for satellite
data analysis, focusing on Sentinel-2 imagery. The steps include importing datasets from
the GEE catalog or uploading custom data, defining the Region of Interest (ROI) using
geometries or shapefiles, and filtering data based on cloud cover, date ranges, and
location.
You have also covered the part on how to visualise the data by adjusting parameters such
as RGB bands, min/max values, and colour palettes. Additionally, we demonstrate how to
apply cloud masking algorithms and compute vegetation indices like NDVI to assess
vegetation health and density. The NDVI calculation is explained using a straightforward
formula and visualised with colour ramps for interpretation.
Further, the tutorial walks through exporting processed NDVI data to Google Drive for easy
access and downloading in GeoTIFF format, which can then be analysed in GIS software
Contributed by Dr. Rohit Kumar, New Delhi and Dr. Neeti, Azim Premji University, Bangaluru
20
like QGIS. The process of visualising and creating histograms for NDVI values, as well as
exporting results, is also discussed in detail. Finally, additional satellite data processing
techniques like DEM, LULC, cloud removal, and soil moisture analysis are covered,
showcasing the versatility of Google Earth Engine for geospatial analysis.
3.6 EXERCISES: TO BE SUBMITTED
Submit answer to the following to your counsellor for evaluation:
1. Write the major steps to import satellite datasets (e.g., Sentinel-2) and shapefiles for
a specific region in Google Earth Engine.
2. Snapshot of the codes you have written to perform NDVI analysis.
3. Snapshot of the NDVI output in GEE window.
4. Snapshot of the soil moisture output.
5. Write the steps for exporting NDVI data from GEE to your Google drive.
3.7 EXERCISES: EXPLORE YOURSELF
1. Explore the classification algorithms available in GEE.
2. Try implementing classification of remote sensing data of your choice for LULC
mapping.
3.8 REFERENCES
https://cloud.google.com/earth-engine?hl=en
https://earthengine.google.com/
https://earthengine.google.com/faq/
Contributed by Dr. Rohit Kumar, New Delhi and Dr. Neeti, Azim Premji University, Bangaluru
21