r-spatial / rgee

Google Earth Engine for R
https://r-spatial.github.io/rgee/
Other
692 stars 148 forks source link

Selection of Sentinel-2 images using `CLOUD_SCORE_PLUS` collection #362

Open Leprechault opened 6 months ago

Leprechault commented 6 months ago

I want to select Sentinel-2 images using CLOUD_SCORE_PLUS for cloud/shadow quality, not QA60. For this, I try for a maximum 20% of shadow:

# Packages
library(tidyverse)
library(rgee)
library(sf)
ee_Initialize(drive=TRUE)

# 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_HARMONIZED')
csPlus <- ee$ImageCollection('GOOGLE/CLOUD_SCORE_PLUS/V1/S2_HARMONIZED')

# Define the QA band and clear threshold
QA_BAND <- 'cs'
CLEAR_THRESHOLD <- 0.5

# Function for remove cloud and shadows ------------------------------------------
s2_clean <- function(img) {

  # Select only band of interest, for instance, B2,B3,B4,B8
  img_band_selected <- img$select("B[2-4|8]")
  # quality band
  img$updateMask(img$select(QA_BAND)$gte(CLEAR_THRESHOLD))
}

# Select S2 images ---------------------------------------------------------------
s2_roi  <- s2$
  filterBounds(roi)$
  linkCollection(csPlus, list(QA_BAND))$
  filter(ee$Filter$date(as.character(as.Date("2024-01-01")), as.character(as.Date(as.Date(Sys.Date())))))$
  map(s2_clean)

s2_roi_add_area <- s2_roi$map(
  function(img) {
    img$set("area", img$clip(roi)$geometry()$area())
  }
)

# Get the dates and IDs of the selected images ------------------------------------
area <- floor(ee_utils_py_to_r(roi$area(maxError=1)$getInfo()))
ic_date_gt_area <- s2_roi_add_area$filterMetadata("area", "greater_than", area)

nimages <- ic_date_gt_area$size()$getInfo()
nimages 

# Function for the last 6 characters
substrRight <- function(x, n){
  substr(x, nchar(x)-n+1, nchar(x))
}

# Download the results
s2_ic_local <- ee_imagecollection_to_local(
  ic = s2_roi_add_area,
  scale = 10,
  region = roi,
  via = "drive",
  add_metadata = FALSE,
  dsn = paste0("stringr::str_extract(gsub("COPERNICUS/S2_SR/","",ic_date$id),
"20\\d{2}\\d{2}\\d{2}"),"_",substrRight(ic_date$id,6))

But there is some trouble because the number of images is always the same despite the changes in the CLEAR_THRESHOLD <- 0.2 parameter values. Please, could someone help me?