Monitoring vegetation dynamics using NDVI (Normalized Difference Vegetation Index) is a vital component of ecological research and agricultural planning. In this article, we walk through how to automate the export of monthly NDVI images for Kenya using Google Earth Engine (GEE) through R with reticulate, and how to efficiently manage Google Drive storage by automatically downloading and deleting exported assets.
Sentinel-2 satellite data offers 10m–60m resolution optical imagery, ideal for computing NDVI. However, manually exporting NDVI for each month and managing these files on Google Drive is time-consuming and storage-intensive.
This R-based automation pipeline:
Exports cloud-masked monthly NDVI composites.
Uses fallback median composites for months with no data.
Downloads NDVI .tif files to a local machine.
Cleans up your Google Drive by removing exported files after download.
Conda environment with earthengine-api installed (e.g., gee-demo).
R packages: reticulate, googledrive, fs, purrr.
Earth Engine access and initialized project.
Google Drive linked to Earth Engine exports.
library(reticulate)
use_condaenv("gee-demo", conda = "auto", required = TRUE)
ee <- import("ee")
time <- import("time")
ee$Initialize(project = "smart-caster-359617")
kenya_poly <- ee$Geometry$Polygon(list(
list(c(33.2, -5.4), c(42.6, -5.4), c(42.6, 5.6), c(33.2, 5.6), c(33.2, -5.4))
))
maskClouds <- function(image) {
mask_qa <- image$select('QA60')$bitwiseAnd(py_eval("1 << 10"))$eq(0L)$
And(image$select('QA60')$bitwiseAnd(py_eval("1 << 11"))$eq(0L))
scl_mask <- image$select('SCL')$gte(4L)$And(image$select('SCL')$lte(7L))
image$updateMask(mask_qa$And(scl_mask))
}
computeNDVI <- function(image) {
image$normalizedDifference(list('B8', 'B4'))$rename('NDVI')$
copyProperties(image, list('system:time_start'))
}
The function export_monthly_ndvi():
Iterates through each month of a given year.
Filters and processes Sentinel-2 data.
Exports NDVI composites to Google Drive.
export_monthly_ndvi(2020)
If no images are available for a given month, it uses a fallback median composite of the full year—ensuring coverage continuity.
After export, this function automates downloading all .tif files and deletes the source folder from Drive:
download_and_cleanup_ndvi("ndvi")
Steps:
Authenticates with Google Drive.
Locates the export folder
Downloads each file to a local folder.
Deletes the remote folder to free up space.
Google Drive has limited storage, especially on free plans. This pipeline:
Avoids piling up .tif exports month after month.
Downloads immediately and removes cloud copies.
Maintains local backups while freeing cloud space.
This is particularly useful for longitudinal NDVI monitoring across large areas like Kenya, where data can accumulate quickly.
After running the script, you'll have a folder like:
your_working_directory/
├── ndvi/
│ ├── NDVI_2020_01.tif
│ ├── NDVI_2020_02_fallback.tif
│ ├── ...