r-spatial / rgee

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

system:time_start #342

Open wilson733 opened 1 year ago

wilson733 commented 1 year ago

Description

I tried to to rename each image from sentinel 2 at format "YYYY-MM-dd". But the images dont change the format of the dates

What I Did

library(pacman)
p_load(rgee, reticulate, sf, sp, tidyverse, lubridate, DescTools)
ee_Initialize(drive = T)

archivo <-  choose.files(caption = "Seleccione archivo a estandatizar (shp, gpkg)",
                         multi = FALSE)
file_name <- paste0(tools::file_path_sans_ext(archivo),
                    '_AMBIENTADO_GCI.',
                    tools::file_ext(archivo))

poligono<-st_read(archivo, crs= 32721, stringsAsFactors = FALSE,
                  quiet = TRUE)

roi <-poligono %>%
  sf_as_ee()

#Creamos una coleccion de imagenes

collection_imag<- ee$ImageCollection("COPERNICUS/S2_HARMONIZED")$
  filterBounds(roi)$
  filter(ee$Filter$lt("CLOUD_COVERAGE_ASSESSMENT", 1))

#function to calculate satellite index

func_gci <-function(image){
  gci =image$expression(
    expression = 'NIR/GREEN-1',
    opt_map = list(
      'NIR' = image$select('B8'),
      'GREEN' = image$select('B3')
    )
  )
  return(image$addBands(gci$rename('GCI')))
}

collection_gci<- collection_imag$map(func_gci)$
  select("GCI")$map(function(image) {
    image$clip(roi)
  }
  )

rescale_rename <- function(image) {
  date <- ee$Date(image$get("system:time_start"))$format('YYYY_MM_dd')
  return(image$select('GCI')$rename(date))
}

gci_rescaled <- collection_gci$map(rescale_rename)

# Crear una grilla regular de 10 metros x 10 metros centrada en los polígonos

grid <- poligono%>%
  st_make_grid(what = "centers", cellsize = c(250, 250))%>%
  st_intersection(poligono)%>%
  sf_as_ee()
# Extraer los valores de la imagen apilada utilizando la grilla
valores <- ee_extract(gci_rescaled, grid, sf = T, scale = 20)
 X20230531T135709_20230531T140936_T21HTE_2023_05_31