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).
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.
After the project is initialized you’ll see an RStudio IDE. Name the project “rgee”.
From the RStudio console, install the rgee and rstudioapi packages.
Import the rgee package.
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.
During the install process you’ll be asked a series of questions, answer yes to all:
- Install Miniconda — yes:
- Store environmental variables EARTHENGINE_PYTHON and EARTHENGINE_ENV in your .Renviron file—yes:
- Restart R — yes:
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.
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:
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"))
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.
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.
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.
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.
Run the following code from the RStudio console to import the reticulate package and install eemont. Restart R after running!
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.
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?