ee$Reducer$平均值:用R中的GEE提取平均数个波段/指数

uoifb46i  于 2023-05-20  发布在  其他
关注(0)|答案(1)|浏览(125)

我想从COPERNICUS/S2_SR集合中获取"B2","B3","B4","B8","NDVI","SAVI" and "TVI"平均数据,按日期/平铺。我喜欢提取这些波段和植被指数意味着在我的ROI和我创建一个s2_clean功能波段选择和指数创建,但不工作。我试着去做:

# 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

但我总是有输出:

Error in py_call_impl(callable, dots$args, dots$keywords) : RuntimeError: Evaluation error: non-numeric argument to binary operator.

拜托,有什么能帮忙的吗?

mnemlml8

mnemlml81#

我认为问题在于,在s2_clean()函数中,你对数字使用了$运算符,正确的等式应该是:

SAVI <- 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))$multiply(1.5)

TVI <- img_band_selected$select("B8")$
subtract(img_band_selected$select("B4"))$
multiply(120)$
subtract(img_band_selected$select("B3")$
  subtract(img_band_selected$select("B2"))$
  multiply(200))$
multiply(0.5)

相关问题