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.