r-spatial / rgee

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

It is possible to have a seasonal operator? #308

Open agronomofiorentini opened 1 year ago

agronomofiorentini commented 1 year ago

At submit an issue, please attached the following information of your rgee session:

library(rgee)

# Initialize the Earth Engine module.
ee_Initialize()

# Print metadata for a DEM dataset.
print(ee$Image('USGS/SRTMGL1_003')$getInfo())

Attach your Python (reticulate) configuration:

library(reticulate)
py_config()

python:         C:/Program Files/Python310/python.exe
libpython:      C:/Program Files/Python310/python310.dll
pythonhome:     C:/Program Files/Python310
version:        3.10.4 (tags/v3.10.4:9d38120, Mar 23 2022, 23:13:41) [MSC v.1929 64 bit (AMD64)]
Architecture:   64bit
numpy:          C:/Users/Utente/AppData/Roaming/Python/Python310/site-packages/numpy
numpy_version:  1.22.3
ee:             C:\Users\Utente\AppData\Roaming\Python\Python310\site-packages\ee

NOTE: Python version was forced by RETICULATE_PYTHON_FALLBACK

Description

Is it possible to make a google earth engine query with seasonal dates?

Let me give you an example, I would like to acquire the satellite images that are between May and June from 2017 to the 2022.

What I Did

library(rgee)

ee_Initialize(drive = TRUE)

roi <- AOI %>%
    st_bbox() %>%
    st_as_sfc() %>%
    sf_as_ee()

  s2 <- ee$ImageCollection("COPERNICUS/S2_SR")

  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)
  }

  sentinel_clean <- function(img) {
    # Select only band of interest, for instance, B4,B8
    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)
  }

  s2_ocona <- s2$
    filterBounds(roi)$
    filter(ee$Filter$lte("CLOUDY_PIXEL_PERCENTAGE", 10))$
    filter(ee$Filter$date(paste("2017-05-01"), paste("2017-06-30")))$
    filter(ee$Filter$date(paste("2018-05-01"), paste("2018-06-30")))$
    # so on
    map(sentinel_clean)

  nimages <- s2_ocona$size()$getInfo()
  ic_date <- ee_get_date_ic(s2_ocona)

  ic_date 

  s2_ic_local <- ee_imagecollection_to_local(ic = s2_ocona,
                                             scale = 10,
                                             region = roi,
                                             via = 'drive')