##Introducción: En este cuaderno se observa cómo con la descarga de una imagen Sentinel y con la ayuda de diferentes librerías como stars y leaflet se pueden hacer ploteos con dos bandas de color diferentes y por último un ploteo del índice de vegetación.

##Instalación de paquetes

rm ()
rm(list = ls(all=TRUE))

#Carga de librerías

library(rstac)
## Warning: package 'rstac' was built under R version 4.6.1
library(stars)
## Warning: package 'stars' was built under R version 4.6.1
## Cargando paquete requerido: abind
## Cargando paquete requerido: sf
## Linking to GEOS 3.14.1, GDAL 3.12.1, PROJ 9.7.1; sf_use_s2() is TRUE
library(tmaptools)
## Warning: package 'tmaptools' was built under R version 4.6.1
library(mapview)
## Warning: package 'mapview' was built under R version 4.6.1
library(leaflet)
library(sf)
library(ggplot2)
## Warning: package 'ggplot2' was built under R version 4.6.1
library(gdalcubes)
## Warning: package 'gdalcubes' was built under R version 4.6.1
library(tmap)
## Warning: package 'tmap' was built under R version 4.6.1

##Definición del área de interés

setwd("C:/Users/imthe/Documents/GB2/Proyecto1")
getwd()
## [1] "C:/Users/imthe/Documents"
list.files()
##  [1] "ArcGISEarth"                                                
##  [2] "Bolivar_líneas de agua_.gpkg"                               
##  [3] "Certificación laboral Pedro Tangua Conductor SCT477.pdf"    
##  [4] "Cuaderno-de-Soil-Grids.knit.md"                             
##  [5] "Cuaderno-de-Soil-Grids.Rmd"                                 
##  [6] "Cuaderno-Sentinel-2.html"                                   
##  [7] "Cuaderno-Sentinel-2.Rmd"                                    
##  [8] "Cuaderno 1 _ Informe de geomática.Rmd"                      
##  [9] "Cuaderno de atributos geomorfométricos.nb.html"             
## [10] "Cuaderno de atributos geomorfométricos.Rmd"                 
## [11] "Cuaderno de Cálculos con Dem.Rmd"                           
## [12] "Cuaderno de Interpolación.nb.html"                          
## [13] "Cuaderno de Interpolación.Rmd"                              
## [14] "Cuaderno de Soil Grids.nb.html"                             
## [15] "Cuaderno de Soil Grids.Rmd"                                 
## [16] "Cuaderno Sentinel-2.nb.html"                                
## [17] "Cuaderno Sentinel-2.Rmd"                                    
## [18] "Cuaderno sobre centroides.nb.html"                          
## [19] "Descargas - Acceso directo.lnk"                             
## [20] "desktop.ini"                                                
## [21] "Formato Responsabilidad IndividualSalAcad- Laura Cortez.pdf"
## [22] "Formato Salidas Académicas-Laura Cortez.pdf"                
## [23] "Formato Salidas Académicas-Laura Cortez.xlsx"               
## [24] "GB2"                                                        
## [25] "InterpolaciónSoilGrids.Rmd"                                 
## [26] "InterpolaciónSoilGrids_files"                               
## [27] "map.geojson"                                                
## [28] "MGN2025_MPIO_GRAFICO"                                       
## [29] "Mi música"                                                  
## [30] "Miprimercuaderno.html"                                      
## [31] "Miprimercuaderno.nb.html"                                   
## [32] "Miprimercuaderno.Rmd"                                       
## [33] "Mis imágenes"                                               
## [34] "Mis vídeos"                                                 
## [35] "Plantillas personalizadas de Office"                        
## [36] "Presentación Caracterización Finca Normandía versión 2.pptx"
## [37] "Santa_Rosa_del_Sur"                                         
## [38] "Santa_Rosa_delSur"                                          
## [39] "World _Cities"                                              
## [40] "Zona de interés- Santa Rosa del Sur.geojson"
Santa_Rosa_del_Sur <- st_read("map.geojson")
## Reading layer `map' from data source `C:\Users\imthe\Documents\map.geojson' using driver `GeoJSON'
## Simple feature collection with 1 feature and 1 field
## Geometry type: POLYGON
## Dimension:     XY
## Bounding box:  xmin: -74.11219 ymin: 7.925401 xmax: -73.9893 ymax: 7.996525
## Geodetic CRS:  WGS 84

