Running Google Earth Engine from RStudio Cloud

Justin Braaten
5 min readMay 24, 2021

A collaboration between myself, David Montero Loaiza, and Cesar Aybar

Increasingly I want to do all of my work from a web browser using cloud services. When it comes to analyzing and visualizing earth observation data with Google Earth Engine, the JavaScript Code Editor has always provided this opportunity. And when I want to work with the Python API, I use Google Colab. There’s a third option though: RStudio Cloud with the rgee package.

The rgee package wraps and extends Earth Engine’s Python API allowing R users to perform Earth Engine analyses using R syntax, including pipes! This also means being able to use RStudio’s IDE and leverage R’s many useful charting and geospatial packages for downstream processing.

Until recently I’ve only used rgee from a local R/RStudio environment. In this blog I share my experience running rgee from RStudio Cloud, including setup and testing a few scripts.

A bit of housekeeping: markdown code blocks indicate that the code should be run from the R console, embedded code blocks indicate an R script, each of which contains everything needed to be run independently (assuming you’ve completed the rgee installation steps).

Setup

Apply for Earth Engine

If you’re not already a registered developer, sign up here.

Sign up for RStudio Cloud

Head over to RStudio Cloud and sign up, or log in if you already have an account.

There are multiple account tiers, including a free tier, which I’ll be using for this experiment. As I’m writing this, the free tier includes:

  • Up to 15 projects total
  • 1 shared space (5 members and 10 projects max)
  • 15 project hours per month
  • Up to 1 GB RAM per project
  • Up to 1 CPU per project
  • Up to 1 hour background execution time

…which should be sufficient because most of the heavy computation will be done on Earth Engine’s machines, not RStudio’s.

Add a project for rgee

Add a new project dedicated to rgee and the packages you’ll want for working with Earth Engine-derived data. From the RStudio Cloud workspace manager, select “New Project”. Learn more about projects here.

Add a new project from the RStudio Cloud workspaces manager. Learn more about projects.

After the project is initialized you’ll see an RStudio IDE. Name the project “rgee”.

Name the new RStudio Cloud project “rgee”.

Install rgee

From the RStudio console, install the rgee and rstudioapi packages.

install.packages(c("rgee", "rstudioapi"))

Import the rgee package.

library(rgee)

Create a Python environment for accessing the Earth Engine Python API through rgee. As part of this step the earthengine-api Python library will be installed.

ee_install()

During the install process you’ll be asked a series of questions, answer yes to all:

  • Install Miniconda — yes: y
  • Store environmental variables EARTHENGINE_PYTHON and EARTHENGINE_ENV in your .Renviron file—yes: y
  • Restart R — yes: 1

At this point, all of the rgee and Earth Engine Python API dependencies are installed. Next, you’ll need to authenticate to Earth Engine.

Earth Engine authentication

Load rgee and initialize.

library(rgee)
ee_Initialize()

When you run ee_Initialize() the program will check to see if there are stored Earth Engine credentials. You’ll be asked whether you’d like to stop seeing a welcome message again, answer yes: y

A prompt will appear for you to authenticate (make sure popup blockers are turned off for rstudio.cloud domains). Paste the generated token into the RStudio command prompt (the credentials will be saved, you’ll only need to do this once). After pressing enter, you’ll be ready to code with rgee!

Install a few helper packages for client-side processing

install.packages(c("tidyverse", "geojsonio", "magick"))

Examples

Nighttime lights

Display an interactive map of nighttime light trends 1992–2014 where the y-intercept is green, and positive/negative slopes as red/blue in RGB space. The entire analysis is done on Earth Engine servers; the resulting tiles are passed to your RStudio Cloud virtual machine for rendering in a Leaflet map.

Map of nighttime light trends 1992–2014.

Extract precipitation values and plot

Fetch average areal rainfall in counties of North Carolina, USA for the year 2001 from the TerraClimate dataset hosted by Earth Engine. Pass the data to your RStudio Cloud VM using the rgee ee_extract function and then plot the tabular data using ggplot2.

Average areal rainfall in counties of North Carolina for the year 2001 according to the TerraClimate dataset.

NDVI animation

Generate a time series animation of NDVI for Peru’s Arequipa Region. A MODIS image collection is prepared by Earth Engine, a resulting GIF animation is downloaded to your RStudio Cloud VM and post-processed using the R magick wrapper to include annotation.

NDVI time series animation for Peru’s Arequipa Region.

eemont in rgee

eemont is a Python package that extends the Earth Engine Python API with pre-processing and processing tools for common satellite platforms by adding new methods for different Earth Engine objects. It has excellent convenience functions that greatly reduce the number of code lines needed to process imagery. I like to use it when working with the Python API in Colab, but it can be used with rgee too, the following shows how.

Install eemont

Run the following code from the RStudio console to import the reticulate package and install eemont. Restart R after running!

library(reticulate)
py_install("eemont")

Calculate cloud-free annual median NDWI with 1 line of code

The following script gathers a Landsat 8 surface reflectance image collection intersecting a region in northwest Colombia for the year 2020, then for each image, applies the CFMask cloud mask, scales to native units, and calculates the NDWI index. Finally, it reduces the temporal dimension to median, producing a cloud-free composite. It does all of the image processing and temporal reduction with 1 line of code, thanks to eemont.

Cloud-free annual median NDWI composite made simple thanks to the eemont package.

Outro

RStudio Cloud worked well in all of these examples despite having only 1 GB of RAM for the free tier. Stay tuned for a future post where I’ll share my experience using RStudio Cloud with rgee, eemont, and rayshader to generate a 3D Landsat timelapse animation based on Alexandre Bevington’s excellent tutorial. Will the low RAM be an issue?

--

--