https://github.com/aranganath24/rgee

Google Earth Engine for R

https://github.com/aranganath24/rgee

Science Score: 41.0%

This score indicates how likely this project is to be science-related based on various indicators:

  • CITATION.cff file
    Found CITATION.cff file
  • codemeta.json file
  • .zenodo.json file
  • DOI references
    Found 3 DOI reference(s) in README
  • Academic publication links
    Links to: joss.theoj.org, zenodo.org
  • Academic email domains
  • Institutional organization owner
  • JOSS paper metadata
  • Scientific vocabulary similarity
    Low similarity (12.3%) to scientific vocabulary
Last synced: 9 months ago · JSON representation ·

Repository

Google Earth Engine for R

Basic Info
Statistics
  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • Open Issues: 0
  • Releases: 0
Fork of r-spatial/rgee
Created about 5 years ago · Last pushed over 3 years ago
Metadata Files
Readme Contributing License Code of conduct Citation

README.md


Markdownify Markdownify Markdownify
rgee: Google Earth Engine for R

rgee is an R binding package for calling Google Earth Engine API from within R. Various functions are implemented to simplify the connection with the R spatial ecosystem.

Open in Colab R build status Project Status: Active – The project has reached a stable, usable
state and is being actively
developed. codecov License lifecycle status CRAN
status DOI
Buy Me A Coffee

Installation  • Hello World  • How does rgee work?  • Guides  • Contributing  • Citation  • Credits

What is Google Earth Engine?

Google Earth Engine is a cloud-based platform that lets users access 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 other Google-supported client libraries.

JS (Code Editor) Python R
``` javascript var db = 'CGIAR/SRTM90_V4' var image = ee.Image(db) print(image.bandNames()) #> 'elevation' ``` ``` python import ee ee.Initialize() db = 'CGIAR/SRTM90_V4' image = ee.Image(db) image.bandNames().getInfo() #> [u'elevation'] ``` ``` r library(rgee) ee_Initialize() db <- 'CGIAR/SRTM90_V4' image <- ee$Image(db) image$bandNames()$getInfo() #> [1] "elevation" ```

Quite similar, isn't it?. However, additional more minor changes should be considered when using Google Earth Engine with R. Please check the consideration section before you start coding!

Installation

Install from CRAN with:

r install.packages("rgee")

Install the development versions from github with

r library(remotes) install_github("r-spatial/rgee")

Additionally, rgee depends on the Python packages: numpy and ee. To install them, users can follow any of these three methods:

  1. Use ee_install (Highly recommend for users with no experience with Python environments)

r rgee::ee_install()

  1. Use eeinstallset_pyenv (Recommend for users with experience with Python environments)

r rgee::ee_install_set_pyenv( py_path = "/home/csaybar/.virtualenvs/rgee/bin/python", # Change it for your own Python PATH py_env = "rgee" # Change it for your own Python ENV )

Take into account that the Python PATH you set must have installed the Earth Engine Python API and numpy. The use of miniconda/anaconda is mandatory for Windows users, Linux and MacOS users could also use virtualenv. See reticulate documentation for more details.

Other option, only possible for MacOS and Linux, is just set the Python PATH:

r rgee::ee_install_set_pyenv( py_path = "/usr/bin/python3", py_env = NULL )

However, rgee::eeinstallupgrade and reticulate::py_install will not work until you set a Python ENV.

  1. Use the Python PATH setting support that offer Rstudio v.1.4 >. See this tutorial.

After install Python dependencies (and Restart R!!), you might use the function below for checking the status of rgee.

r ee_check() # Check non-R dependencies

Sync rgee with other Python packages

Integrate rgee with geemap.

``` r library(reticulate) library(rgee)

1. Initialize the Python Environment

ee_Initialize()

2. Install geemap in the same Python ENV that use rgee

py_install("geemap") gm <- import("geemap") ```

Upgrade the earthengine-api

r library(rgee) ee_Initialize() ee_install_upgrade()

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. It is necessary once by session!.
  • rgee is "pipe-friendly", we re-exports %>%, but rgee does not require its use.

Hello World

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

Authenticate and Initialize the Earth Engine R API.

r library(rgee) ee_Initialize()

Adds a band containing image date as years since 1991.

r createTimeBand <-function(img) { year <- ee$Date(img$get('system:time_start'))$get('year')$subtract(1991L) ee$Image(year)$byte()$addBands(img) }

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

r collection <- ee$ ImageCollection('NOAA/DMSP-OLS/NIGHTTIME_LIGHTS')$ select('stable_lights')$ map(createTimeBand)

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.

r col_reduce <- collection$reduce(ee$Reducer$linearFit()) col_reduce <- col_reduce$addBands( col_reduce$select('scale')) ee_print(col_reduce)

Create a interactive visualization!

r Map$setCenter(9.08203, 47.39835, 3) Map$addLayer( eeObject = col_reduce, visParams = list( bands = c("scale", "offset", "scale"), min = 0, max = c(0.18, 20, -0.18) ), name = "stable lights trend" )

rgee_01

2. Extract precipitation values

Install and load tidyverse and sf R package, after that, initialize the Earth Engine R API.

``` r library(tidyverse) library(rgee) library(sf)

ee_Initialize() ```

Read the nc shapefile.

r nc <- st_read(system.file("shape/nc.shp", package = "sf"), quiet = TRUE)

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

r terraclimate <- ee$ImageCollection("IDAHO_EPSCOR/TERRACLIMATE") %>% ee$ImageCollection$filterDate("2001-01-01", "2002-01-01") %>% ee$ImageCollection$map(function(x) x$select("pr")) %>% # Select only precipitation bands ee$ImageCollection$toBands() %>% # from imagecollection to image ee$Image$rename(sprintf("PP_%02d",1:12)) # rename the bands of an image

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).

