NOTE: Access to Google Earth Engine is only available to registered users. The current version of rgee has been built considering the earthengine-api 0.1.213

Build Status Project Status: Active – The project has reached a stable, usable state and is being actively developed. AppVeyor build status codecov License lifecycle CII Best Practices DOI CRAN status

More than 300+ examples using Google Earth Engine with R are available here

rgee is a binding package for calling Google Earth Engine API from within R. Additionally, several functions have been implemented to make simple the connection with the R spatial ecosystem. The rgee package structure has been inspired by the TensorFlow R package.

What is Google Earth Engine?

Google Earth Engine is a cloud-based platform that allows users to have an easy access to a petabyte-scale archive of remote sensing data and run geospatial analysis on Google’s infrastructure. Currently, Google offers support only for Python and JavaScript. rgee will fill the gap starting to provide support to R!. Below you will find the comparison between the syntax of rgee and the two Google-supported client libraries.

Earth Engine Javascript API:

Earth Engine Python API:

rgee:

Quite similar, isn’t it?. However, there are additional smaller changes that you must consider when you use Google Earth Engine with R. Please check the consideration section before start coding!

Installation

Install the rgee package from GitHub is quite simple, you just have to run in your R console as follows:

remotes::install_github("csaybar/rgee")

rgee depends on sf. Therefore, it is necessary to install its external libraries, follow the installation steps specified here.

Requirements

Prior to using rgee you will need to install a Python version higher than 3.5 in your system. rgee counts with a installation module, use it to quickly set up the external dependencies of rgee:

Additionally, use these functions, as many times as you want, for checking user info and sanity of credentials, drivers and Python packages.

Use this function to clean the system variables set in ee_set_pyenv.

Also, consider checking the setup section for major information to customizing Python installation.

Package Conventions

  • All rgee functions have the prefix ee_. Auto-completion is your friend :).
  • Full access to the Earth Engine API with the prefix ee$…:.
  • Authenticate and Initialize the Earth Engine R API with ee_Initialize:, you just will need to do it once by session!.
  • rgee is “pipe-friendly”, we re-exports %>%, but rgee does not require its use.
  • Wrap your R function using ee_pyfunc before passing them to the Earth Engine Web REST API. This is not compulsory, but it will help reduce possible bugs 🐛.

Quick Demo

1. Compute the trend of night-time lights (JS version)

Authenticate and Initialize the Earth Engine R API.

Adds a band containing image date as years since 1991.

Map the time band creation helper over the night-time lights collection.

Compute a linear fit over the series of values at each pixel, visualizing the y-intercept in green, and positive/negative slopes as red/blue.

Create a interactive visualization!

ee_map(eeobject = col_reduce,
       vizparams = list(min=0, max= c(0.18, 20, -0.18)),
       bands=c('scale', 'offset', 'scale'),
       objname = 'stable lights trend')

rgee_01

2. Extract precipitation values

Load sf and authenticate and initialize the Earth Engine R API.

Read the nc shapefile.

nc <- st_read(system.file("shape/nc.shp", package = "sf"), quiet = TRUE) %>%
  st_transform(4326) # Transform coordinates

Map each image from 2001 to extract the monthly precipitation (Pr) from the Terraclimate dataset

Extract monthly precipitation values from the Terraclimate ImageCollection through ee_extract. ee_extract works similar to raster::extract you just need to define: the ImageCollection object (x), the geometry (y), and a function to summarize the values (fun).

ee_nc_rain <- ee_extract(x = terraclimate, y = nc, fun = ee$Reducer$max(), id = "FIPS")
colnames(ee_nc_rain) <- c("FIPS", month.abb)
head(ee_nc_rain)
FIPS Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
37009 93 68 106 168 73 97 117 107 166 4 89 56
37005 85 64 99 165 66 96 107 106 163 4 83 53
37171 95 54 87 143 59 114 101 119 162 2 67 48
37053 122 50 67 118 135 183 142 213 174 7 72 49
37131 115 49 63 108 115 163 152 195 132 0 57 44
37091 122 43 64 109 121 169 146 200 143 1 57 43

Create a simple plot!

ee_nc_rain <- merge(nc, ee_nc_rain, by = "FIPS")
plot(ee_nc_rain["Jan"], main = "2001 Jan Precipitation - Terraclimate", reset = FALSE)

How does rgee work?

rgee is not a native Earth Engine API like the Javascript or Python client, to do this would be extremely hard, especially considering that the API is in active development. So, how is it possible to run Earth Engine using R? the answer is reticulate. reticulate is an R package designed to allow a seamless interoperability between R and Python. When an Earth Engine process is created in R, firstly, reticulate transforms this piece of code to Python. Once the Python code is obtained, the Earth Engine Python API transform the request to a JSON format. Finally, the query (in JSON) is received by the Google Earth Engine Platform thanks to a Web REST API. The response will follow the same path. If you are searching a way to interact with the Earth Engine Asset (EEA), rgee offers also functions to batch upload(download) spatial objects. Additionally, you could easily manage EEA through the ee_manage_* interface.

workflow

Code of Conduct

Please note that the rgee project is released with a Contributor Code of Conduct. By contributing to this project, you agree to abide by its terms.

Contributing Guide

👍🎉 First off, thanks for taking the time to contribute! 🎉👍 Please review our Contributing Guide.

Share the love ❤️

Think rgee is useful? Let others discover it, by telling them in per on, via Twitter or a blog post.

Using rgee for a paper you are writing? Consider citing it