##Búsqueda de la imagen Sentinel-2

s = stac("https://earth-search.aws.element84.com/v0")
items <- s |>
  stac_search(collections = "sentinel-s2-l2a-cogs",
              bbox = c (st_bbox(Santa_Rosa_del_Sur)["xmin"],
                        st_bbox(Santa_Rosa_del_Sur)["ymin"],
                        st_bbox(Santa_Rosa_del_Sur)["xmax"],
                        st_bbox(Santa_Rosa_del_Sur)["ymax"]),
              datetime = "2025-01-01/2025-12-31",
              limit = 60) |>
  post_request()
items
## ###Items
## - matched feature(s): 118
## - features (60 item(s) / 58 not fetched):
##   - S2A_18NWP_20251210_0_L2A
##   - S2A_18NXP_20251210_0_L2A
##   - S2C_18NWP_20251208_0_L2A
##   - S2C_18NXP_20251208_0_L2A
##   - S2B_18NWP_20251203_0_L2A
##   - S2B_18NXP_20251203_0_L2A
##   - S2A_18NWP_20251130_0_L2A
##   - S2A_18NXP_20251130_0_L2A
##   - S2C_18NWP_20251128_0_L2A
##   - S2C_18NXP_20251128_0_L2A
##   - ... with 50 more feature(s).
## - assets: 
## AOT, B01, B02, B03, B04, B05, B06, B07, B08, B09, B11, B12, B8A, info, metadata, overview, SCL, thumbnail, visual, WVP
## - item's fields: 
## assets, bbox, collection, geometry, id, links, properties, stac_extensions, stac_version, type
range(sapply(items$features, function(x) {x$properties$datetime}))
## [1] "2025-09-09T15:31:13Z" "2025-12-10T15:31:15Z"

##Creación de la colección de Sentinel-2

assets = c("B01","B02","B03","B04","B05","B06", "B07","B08","B8A","B09","B11","SCL")
s2_collection = stac_image_collection(items$features, asset_names = assets, property_filter = function(x) {x[["eo:cloud_cover"]] < 20}) 
s2_collection
## Image collection object, referencing 2 images with 12 bands
## Images:
##                       name      left      top   bottom     right
## 1 S2C_18NWP_20250909_0_L2A -75.00017 8.142025 7.147753 -74.00352
## 2 S2C_18NXP_20250909_0_L2A -74.09436 8.141010 7.144911 -73.09588
##              datetime        srs
## 1 2025-09-09T15:31:16 EPSG:32618
## 2 2025-09-09T15:31:13 EPSG:32618
## 
## Bands:
##    name offset scale unit nodata image_count
## 1   B01      0     1                       2
## 2   B02      0     1                       2
## 3   B03      0     1                       2
## 4   B04      0     1                       2
## 5   B05      0     1                       2
## 6   B06      0     1                       2
## 7   B07      0     1                       2
## 8   B08      0     1                       2
## 9   B09      0     1                       2
## 10  B11      0     1                       2
## 11  B8A      0     1                       2
## 12  SCL      0     1                       2
length(s2_collection)
## [1] 1
length(items$features)
## [1] 60
items$features[[1]]$properties$`eo:cloud_cover`
## [1] 32.35
sapply(items$features, function(x) x$properties$`eo:cloud_cover`) |> summary()
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##   19.83   44.85   68.43   67.68   92.38   99.93

##Creación y procesamiento de data cubes

(Santa_Rosa_del_Sur_box = st_bbox(Santa_Rosa_del_Sur))
##       xmin       ymin       xmax       ymax 
## -74.112190   7.925401 -73.989297   7.996525
(Santa_Rosa_del_Sur_b9377 <- st_bbox (st_transform(Santa_Rosa_del_Sur, "EPSG:9377")))
##    xmin    ymin    xmax    ymax 
## 4877478 2433913 4890999 2441741
gdalcubes_options(parallel = 8)
(v = cube_view(srs="EPSG:9377", dx=10, dy=10, dt="P1M", 
                           aggregation="median", resampling = "average",
                           extent=list(t0 = "2025-01-01", t1 = "2025-12-31",
                           
                           left=Santa_Rosa_del_Sur_b9377 ["xmin"], right=Santa_Rosa_del_Sur_b9377 ["xmax"],
                           top=Santa_Rosa_del_Sur_b9377 ["ymax"],
                           bottom=Santa_Rosa_del_Sur_b9377 ["ymin"])))
