Google Earth Engine Tutorial: Beginners Guide 30 Extract Monthly NDVI to CSV
Credit: Youtube Channel “Terra Spatial, Learn how to calculate and export monthly NDVI values to CSV format for time series analysis.”
You can see all the tutorials from here: Techgeo Academy.
Introduction to Google Earth Engine
Google Earth Engine (GEE) is a powerful platform for environmental data analysis and visualization. It enables users to process and analyze large geospatial datasets using JavaScript or Python. This tutorial will guide you through the process of extracting monthly Normalized Difference Vegetation Index (NDVI) values from a satellite dataset and exporting them to a CSV file.
Prerequisites
To follow this tutorial, ensure you have the following:
- A Google account.
- Access to the Google Earth Engine API (sign up at https://earthengine.google.com/).
- Basic understanding of JavaScript (for GEE scripting).
- Google Drive enabled for exporting CSV files.
Step-by-Step Guide to Extract Monthly NDVI
Step 1: Initialize Google Earth Engine
Before running any code, initialize the Earth Engine API with your Google account. Use the following script in the GEE Code Editor:
var ee = require('ee');
ee.Authenticate();
ee.Initialize();
Step 2: Load the NDVI Dataset
Choose a dataset with NDVI values, such as MODIS (MOD13Q1). Load it using Earth Engine:
var dataset = ee.ImageCollection('MODIS/006/MOD13Q1');
var ndvi = dataset.select('NDVI'); // Select NDVI band
Step 3: Filter Data by Time Range
Limit your dataset to a specific time period. For example, to get data from 2020 to 2022:
var filtered = ndvi.filterDate('2020-01-01', '2022-12-31');
Step 4: Create Monthly Images
Iterate over each month in the filtered dataset. Use filter(ee.Filter.month('month', 1, 12))
for each month and then process the data:
var months = ee.List.sequence(1, 12);
var monthlyImages = months.map(function(month) {
return filtered.filter(ee.Filter.month('month', month)).mean();
});
Step 5: Export to CSV
For each monthly image, export the NDVI values to a CSV. Use exportImage
with the format
set to ‘csv’ and specify the geometry
and scale
:
monthlyImages.evaluate(function(images) {
images.forEach(function(image) {
var img = ee.Image(image);
var fileName = 'monthly_ndvi_' + img.get('system:time_start').getInfo();
img.select('NDVI').getRegion(ee.Geometry.Rectangle([ -180, -90, 180, 90 ]), 1000).getInfo();
ee.data.exportImage(img, {
description: fileName,
fileNamePrefix: fileName,
region: ee.Geometry.Rectangle([ -180, -90, 180, 90 ]),
scale: 500,
format: 'csv'
});
});
});
Step 6: Monitor Export
After running the script, check the Code Editor
> Tasks
section to monitor the export progress. The CSV file will be saved in your Google Drive.
FAQ
Why isn’t the CSV file downloading?
Ensure you are authenticated with your Google account and have linked it to Google Drive. Check the Tasks
panel for errors during the export process.
Can I use a different dataset for NDVI?
Yes, replace the dataset ID (e.g., ‘MODIS/006/MOD13Q1’) with another source like Landsat or Sentinel. Verify the dataset’s NDVI band name and adjust the code accordingly.
How do I calculate the mean NDVI for a region?
Use reduceRegion
with the mean
reducer. Example: img.reduceRegion(ee.Reducer.mean(), geometry, scale).get('NDVI')
.
What is the maximum scale for exporting CSV?
Scale is determined by the dataset’s resolution. For MODIS, 500m is standard, but adjust based on your needs and data availability.
Can I export data for specific locations?
Yes, replace the region
parameter with a custom geometry, such as a polygon or a point, using ee.Geometry.Rectangle
or ee.Geometry
functions.