r ee_nc_rain <- ee_extract(x = terraclimate, y = nc["NAME"], sf = FALSE)

Use ggplot2 to generate a beautiful static plot!

r ee_nc_rain %>% pivot_longer(-NAME, names_to = "month", values_to = "pr") %>% mutate(month, month=gsub("PP_", "", month)) %>% ggplot(aes(x = month, y = pr, group = NAME, color = pr)) + geom_line(alpha = 0.4) + xlab("Month") + ylab("Precipitation (mm)") + theme_minimal()

### 3. Create an NDVI-animation (JS version)

Install and load sf, after that, initialize the Earth Engine R API.

``` r library(magick) library(rgee) library(sf)

ee_Initialize() ```

Define the regional bounds of animation frames and a mask to clip the NDVI data by.

r mask <- system.file("shp/arequipa.shp", package = "rgee") %>% st_read(quiet = TRUE) %>% sf_as_ee() region <- mask$geometry()$bounds()

Retrieve the MODIS Terra Vegetation Indices 16-Day Global 1km dataset as an ee.ImageCollection and select the NDVI band.

r col <- ee$ImageCollection('MODIS/006/MOD13A2')$select('NDVI')

Group images by composite date

r col <- col$map(function(img) { doy <- ee$Date(img$get('system:time_start'))$getRelative('day', 'year') img$set('doy', doy) }) distinctDOY <- col$filterDate('2013-01-01', '2014-01-01')

Define a filter that identifies which images from the complete collection match the DOY from the distinct DOY collection.

r filter <- ee$Filter$equals(leftField = 'doy', rightField = 'doy')

Define a join; convert the resulting FeatureCollection to an ImageCollection.

r join <- ee$Join$saveAll('doy_matches') joinCol <- ee$ImageCollection(join$apply(distinctDOY, col, filter))

Apply median reduction among matching DOY collections.

r comp <- joinCol$map(function(img) { doyCol = ee$ImageCollection$fromImages( img$get('doy_matches') ) doyCol$reduce(ee$Reducer$median()) })

Define RGB visualization parameters.

r visParams = list( min = 0.0, max = 9000.0, bands = "NDVI_median", palette = c( 'FFFFFF', 'CE7E45', 'DF923D', 'F1B555', 'FCD163', '99B718', '74A901', '66A000', '529400', '3E8601', '207401', '056201', '004C00', '023B01', '012E01', '011D01', '011301' ) )

Create RGB visualization images for use as animation frames.

r rgbVis <- comp$map(function(img) { do.call(img$visualize, visParams) %>% ee$Image$clip(mask) })

Define GIF visualization parameters.

r gifParams <- list( region = region, dimensions = 600, crs = 'EPSG:3857', framesPerSecond = 10 )