## A data cube view object
## 
## Dimensions:
##                low             high count pixel_size
## t       2025-01-01       2025-12-31    12        P1M
## y 2433912.05432737 2441742.05432737   783         10
## x 4877473.59621138 4891003.59621138  1353         10
## 
## SRS: "EPSG:9377"
## Temporal aggregation method: "median"
## Spatial resampling method: "average"

##Visualización del data cube

raster_cube(s2_collection, v) |>
  select_bands(c("B02","B03","B04")) |>
  reduce_time(c("median(B02)", "median(B03)", "median(B04)")) |>
  plot(rgb = 3:1, zlim = c(0,2500))

raster_cube(s2_collection, v) |>
  select_bands(c("B04","B11","B08")) |>
  reduce_time(c("median(B04)", "median(B11)", "median(B08)")) |>
  plot(rgb = 3:1, zlim = c(0,4500))

##Descarga de los datos

tutorialDir = path.expand("./Santa_Rosa_del_Sur/")
if (!file.exists ((file.path(tutorialDir, "Santa_Rosa_del_Sur_10"))))
{
  s2.mask = image_mask ("SCL", values = c (8,9))
  gdalcubes_options(parallel = 4, ncdf_compression_level = 0)
  raster_cube(s2_collection, v, mask = s2.mask) |>
    write_tif(file.path(tutorialDir, "Santa_Rosa_del_Sur_10.tif"))
}
list.files("Santa_Rosa_del_Sur", recursive = TRUE)
##   [1] "Santa_Rosa_del_Sur_10.tif/cube_1790a1f15d22025-01-01.tif" 
##   [2] "Santa_Rosa_del_Sur_10.tif/cube_1790a1f15d22025-02-01.tif" 
##   [3] "Santa_Rosa_del_Sur_10.tif/cube_1790a1f15d22025-03-01.tif" 
##   [4] "Santa_Rosa_del_Sur_10.tif/cube_1790a1f15d22025-04-01.tif" 
##   [5] "Santa_Rosa_del_Sur_10.tif/cube_1790a1f15d22025-05-01.tif" 
##   [6] "Santa_Rosa_del_Sur_10.tif/cube_1790a1f15d22025-06-01.tif" 
##   [7] "Santa_Rosa_del_Sur_10.tif/cube_1790a1f15d22025-07-01.tif" 
##   [8] "Santa_Rosa_del_Sur_10.tif/cube_1790a1f15d22025-08-01.tif" 
##   [9] "Santa_Rosa_del_Sur_10.tif/cube_1790a1f15d22025-09-01.tif" 
##  [10] "Santa_Rosa_del_Sur_10.tif/cube_1790a1f15d22025-10-01.tif" 
##  [11] "Santa_Rosa_del_Sur_10.tif/cube_1790a1f15d22025-11-01.tif" 
##  [12] "Santa_Rosa_del_Sur_10.tif/cube_1790a1f15d22025-12-01.tif" 
##  [13] "Santa_Rosa_del_Sur_10.tif/cube_21a4122a6b112025-01-01.tif"
##  [14] "Santa_Rosa_del_Sur_10.tif/cube_21a4122a6b112025-02-01.tif"
##  [15] "Santa_Rosa_del_Sur_10.tif/cube_21a4122a6b112025-03-01.tif"
##  [16] "Santa_Rosa_del_Sur_10.tif/cube_21a4122a6b112025-04-01.tif"
##  [17] "Santa_Rosa_del_Sur_10.tif/cube_21a4122a6b112025-05-01.tif"
##  [18] "Santa_Rosa_del_Sur_10.tif/cube_21a4122a6b112025-06-01.tif"
##  [19] "Santa_Rosa_del_Sur_10.tif/cube_21a4122a6b112025-07-01.tif"
##  [20] "Santa_Rosa_del_Sur_10.tif/cube_21a4122a6b112025-08-01.tif"
##  [21] "Santa_Rosa_del_Sur_10.tif/cube_21a4122a6b112025-09-01.tif"
##  [22] "Santa_Rosa_del_Sur_10.tif/cube_21a4122a6b112025-10-01.tif"
##  [23] "Santa_Rosa_del_Sur_10.tif/cube_21a4122a6b112025-11-01.tif"
##  [24] "Santa_Rosa_del_Sur_10.tif/cube_21a4122a6b112025-12-01.tif"
##  [25] "Santa_Rosa_del_Sur_10.tif/cube_21a4541993a2025-01-01.tif" 
##  [26] "Santa_Rosa_del_Sur_10.tif/cube_21a4541993a2025-02-01.tif" 
##  [27] "Santa_Rosa_del_Sur_10.tif/cube_21a4541993a2025-03-01.tif" 
##  [28] "Santa_Rosa_del_Sur_10.tif/cube_21a4541993a2025-04-01.tif" 
##  [29] "Santa_Rosa_del_Sur_10.tif/cube_21a4541993a2025-05-01.tif" 
##  [30] "Santa_Rosa_del_Sur_10.tif/cube_21a4541993a2025-06-01.tif" 
##  [31] "Santa_Rosa_del_Sur_10.tif/cube_21a4541993a2025-07-01.tif" 
##  [32] "Santa_Rosa_del_Sur_10.tif/cube_21a4541993a2025-08-01.tif" 
##  [33] "Santa_Rosa_del_Sur_10.tif/cube_21a4541993a2025-09-01.tif" 
##  [34] "Santa_Rosa_del_Sur_10.tif/cube_21a4541993a2025-10-01.tif" 
##  [35] "Santa_Rosa_del_Sur_10.tif/cube_21a4541993a2025-11-01.tif" 
##  [36] "Santa_Rosa_del_Sur_10.tif/cube_21a4541993a2025-12-01.tif" 
##  [37] "Santa_Rosa_del_Sur_10.tif/cube_21a4640821cc2025-01-01.tif"
##  [38] "Santa_Rosa_del_Sur_10.tif/cube_21a4640821cc2025-02-01.tif"
##  [39] "Santa_Rosa_del_Sur_10.tif/cube_21a4640821cc2025-03-01.tif"
##  [40] "Santa_Rosa_del_Sur_10.tif/cube_21a4640821cc2025-04-01.tif"
##  [41] "Santa_Rosa_del_Sur_10.tif/cube_21a4640821cc2025-05-01.tif"
##  [42] "Santa_Rosa_del_Sur_10.tif/cube_21a4640821cc2025-06-01.tif"
##  [43] "Santa_Rosa_del_Sur_10.tif/cube_21a4640821cc2025-07-01.tif"
##  [44] "Santa_Rosa_del_Sur_10.tif/cube_21a4640821cc2025-08-01.tif"
##  [45] "Santa_Rosa_del_Sur_10.tif/cube_21a4640821cc2025-09-01.tif"
##  [46] "Santa_Rosa_del_Sur_10.tif/cube_21a4640821cc2025-10-01.tif"
##  [47] "Santa_Rosa_del_Sur_10.tif/cube_21a4640821cc2025-11-01.tif"
##  [48] "Santa_Rosa_del_Sur_10.tif/cube_21a4640821cc2025-12-01.tif"
##  [49] "Santa_Rosa_del_Sur_10.tif/cube_2514354b6862025-01-01.tif" 
##  [50] "Santa_Rosa_del_Sur_10.tif/cube_2514354b6862025-02-01.tif" 
##  [51] "Santa_Rosa_del_Sur_10.tif/cube_2514354b6862025-03-01.tif" 
##  [52] "Santa_Rosa_del_Sur_10.tif/cube_2514354b6862025-04-01.tif" 
##  [53] "Santa_Rosa_del_Sur_10.tif/cube_2514354b6862025-05-01.tif" 
##  [54] "Santa_Rosa_del_Sur_10.tif/cube_2514354b6862025-06-01.tif" 
##  [55] "Santa_Rosa_del_Sur_10.tif/cube_2514354b6862025-07-01.tif" 
##  [56] "Santa_Rosa_del_Sur_10.tif/cube_26843385baa2025-01-01.tif" 
##  [57] "Santa_Rosa_del_Sur_10.tif/cube_26843385baa2025-02-01.tif" 
##  [58] "Santa_Rosa_del_Sur_10.tif/cube_26843385baa2025-03-01.tif" 
##  [59] "Santa_Rosa_del_Sur_10.tif/cube_26843385baa2025-04-01.tif" 
##  [60] "Santa_Rosa_del_Sur_10.tif/cube_26843385baa2025-05-01.tif" 
##  [61] "Santa_Rosa_del_Sur_10.tif/cube_26843385baa2025-06-01.tif" 
##  [62] "Santa_Rosa_del_Sur_10.tif/cube_26843385baa2025-07-01.tif" 
##  [63] "Santa_Rosa_del_Sur_10.tif/cube_26843385baa2025-08-01.tif" 
##  [64] "Santa_Rosa_del_Sur_10.tif/cube_26843385baa2025-09-01.tif" 
##  [65] "Santa_Rosa_del_Sur_10.tif/cube_26843385baa2025-10-01.tif" 
##  [66] "Santa_Rosa_del_Sur_10.tif/cube_26843385baa2025-11-01.tif" 
##  [67] "Santa_Rosa_del_Sur_10.tif/cube_26843385baa2025-12-01.tif" 
##  [68] "Santa_Rosa_del_Sur_10.tif/cube_2ff87b8813ca2025-01-01.tif"
##  [69] "Santa_Rosa_del_Sur_10.tif/cube_2ff87b8813ca2025-02-01.tif"
##  [70] "Santa_Rosa_del_Sur_10.tif/cube_2ff87b8813ca2025-03-01.tif"
##  [71] "Santa_Rosa_del_Sur_10.tif/cube_2ff87b8813ca2025-04-01.tif"
##  [72] "Santa_Rosa_del_Sur_10.tif/cube_2ff87b8813ca2025-05-01.tif"
##  [73] "Santa_Rosa_del_Sur_10.tif/cube_2ff87b8813ca2025-06-01.tif"
##  [74] "Santa_Rosa_del_Sur_10.tif/cube_2ff87b8813ca2025-07-01.tif"
##  [75] "Santa_Rosa_del_Sur_10.tif/cube_2ff87b8813ca2025-08-01.tif"
##  [76] "Santa_Rosa_del_Sur_10.tif/cube_2ff87b8813ca2025-09-01.tif"
##  [77] "Santa_Rosa_del_Sur_10.tif/cube_2ff87b8813ca2025-10-01.tif"
##  [78] "Santa_Rosa_del_Sur_10.tif/cube_2ff87b8813ca2025-11-01.tif"
##  [79] "Santa_Rosa_del_Sur_10.tif/cube_2ff87b8813ca2025-12-01.tif"
##  [80] "Santa_Rosa_del_Sur_10.tif/cube_338c161640822025-01-01.tif"
##  [81] "Santa_Rosa_del_Sur_10.tif/cube_338c161640822025-02-01.tif"
##  [82] "Santa_Rosa_del_Sur_10.tif/cube_338c161640822025-03-01.tif"
##  [83] "Santa_Rosa_del_Sur_10.tif/cube_338c161640822025-04-01.tif"
##  [84] "Santa_Rosa_del_Sur_10.tif/cube_338c161640822025-05-01.tif"
##  [85] "Santa_Rosa_del_Sur_10.tif/cube_338c161640822025-06-01.tif"
##  [86] "Santa_Rosa_del_Sur_10.tif/cube_338c161640822025-07-01.tif"
##  [87] "Santa_Rosa_del_Sur_10.tif/cube_338c1a6774222025-01-01.tif"
##  [88] "Santa_Rosa_del_Sur_10.tif/cube_338c1a6774222025-02-01.tif"
##  [89] "Santa_Rosa_del_Sur_10.tif/cube_338c1a6774222025-03-01.tif"
##  [90] "Santa_Rosa_del_Sur_10.tif/cube_338c1a6774222025-04-01.tif"
##  [91] "Santa_Rosa_del_Sur_10.tif/cube_338c1a6774222025-05-01.tif"
##  [92] "Santa_Rosa_del_Sur_10.tif/cube_338c1a6774222025-06-01.tif"
##  [93] "Santa_Rosa_del_Sur_10.tif/cube_338c1a6774222025-07-01.tif"
##  [94] "Santa_Rosa_del_Sur_10.tif/cube_338c1a6774222025-08-01.tif"
##  [95] "Santa_Rosa_del_Sur_10.tif/cube_338c1a6774222025-09-01.tif"
##  [96] "Santa_Rosa_del_Sur_10.tif/cube_338c1a6774222025-10-01.tif"
##  [97] "Santa_Rosa_del_Sur_10.tif/cube_338c1a6774222025-11-01.tif"
##  [98] "Santa_Rosa_del_Sur_10.tif/cube_338c1a6774222025-12-01.tif"
##  [99] "Santa_Rosa_del_Sur_10.tif/cube_338c2ec16a42025-01-01.tif" 
## [100] "Santa_Rosa_del_Sur_10.tif/cube_338c2ec16a42025-02-01.tif" 
## [101] "Santa_Rosa_del_Sur_10.tif/cube_338c2ec16a42025-03-01.tif" 
## [102] "Santa_Rosa_del_Sur_10.tif/cube_338c2ec16a42025-04-01.tif" 
## [103] "Santa_Rosa_del_Sur_10.tif/cube_338c2ec16a42025-05-01.tif" 
## [104] "Santa_Rosa_del_Sur_10.tif/cube_338c2ec16a42025-06-01.tif" 
## [105] "Santa_Rosa_del_Sur_10.tif/cube_338c2ec16a42025-07-01.tif" 
## [106] "Santa_Rosa_del_Sur_10.tif/cube_338c2ec16a42025-08-01.tif" 
## [107] "Santa_Rosa_del_Sur_10.tif/cube_338c2ec16a42025-09-01.tif" 
## [108] "Santa_Rosa_del_Sur_10.tif/cube_338c2ec16a42025-10-01.tif" 
## [109] "Santa_Rosa_del_Sur_10.tif/cube_338c2ec16a42025-11-01.tif" 
## [110] "Santa_Rosa_del_Sur_10.tif/cube_338c2ec16a42025-12-01.tif" 
## [111] "Santa_Rosa_del_Sur_10.tif/cube_338c3ebc12da2025-01-01.tif"
## [112] "Santa_Rosa_del_Sur_10.tif/cube_338c3ebc12da2025-02-01.tif"
## [113] "Santa_Rosa_del_Sur_10.tif/cube_338c3ebc12da2025-03-01.tif"
## [114] "Santa_Rosa_del_Sur_10.tif/cube_338c3ebc12da2025-04-01.tif"
## [115] "Santa_Rosa_del_Sur_10.tif/cube_338c3ebc12da2025-05-01.tif"
## [116] "Santa_Rosa_del_Sur_10.tif/cube_338c3ebc12da2025-06-01.tif"
## [117] "Santa_Rosa_del_Sur_10.tif/cube_338c3ebc12da2025-07-01.tif"
## [118] "Santa_Rosa_del_Sur_10.tif/cube_338c3ebc12da2025-08-01.tif"
## [119] "Santa_Rosa_del_Sur_10.tif/cube_338c3ebc12da2025-09-01.tif"
## [120] "Santa_Rosa_del_Sur_10.tif/cube_338c3ebc12da2025-10-01.tif"
## [121] "Santa_Rosa_del_Sur_10.tif/cube_338c3ebc12da2025-11-01.tif"
## [122] "Santa_Rosa_del_Sur_10.tif/cube_338c3ebc12da2025-12-01.tif"
## [123] "Santa_Rosa_del_Sur_10.tif/cube_338c4e9647e42025-01-01.tif"
## [124] "Santa_Rosa_del_Sur_10.tif/cube_338c4e9647e42025-02-01.tif"
## [125] "Santa_Rosa_del_Sur_10.tif/cube_338c4e9647e42025-03-01.tif"
## [126] "Santa_Rosa_del_Sur_10.tif/cube_338c4e9647e42025-04-01.tif"
## [127] "Santa_Rosa_del_Sur_10.tif/cube_338c4e9647e42025-05-01.tif"
## [128] "Santa_Rosa_del_Sur_10.tif/cube_338c4e9647e42025-06-01.tif"
## [129] "Santa_Rosa_del_Sur_10.tif/cube_338c4e9647e42025-07-01.tif"
## [130] "Santa_Rosa_del_Sur_10.tif/cube_338c4e9647e42025-08-01.tif"
## [131] "Santa_Rosa_del_Sur_10.tif/cube_338c4e9647e42025-09-01.tif"
## [132] "Santa_Rosa_del_Sur_10.tif/cube_338c4e9647e42025-10-01.tif"
## [133] "Santa_Rosa_del_Sur_10.tif/cube_338c4e9647e42025-11-01.tif"
## [134] "Santa_Rosa_del_Sur_10.tif/cube_338c4e9647e42025-12-01.tif"
## [135] "Santa_Rosa_del_Sur_10.tif/cube_338c624d684c2025-01-01.tif"
## [136] "Santa_Rosa_del_Sur_10.tif/cube_338c624d684c2025-02-01.tif"
## [137] "Santa_Rosa_del_Sur_10.tif/cube_338c624d684c2025-03-01.tif"
## [138] "Santa_Rosa_del_Sur_10.tif/cube_338c624d684c2025-04-01.tif"
## [139] "Santa_Rosa_del_Sur_10.tif/cube_338c624d684c2025-05-01.tif"
## [140] "Santa_Rosa_del_Sur_10.tif/cube_338c624d684c2025-06-01.tif"
## [141] "Santa_Rosa_del_Sur_10.tif/cube_338c624d684c2025-07-01.tif"
## [142] "Santa_Rosa_del_Sur_10.tif/cube_338c624d684c2025-08-01.tif"
## [143] "Santa_Rosa_del_Sur_10.tif/cube_338c624d684c2025-09-01.tif"
## [144] "Santa_Rosa_del_Sur_10.tif/cube_338c624d684c2025-10-01.tif"
## [145] "Santa_Rosa_del_Sur_10.tif/cube_338c624d684c2025-11-01.tif"
## [146] "Santa_Rosa_del_Sur_10.tif/cube_338c624d684c2025-12-01.tif"
## [147] "Santa_Rosa_del_Sur_10.tif/cube_338c6c55532f2025-01-01.tif"
## [148] "Santa_Rosa_del_Sur_10.tif/cube_338c6c55532f2025-02-01.tif"
## [149] "Santa_Rosa_del_Sur_10.tif/cube_338c6c55532f2025-03-01.tif"
## [150] "Santa_Rosa_del_Sur_10.tif/cube_338c6c55532f2025-04-01.tif"
## [151] "Santa_Rosa_del_Sur_10.tif/cube_338c6c55532f2025-05-01.tif"
## [152] "Santa_Rosa_del_Sur_10.tif/cube_338c6c55532f2025-06-01.tif"
## [153] "Santa_Rosa_del_Sur_10.tif/cube_338c6c55532f2025-07-01.tif"
## [154] "Santa_Rosa_del_Sur_10.tif/cube_338c6c55532f2025-08-01.tif"
## [155] "Santa_Rosa_del_Sur_10.tif/cube_338c6c55532f2025-09-01.tif"
## [156] "Santa_Rosa_del_Sur_10.tif/cube_338c6c55532f2025-10-01.tif"
## [157] "Santa_Rosa_del_Sur_10.tif/cube_338c6c55532f2025-11-01.tif"
## [158] "Santa_Rosa_del_Sur_10.tif/cube_338c6c55532f2025-12-01.tif"
## [159] "Santa_Rosa_del_Sur_10.tif/cube_338c73ef62f72025-01-01.tif"
## [160] "Santa_Rosa_del_Sur_10.tif/cube_338c73ef62f72025-02-01.tif"
## [161] "Santa_Rosa_del_Sur_10.tif/cube_338c73ef62f72025-03-01.tif"
## [162] "Santa_Rosa_del_Sur_10.tif/cube_338c73ef62f72025-04-01.tif"
## [163] "Santa_Rosa_del_Sur_10.tif/cube_338c73ef62f72025-05-01.tif"
## [164] "Santa_Rosa_del_Sur_10.tif/cube_338c73ef62f72025-06-01.tif"
## [165] "Santa_Rosa_del_Sur_10.tif/cube_338c73ef62f72025-07-01.tif"
## [166] "Santa_Rosa_del_Sur_10.tif/cube_338c73ef62f72025-08-01.tif"
## [167] "Santa_Rosa_del_Sur_10.tif/cube_338c73ef62f72025-09-01.tif"
## [168] "Santa_Rosa_del_Sur_10.tif/cube_338c73ef62f72025-10-01.tif"
## [169] "Santa_Rosa_del_Sur_10.tif/cube_338c73ef62f72025-11-01.tif"
## [170] "Santa_Rosa_del_Sur_10.tif/cube_338c73ef62f72025-12-01.tif"
## [171] "Santa_Rosa_del_Sur_10.tif/cube_53545de09db2025-01-01.tif" 
## [172] "Santa_Rosa_del_Sur_10.tif/cube_53545de09db2025-02-01.tif" 
## [173] "Santa_Rosa_del_Sur_10.tif/cube_53545de09db2025-03-01.tif" 
## [174] "Santa_Rosa_del_Sur_10.tif/cube_53545de09db2025-04-01.tif" 
## [175] "Santa_Rosa_del_Sur_10.tif/cube_53545de09db2025-05-01.tif" 
## [176] "Santa_Rosa_del_Sur_10.tif/cube_53545de09db2025-06-01.tif" 
## [177] "Santa_Rosa_del_Sur_10.tif/cube_53545de09db2025-07-01.tif" 
## [178] "Santa_Rosa_del_Sur_10.tif/cube_53545de09db2025-08-01.tif" 
## [179] "Santa_Rosa_del_Sur_10.tif/cube_53545de09db2025-09-01.tif" 
## [180] "Santa_Rosa_del_Sur_10.tif/cube_53545de09db2025-10-01.tif" 
## [181] "Santa_Rosa_del_Sur_10.tif/cube_53545de09db2025-11-01.tif" 
## [182] "Santa_Rosa_del_Sur_10.tif/cube_53545de09db2025-12-01.tif" 
## [183] "Santa_Rosa_del_Sur_10.tif/cube_63983ac96ee82025-01-01.tif"
## [184] "Santa_Rosa_del_Sur_10.tif/cube_63983ac96ee82025-02-01.tif"
## [185] "Santa_Rosa_del_Sur_10.tif/cube_63983ac96ee82025-03-01.tif"
## [186] "Santa_Rosa_del_Sur_10.tif/cube_63983ac96ee82025-04-01.tif"
## [187] "Santa_Rosa_del_Sur_10.tif/cube_63983ac96ee82025-05-01.tif"
## [188] "Santa_Rosa_del_Sur_10.tif/cube_63983ac96ee82025-06-01.tif"
## [189] "Santa_Rosa_del_Sur_10.tif/cube_63983ac96ee82025-07-01.tif"
## [190] "Santa_Rosa_del_Sur_10.tif/cube_63983ac96ee82025-08-01.tif"
## [191] "Santa_Rosa_del_Sur_10.tif/cube_63983ac96ee82025-09-01.tif"
## [192] "Santa_Rosa_del_Sur_10.tif/cube_63983ac96ee82025-10-01.tif"
## [193] "Santa_Rosa_del_Sur_10.tif/cube_63983ac96ee82025-11-01.tif"
## [194] "Santa_Rosa_del_Sur_10.tif/cube_63983ac96ee82025-12-01.tif"
archivos <- list.files(
  "Santa_Rosa_del_Sur",
  pattern = "\\.tif$",
  full.names = TRUE
)

