'ee$Reducer$mean: Extract mean several bands/indices with GEE in R
I'd like to get "B2","B3","B4","B8","NDVI","SAVI" and "TVI"
average data from COPERNICUS/S2_SR
collection by date/tile. I like to extract these bands and vegetation indices mean inside my ROI and I create a s2_clean
function for bands selection and indices creation but doesn't work.
I try to do:
# Packages
library(tidyverse)
library(rgee)
library(sf)
ee_Initialize(drive=TRUE)
# Function for remove cloud and shadows ------------------------------------------
getQABits <- function(image, qa) {
# Convert decimal (character) to decimal (little endian)
qa <- sum(2^(which(rev(unlist(strsplit(as.character(qa), "")) == 1))-1))
# Return a single band image of the extracted QA bits, giving the qa value.
image$bitwiseAnd(qa)$lt(1)
}
# Selection of bands and vegetation indices ---------------------------------------
s2_clean <- function(img) {
# Select NDVI
img_band_selected <- img$select("B[2-4|8]")
# quality band
ndvi_qa <- img$select("QA60")
# Select pixels to mask
quality_mask <- getQABits(ndvi_qa, "110000000000")
# Mask pixels with value zero.
img_band_selected$updateMask(quality_mask)
# Lists of bands and vegetation indices
B2 <- img$select("B2")
B3 <- img$select("B3")
B4 <- img$select("B4")
B8 <- img$select("B8")
NDVI <- img_band_selected$select("B8")$subtract(img_band_selected$select("B4"))$divide(img_band_selected$select("B8")$add(img_band_selected$select("B4")))
SAVI <- 1.5*img_band_selected$select("B8")$subtract(img_band_selected$select("B4"))$divide(img_band_selected$select("B8")$subtract(img_band_selected$select("B4"))$add(0.5))
TVI <- 0.5$multiply(120$multiply(img_band_selected$select("B8")$subtract(img_band_selected$select("B4")))-(200*(img_band_selected$select("B3")$subtract(img_band_selected$select("B2")))))
select<- c(B2,B3,B4,B8,NDVI,SAVI,TVI)
return(selected)
}
# Define a Region of interest
roi <-ee$Geometry$Point(-52.19032,-30.25413)$buffer(500)
# Sentinel-2 MSI dataset into the Earth Engine’s public data archive ------------
s2 <- ee$ImageCollection("COPERNICUS/S2_SR")
# Select S2 images ---------------------------------------------------------------
s2_roi <- s2$
filterBounds(roi)$
filter(ee$Filter$lte("CLOUDY_PIXEL_PERCENTAGE", 1))$
filter(ee$Filter$date(as.character(as.Date("2019-12-04")), as.character(as.Date("2020-05-03"))))$
map(s2_clean)
s2_roi_add_area <- s2_roi$map(
function(img) {
img$set("area", img$clip(roi)$geometry()$area())
}
)
#Extract average NDVI values
ee_mean<- ee_extract(
x = s2_roi_add_area,
y = roi,
scale = 10,
fun = ee$Reducer$mean(),
via = "drive"
)
ee_mean
But I always have as output:
Error in py_call_impl(callable, dots$args, dots$keywords) : RuntimeError: Evaluation error: non-numeric argument to binary operator.
Plaese, any help with it?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|