2. Google Earth Engine for R
2.1 Introduction
Google Earth Engine is a cloud-based platform that allows both expert and non-expert users to access and run a large, planetary-scale remote sensing dataset in a matter of minutes.
Currently Google Earth Engine has JavaScript and Python as its official client libraries; however rgee is a new alternative that allows this same Google potential to be integrated within R.
2.2 Why rgee instead of Javascript or Python?
JS (Code Editor) | Python | R |
---|---|---|
|
|
|
- The syntax is shorter and understandable for users with little programming experience.
- It allows the integration of the R spatial ecosystem.
- Application development is easier and more accessible thanks to the integration of flexdashboard and shiny.
- Easy sharing of reproducible code.
- API more friendly and intuitive to R users.
2.3 Installation 📥
In the menu bar, you’ll find the installation tab, where you’ll learn how to instal step by step rgee in different Operating Systems. Beforehand, you need to install some spatial dependencies in your OS. Use the following Bash commands.
# install system dependencies:
sudo apt install libudunits2-dev libgdal-dev libgeos-dev libproj-dev libfontconfig1-dev libjq-dev libprotobuf-dev protobuf-compiler
Once the dependencies are installed, you may proceed to install rgee inside R.
# binary versions of key R packages:
sudo apt install r-cran-rgee r-cran-geojsonio
Information: |
- You must have installed geojsonio to work with some rgee functions like sf_as_ee. |
rgee depends on some Python libraries like numpy and the earth engine api. All this is automatically installed using the following function:
rgee::ee_install()
You need to restart the R session to verify the installation.
library(rgee)
ee_check()
◉ Python version
✓ [Ok] /home/ambarja/.virtualenvs/rgee/bin/python v3.8
◉ Python packages:
✓ [Ok] numpy
✓ [Ok] earthengine-api
2.4 Authentication 🔐
When working with Google Earth Engine you need to register your credentials to move data from Google Drive and Google Cloud Storage to your desktop. These dependencies are not mandatory, but are available within rgee. rgee allows you to work with the three google API’S. These are:
- Google Earth Engine
- Google Drive
- Google Cloud Storage
To authenticate/initialize either Google Drive or Google Cloud Storage, you just need to run:
library(rgee)
#ee_reattach() # reattach ee as a reserve word
# Initialize just Earth Engine
ee_Initialize()
ee_Initialize(user = 'user_name@gmail.com') # Use the argument email is not mandatory, but it's helpful to change of EE user.
# Initialize Earth Engine and GD
ee_Initialize(user = 'user_name@gmail.com', drive = TRUE)
# Initialize Earth Engine and GCS
ee_Initialize(user = 'user_name@gmail.com', gcs = TRUE)
# Initialize Earth Engine, GD and GCS
ee_Initialize(user = 'user_name@gmail.com', drive = TRUE, gcs = TRUE)
If the Google account is verified and the permission is granted, you will be directed to an authentication token. Copy this token and paste it in your R console. Unlike Earth Engine and Google Drive, Google Cloud Storage needs to set up its credential manually (link1 and link2). In all cases, the user’s credentials will be stored in:
ee_get_earthengine_path()
You only have to authorize it once, it will not be necessary for future sessions.
More information, please click here
2.5 Hello spatial world 🌎
library(rgee)
ee_Initialize()
── rgee 1.1.2 ───────────────────────────── earthengine-api 0.1.292 ──
✓ user: not_defined
✓ Initializing Google Earth Engine: DONE!
✓ Earth Engine account: users/ambarja
──────────────────────────────────────────────────────────────────────
# traditional R character
print("Hello spatial world!")
[1] "Hello world!"
# Earth Engine Pipes Style
ee$String("Hello World from Earth Engine!")$getInfo()
[1] "Hello World from Earth Engine!"
Viewing elevation data (pipe integration)
library(rgee)
library(viridis)
ee_Initialize()
viz = list(
min = 500,
max = 5000,
palette = rocket(n = 100,direction = -1)
)
ee$Image("CGIAR/SRTM90_V4") %>%
Map$addLayer(name = "Elevation",visParams = viz) +
Map$addLegend(visParams = viz, name = "Elevation")