Leprechault
Leprechault

Reputation: 1823

Mathematical expression problem using image$expression()

I'd like to create the SAVI index using the formula '1.5 * ((NIR - RED) / (NIR - RED + 0.5))' inside image$expression function, but when I try to do it:

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

    # Compute the SAVI using an expression.
    img_band_selected <- image$expression(
        expression = '1.5 * ((NIR - RED) / (NIR - RED + 0.5))',
        opt_map =  list(
            'NIR' = image$select('B8'),
            'RED' = image$select('B4')
        )
    )
        return(img_band_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

The output is:

Error in py_call_impl(callable, dots$args, dots$keywords) : 
RuntimeError: Evaluation error: object of type 'closure' is not subsettable.

Please, any type or help about it?

Upvotes: 1

Views: 133

Answers (0)

Related Questions