r-spatial / r-spatial/rgee

Selection of Sentinel-2 images using `CLOUD_SCORE_PLUS` collection

Open
#362 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
R
Stars
777
Forks
159
PR merge metrics
No merged PRs in 30d

Description

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?

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the R example in the issue, especially the s2_clean function and the linkCollection call that adds CLOUD_SCORE_PLUS. Run the example while changing CLEAR_THRESHOLD from 0.5 to 0.2 and compare the resulting collection size; done means the threshold changes the selected-image count as expected or the cause is documented.

Written by the indexing model from the issue text.

Assessment

Tech stack
r
Domain
data
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.