0

我想使用包COPERNICUS/S2_SR按日期从收集中获取 NDVI 平均数据。rgee我不喜欢用 my 中的所有值提取 NDVI 平均值ROI,但在第 5 个百分位和第 95 个 NDVI 值的区间内。

我尝试这样做:

# 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[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 NDVI 
  img_band_selected <- img_band_selected$select("B8")$subtract(img_band_selected$select("B4"))$divide(img_band_selected$select("B8")$add(img_band_selected$select("B4")))
}


# 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_ndvi<- ee_extract(
 x = s2_roi_add_area,
 y = roi,
 scale = 10,
 fun = ee$Reducer$mean(),
 via = "drive"
)
ee_mean_ndvi

我没有找到任何组合ee$Reducer$mean()ee$Reducer$percentile(95)在同一ee_extract功能中的方法。请问,有什么帮助吗?

4

0 回答 0