Get month names

r dates_modis_mabbr <- distinctDOY %>% ee_get_date_ic %>% # Get Image Collection dates '[['("time_start") %>% # Select time_start column lubridate::month() %>% # Get the month component of the datetime '['(month.abb, .) # subset around month abbreviations

Use eeutilsgif_* functions to render the GIF animation and add some texts.

``` r animation <- eeutilsgifcreator(rgbVis, gifParams, mode = "wb") animation %>% eeutilsgifannotate( text = "NDVI: MODIS/006/MOD13A2", size = 15, color = "white", location = "+10+10" ) %>% eeutilsgifannotate( text = datesmodismabbr, size = 30, location = "+290+350", color = "white", font = "arial", boxcolor = "#000000" ) # -> animationwtxt

eeutilsgifsave(animationwtxt, path = "rasterasee.gif")

```

## How does rgee work?

rgee is not a native Earth Engine API like the Javascript or Python client. Develop an Earth Engine API from scratch would be tough to maintain, 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 seamless interoperability between R and Python. When an Earth Engine request is created in R, reticulate will transform this piece into Python. Once the Python code is generated, the Earth Engine Python API converts the request to a JSON format. Finally, the request is received by the GEE Platform through a Web REST API. The response will follow the same path.

workflow

Quick Start User's Guide for rgee

Created by: - EN and POR: Andres Luiz Lima Costa https://bit.ly/3p1DFm9 - SPA: Antony Barja Ingaruca https://ambarja.github.io/

## 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

👍 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 person via Twitter or a blog post.

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

``` r citation("rgee") To cite rgee in publications use:

C Aybar, Q Wu, L Bautista, R Yali and A Barja (2020) rgee: An R package for interacting with Google Earth Engine Journal of Open Source Software URL https://github.com/r-spatial/rgee/.

A BibTeX entry for LaTeX users is

@Article{, title = {rgee: An R package for interacting with Google Earth Engine}, author = {Cesar Aybar and Quisheng Wu and Lesly Bautista and Roy Yali and Antony Barja}, journal = {Journal of Open Source Software}, year = {2020}, } ```

Credits

We want to offer a special thanks :raised_hands: :clap: to Justin Braaten for his wise and helpful comments in the whole development of rgee. As well, we would like to mention the following third-party R/Python packages for contributing indirectly to the improvement of rgee:

Owner

  • Login: aranganath24
  • Kind: user

Citation (CITATION.cff)

# This CITATION.cff file was created by ruby-cff (v 0.9.0).
# Gem: https://rubygems.org/gems/cff
# CFF: https://citation-file-format.github.io/

cff-version: 1.2.0
message: "If you use this software, please cite it as below."
title: "rgee: An R package for interacting with Google Earth Engine"
authors:
- family-names: Aybar 
  given-names: Cesar
  orcid: https://orcid.org/0000-0003-2745-9535
- family-names: Qiusheng  
  given-names: Wu
  orcid: https://orcid.org/0000-0001-5437-4073
- family-names: Bautista
  given-names: Lesly
  orcid: https://orcid.org/0000-0003-3523-8687
- family-names: Yali
  given-names: Roy
  orcid: https://orcid.org/0000-0003-4542-3755
- family-names: Barja
  given-names: Antony
  orcid: https://orcid.org/0000-0001-5921-2858
version: 1.1.1
date-released: 2019-07-27
license: Apache License 2.0
url: "https://github.com/r-spatial/rgee"
preferred-citation:
type: article
authors:
- family-names: Aybar 
  given-names: Cesar
  orcid: https://orcid.org/0000-0003-2745-9535
- family-names: Qiusheng  
  given-names: Wu
  orcid: https://orcid.org/0000-0001-5437-4073
- family-names: Bautista
  given-names: Lesly
  orcid: https://orcid.org/0000-0003-3523-8687
- family-names: Yali
  given-names: Roy
  orcid: https://orcid.org/0000-0003-4542-3755
- family-names: Barja
  given-names: Antony
  orcid: https://orcid.org/0000-0001-5921-2858
journal: "Journal of Open Source Software"
month: 7
start: 1
end: 6 
title: "rgee: An R package for interacting with Google Earth Engine"
year: 2020

GitHub Events

Total
Last Year