archivos
## [1] "Santa_Rosa_del_Sur/Santa_Rosa_del_Sur_10.tif"

##Computación y visualización del Índice de Vegetación

f <- "Santa_Rosa_del_Sur/Santa_Rosa_del_Sur_10.tif/cube_21a4122a6b112025-01-01.tif"

Santa_Rosa_del_Sur_s2 <- terra::rast(f)
red <- Santa_Rosa_del_Sur_s2 [[4]]
nir <- Santa_Rosa_del_Sur_s2 [[8]]

ndvi <- (nir-red) / (nir+red)
ndvi
## class       : SpatRaster
## size        : 783, 1353, 1  (nrow, ncol, nlyr)
## resolution  : 10, 10  (x, y)
## extent      : 4877474, 4891004, 2433912, 2441742  (xmin, xmax, ymin, ymax)
## coord. ref. : MAGNA-SIRGAS 2018 / Origen-Nacional (EPSG:9377)
## source(s)   : memory
## varname     : cube_21a4122a6b112025-01-01
## name        : B08
## min value   : NaN
## max value   : NaN
ndvi_palette <- colorNumeric(
  palette = c("red", "yellow", "green"), 
  domain = c(-1, 1),
  na.color = "transparent" 
)


map <- leaflet() %>%
 
  addProviderTiles("Esri.WorldImagery", group = "Esri Satellite") %>%
 
  setView(lng = -76.0, lat = 4.50, zoom = 14) %>%
 
  addRasterImage(
    x = ndvi, 
    colors = ndvi_palette, 
    opacity = 0.6,          
    group = "NDVI Layer"    
  ) %>%
  
  
  addLegend(
    pal = ndvi_palette, 
    values = c(-1, 1), 
    title = "NDVI Index",
    position = "bottomright",
    opacity = 0.8
  ) %>%
  
 
  addLayersControl(
    baseGroups = c("Esri Satellite"),
    overlayGroups = c("NDVI Layer"),
    options = layersControlOptions(collapsed = FALSE)
  )
map