Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Selection of Sentinel-2 images using CLOUD_SCORE_PLUS collection #362

Open
Leprechault opened this issue May 3, 2024 · 0 comments
Open

Comments

@Leprechault
Copy link

Leprechault commented May 3, 2024

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?

@Leprechault Leprechault changed the title Selection of Sentinel-2 images using CLOUD_SCORE_PLUS Selection of Sentinel-2 images using CLOUD_SCORE_PLUS collection May 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant