0

I am trying to obtain the Landsat yearly maximum NDVI values for a region using Earth Engine through the R package rgee (an implementation of Earth Engine Python API for R). So far, I had success in achieving this in the Earth Engine code editor following a similar case in https://mygeoblog.com/2017/05/18/annual-rainfall/ . However, I have not been able to achieve this in the rgee package using a similar code.

This is my code in the code editor:

function addNDVI(image) {
  var ndvi = image.normalizedDifference(['B5', 'B4']).rename('NDVI');
  return image.addBands(ndvi);
}

var region = ee.Geometry.Polygon([[-105, 39.05], [-104.5, 39.05], [-104.5, 38.75], [-105, 38.75], [-105, 39.05]]);

var landsat = ee.ImageCollection('LANDSAT/LC08/C01/T1_SR')
                  .filterDate('2014-01-01', '2019-12-31')
                  .filterBounds(region)
                  .map(addNDVI)
                  .select('NDVI');

var years = ee.List.sequence(2014, 2019);

var yearlyLandsat = ee.ImageCollection.fromImages(
  years.map(function (year) {
    var annual = landsat
      .filter(ee.Filter.calendarRange(year, year, 'year'))
      .max();
    return annual
      .set('year', year)
      .set('system:time_start', ee.Date.fromYMD(year, 1, 1));
  }));
print(years)
print(yearlyLandsat)

This is my code on rgee so far:

library(rgee)

ee_Initialize()

addNDVI <- function(image) {
  ndvi <- image$normalizedDifference(c('B5', 'B4'))$rename('NDVI');
  return(image$addBands(ndvi))
}

region <- ee$Geometry$Polygon(list(c(-105, 39.05), c(-104.5, 39.05), c(-104.5, 38.75), c(-105, 38.75), c(-105, 39.05)))

landsat <- ee$ImageCollection('LANDSAT/LC08/C01/T1_SR')$
                  filterDate('2014-01-01', '2019-12-31')$
                  filterBounds(region)$
                  map(addNDVI)$
                  select('NDVI')

years <- ee$List$sequence(2014, 2019)

landsatYearly <- ee$ImageCollection$fromImages(
    years$map(function(year) {
        yearly <- landsat$
            filter(ee$Filter$calendarRange(year, year, "Year"))$
            select("NDVI")$max()
        return(yearly$
               set("year", year)$
               set("system:time_start", ee$Date$fromYMD(year, 1, 1)))
}))

This code for rgee keeps giving me this error: Evaluation error: argument "year" is missing, with no default.

I believe the answer to this issue may be related to Map function over ImageCollection in Python 2.7 in Earth Engine . However, I have not been able to make it work. How this part for calculating the yearly maximum NDVI should be written for rgee? The logic should be similar to applying this in the Earth Engine Python API.

1 Answer 1

1

This has been described under consideration section on the github page of rgee:

The code before is perfectly valid but rgee will produce an error. This problem should be easily solved by adding the function ee_utils_pyfunc. It will permit to wrap R functions before to send it to reticulate.

Try the following:

library(rgee)

ee_Initialize()

addNDVI <- function(image) {
  ndvi <- image$normalizedDifference(c('B5', 'B4'))$rename('NDVI');
  return(image$addBands(ndvi))
}

region <- ee$Geometry$Polygon(list(c(-105, 39.05), c(-104.5, 39.05), c(-104.5, 38.75), c(-105, 38.75), c(-105, 39.05)))

landsat <- ee$ImageCollection('LANDSAT/LC08/C01/T1_SR')$
  filterDate('2014-01-01', '2019-12-31')$
  filterBounds(region)$
  map(addNDVI)$
  select('NDVI')

years <- ee$List$sequence(2014, 2019)

landsatYearly <- ee$ImageCollection$fromImages(
  years$map( ee_utils_pyfunc(function(year) {
    yearly <- landsat$
      filter(ee$Filter$calendarRange(year, year, "Year"))$
      select("NDVI")$max()
    return(yearly$
             set("year", year)$
             set("system:time_start", ee$Date$fromYMD(year, 1, 1)))
  })))

ee_print(landsatYearly)

# ────────────────────────────────────────────────────────────────────── Earth Engine ImageCollection ──
# ImageCollection Metadata:
#   - Class                      : ee$ImageCollection
# - Number of Images           : 6
# - Number of Properties       : 0
# - Number of Pixels*          : 388800
# - Approximate size*          : 1.19 MB
# Image Metadata (img_index = 0):
#   - ID                         : no_id
# - Time start                 : 2014-01-01
# - Number of Bands            : 1
# - Bands names                : NDVI
# - Number of Properties       : 3
# - Number of Pixels*          : 64800
# - Approximate size*          : 202.50 KB
# Band Metadata (img_band = 'NDVI'):
#   - EPSG (SRID)                : 4326
# - proj4string                : +proj=longlat +datum=WGS84 +no_defs 
# - Geotransform               : 1 0 0 0 1 0
# - Nominal scale (meters)     : 111319.5
# - Dimensions                 : 360 180
# - Number of Pixels           : 64800
# - Data type                  : FLOAT
# - Approximate size           : 202.50 KB
# ──────────────────────────────────────────────────────────────────────────────────────────────────────
# NOTE: (*) Properties calculated considering a constant  geotransform and data type.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.