Apéndice D — Ubicación

Sitio experimental Valle del Mayo, 27°00’41.1”N 109°29’56.2”W.

Mostrar el código
library(leaflet)
library(leaflet.providers)
library(raster)
library("readxl")
library(RColorBrewer)

# Cargar la imagen TIFF georreferenciada
#raster_image <- raster("images/odm_orthophoto.tif")

#Reducir la resolución si es necesario
#raster_reduced <- aggregate(raster_image, fact=10)



#leaflet() %>%
#  addTiles() %>% # Add default OpenStreetMap map tiles
#  setView(-109.498944, 27.011417, zoom = 18)  %>%
#  addMarkers(lng=-109.498944, lat=27.011417, popup="Ubicación de parcela experimental")%>%
#  addRasterImage(raster_reduced, opacity = 1)

# Crear el mapa Leaflet y añadir la capa raster con los colores originales
#leaflet() %>%
#  addTiles(urlTemplate = 'http://{s}.google.com/vt/lyrs=s&x={x}&y={y}&z={z}', 
#           options = tileOptions(subdomains = c('mt0', 'mt1', 'mt2', 'mt3'), maxZoom = 20)) %>%
#  setView(-109.498944, 27.011417, zoom = 19) %>%
#  addMarkers(lng=-109.498944, lat=27.011417, popup="Ubicación de parcela experimental") %>%
#  addRasterImage(raster_reduced, opacity = 1)%>%
#  addMarkers(lng=-109.498944, lat=27.011417, popup="Ubicación de parcela experimental")%>% addCircleMarkers(data = Coordenadas2, radius = 2, stroke = FALSE, fillOpacity = 1.0)

# Función para reescalar valores de NDVI al rango -1 a 1
rescale_ndvi <- function(ndvi, old_min, old_max) {
  return((ndvi - old_min) / (old_max - old_min) * 2 - 1)
}

# Cargar los archivos TIFF de NDVI
ndvi_file1 <- raster("images/copernicus/2023-12-07-00_00_2023-12-07-23_59_Sentinel-2_L2A_NDVI.tiff")
ndvi_file2 <- raster("images/copernicus/2023-12-17-00_00_2023-12-17-23_59_Sentinel-2_L2A_NDVI.tiff")
ndvi_file3 <- raster("images/copernicus/2024-01-01-00_00_2024-01-01-23_59_Sentinel-2_L2A_NDVI.tiff")
ndvi_file4 <- raster("images/copernicus/2024-01-11-00_00_2024-01-11-23_59_Sentinel-2_L2A_NDVI.tiff")
ndvi_file5 <- raster("images/copernicus/2024-01-16-00_00_2024-01-16-23_59_Sentinel-2_L2A_NDVI.tiff")
ndvi_file6 <- raster("images/copernicus/2024-01-31-00_00_2024-01-31-23_59_Sentinel-2_L2A_NDVI.tiff")
ndvi_file7 <- raster("images/copernicus/2024-02-10-00_00_2024-02-10-23_59_Sentinel-2_L2A_NDVI.tiff")
ndvi_file8 <- raster("images/copernicus/2024-03-01-00_00_2024-03-01-23_59_Sentinel-2_L2A_NDVI.tiff")
ndvi_file9 <- raster("images/copernicus/2024-03-21-00_00_2024-03-21-23_59_Sentinel-2_L2A_NDVI.tiff")
ndvi_file10 <- raster("images/copernicus/2024-03-26-00_00_2024-03-26-23_59_Sentinel-2_L2A_NDVI.tiff")
ndvi_file11 <- raster("images/copernicus/2024-04-10-00_00_2024-04-10-23_59_Sentinel-2_L2A_NDVI.tiff")
ndvi_file12 <- raster("images/copernicus/2024-04-20-00_00_2024-04-20-23_59_Sentinel-2_L2A_NDVI.tiff")
ndvi_file13 <- raster("images/copernicus/2024-05-05-00_00_2024-05-05-23_59_Sentinel-2_L2A_NDVI.tiff")
ndvi_file14 <- raster("images/copernicus/2024-05-25-00_00_2024-05-25-23_59_Sentinel-2_L2A_NDVI.tiff")

# Verificar los valores mínimos y máximos
ndvi1_min <- cellStats(ndvi_file1, min)
ndvi1_max <- cellStats(ndvi_file1, max)
ndvi2_min <- cellStats(ndvi_file2, min)
ndvi2_max <- cellStats(ndvi_file2, max)
ndvi3_min <- cellStats(ndvi_file3, min)
ndvi3_max <- cellStats(ndvi_file3, max)
ndvi4_min <- cellStats(ndvi_file4, min)
ndvi4_max <- cellStats(ndvi_file4, max)
ndvi5_min <- cellStats(ndvi_file5, min)
ndvi5_max <- cellStats(ndvi_file5, max)
ndvi6_min <- cellStats(ndvi_file6, min)
ndvi6_max <- cellStats(ndvi_file6, max)
ndvi7_min <- cellStats(ndvi_file7, min)
ndvi7_max <- cellStats(ndvi_file7, max)
ndvi8_min <- cellStats(ndvi_file8, min)
ndvi8_max <- cellStats(ndvi_file8, max)
ndvi9_min <- cellStats(ndvi_file9, min)
ndvi9_max <- cellStats(ndvi_file9, max)
ndvi10_min <- cellStats(ndvi_file10, min)
ndvi10_max <- cellStats(ndvi_file10, max)
ndvi11_min <- cellStats(ndvi_file11, min)
ndvi11_max <- cellStats(ndvi_file11, max)
ndvi12_min <- cellStats(ndvi_file12, min)
ndvi12_max <- cellStats(ndvi_file12, max)
ndvi13_min <- cellStats(ndvi_file13, min)
ndvi13_max <- cellStats(ndvi_file13, max)
ndvi14_min <- cellStats(ndvi_file14, min)
ndvi14_max <- cellStats(ndvi_file14, max)

# Calcular el mínimo y máximo global
global_min <- min(ndvi1_min, ndvi2_min, ndvi3_min, ndvi4_min, ndvi5_min, ndvi6_min, ndvi7_min, ndvi8_min, ndvi9_min, ndvi10_min,ndvi11_min, ndvi12_min, ndvi13_min, ndvi14_min)
global_max <- max(ndvi1_max, ndvi2_max, ndvi3_max, ndvi4_max, ndvi5_max, ndvi6_max, ndvi7_max, ndvi8_max, ndvi9_max, ndvi10_max,ndvi11_max, ndvi12_max, ndvi13_max, ndvi14_max)

# Reescalar los valores de NDVI al rango 0 a 1
ndvi_file1_rescaled <- calc(ndvi_file1, fun = function(x) rescale_ndvi(x, global_min, global_max))
ndvi_file2_rescaled <- calc(ndvi_file2, fun = function(x) rescale_ndvi(x, global_min, global_max))
ndvi_file3_rescaled <- calc(ndvi_file3, fun = function(x) rescale_ndvi(x, global_min, global_max))
ndvi_file4_rescaled <- calc(ndvi_file4, fun = function(x) rescale_ndvi(x, global_min, global_max))
ndvi_file5_rescaled <- calc(ndvi_file5, fun = function(x) rescale_ndvi(x, global_min, global_max))
ndvi_file6_rescaled <- calc(ndvi_file6, fun = function(x) rescale_ndvi(x, global_min, global_max))
ndvi_file7_rescaled <- calc(ndvi_file7, fun = function(x) rescale_ndvi(x, global_min, global_max))
ndvi_file8_rescaled <- calc(ndvi_file8, fun = function(x) rescale_ndvi(x, global_min, global_max))
ndvi_file9_rescaled <- calc(ndvi_file9, fun = function(x) rescale_ndvi(x, global_min, global_max))
ndvi_file10_rescaled <- calc(ndvi_file10, fun = function(x) rescale_ndvi(x, global_min, global_max))
ndvi_file11_rescaled <- calc(ndvi_file11, fun = function(x) rescale_ndvi(x, global_min, global_max))
ndvi_file12_rescaled <- calc(ndvi_file12, fun = function(x) rescale_ndvi(x, global_min, global_max))
ndvi_file13_rescaled <- calc(ndvi_file13, fun = function(x) rescale_ndvi(x, global_min, global_max))
ndvi_file14_rescaled <- calc(ndvi_file14, fun = function(x) rescale_ndvi(x, global_min, global_max))



# Crear un color ramp para NDVI
ndvi_colors <- colorNumeric(palette = rev(brewer.pal(9, "YlGn")), domain = c(-1, 1))






# Leer los datos desde el archivo Excel

Coordenadas2 <- read_excel("csv/coordenadas2.xlsx")


#leaflet() %>%
#  addTiles(urlTemplate = 'http://{s}.google.com/vt/lyrs=s&x={x}&y={y}&z={z}', 
#           options = tileOptions(subdomains = c('mt0', 'mt1', 'mt2', 'mt3'), maxZoom = 20)) %>%
#  setView(-109.498944, 27.011417, zoom = 19) %>%
#  addMarkers(lng=-109.49856539748855, lat=27.011745229534263, popup="Ubicación de parcela experimental. Sitio experimental Valle del Mayo, 27°00’41.1”N 109°29’56.2”W.")%>% addCircleMarkers(data = Coordenadas2, radius = 4, stroke = FALSE, fillOpacity = 0.7)

#mapshot(Mapa, file = "Mapa.png")


# Definir las coordenadas del polígono (puedes cargarlas desde un archivo similar a Coordenadas2 si las tienes)
polygon_coords <- matrix(c(
  -109.49969415211532, 27.01173379269226,
  -109.49857565463655, 27.011749037495047,
  -109.49857599326661, 27.011210563348744,
  -109.49969772148300, 27.011202618008525,
  -109.49969415211532, 27.01173379269226  # Repetimos la primera coordenada para cerrar el polígono
), ncol = 2, byrow = TRUE)

# Coordenadas de la primera línea
line_coords <- matrix(c(
  -109.49962453569314, 27.011546617161286,
  -109.49862453569314, 27.011556617161286
), ncol = 2, byrow = TRUE)

# Coordenadas de la segunda línea
line_coords2 <- matrix(c(
  -109.49962453569314, 27.011371350701835,
  -109.49862053569314, 27.011381350701835
), ncol = 2, byrow = TRUE)

# Coordenadas de la primera línea
line_coords3 <- matrix(c(
  -109.49936453569314, 27.011743345045915,
  -109.49937881650642, 27.011199644294383
), ncol = 2, byrow = TRUE)

# Coordenadas de la primera línea
line_coords4 <- matrix(c(
  -109.49911453569314, 27.011743345045915,
  -109.49912881650642, 27.011199644294383
), ncol = 2, byrow = TRUE)

# Coordenadas de la primera línea
line_coords5 <- matrix(c(
  -109.49886453569314, 27.011743345045915,
  -109.49887881650642, 27.011199644294383
), ncol = 2, byrow = TRUE)

# Coordenadas de la primera línea
line_coords6 <- matrix(c(
  -109.49861453569314, 27.011743345045915,
  -109.49862881650642, 27.011210563348744
), ncol = 2, byrow = TRUE)

# Coordenadas de la primera línea
line_coords7 <- matrix(c(
  -109.49962453569314, 27.011733345045915,
  -109.49963881650642, 27.011200463348744
), ncol = 2, byrow = TRUE)


file <- 'https://onedrive.live.com/embed?resid=f592a8542a1012f2%21135730&authkey=%21ANRwJHKLJEHQzKM&width=6113&height=6113.svg'
style <- 'width:300px'

leaflet() %>%
  addTiles(urlTemplate = 'http://{s}.google.com/vt/lyrs=s&x={x}&y={y}&z={z}', 
           options = tileOptions(subdomains = c('mt0', 'mt1', 'mt2', 'mt3'), maxZoom = 20), group = "Google Maps") %>%
  setView(-109.498944, 27.011417, zoom = 19) %>%
  addMarkers(lng = -109.49856539748855, lat = 27.011745229534263, 
             popup = paste0("Ubicación de parcela experimental. Sitio experimental Valle del Mayo, 27°00’41.1”N 109°29’56.2”W.", "<img src = ", file," style=",style, ">"), 
             group = "Marcadores"
             ) %>%
  addPolygons(lng = polygon_coords[,1], lat = polygon_coords[,2], 
              color = "red", weight = 4, fillColor = "transparent", group = "Parcela Experimental") %>%
  addCircleMarkers(data = Coordenadas2, radius = 4, stroke = FALSE, color = "yellow", fillOpacity = 0.7, label = ~parcela, popup = ~parcela, group = "Áreas de Muestreo") %>%
  addRasterImage(ndvi_file1_rescaled, colors = ndvi_colors, opacity = 1, group = "NDVI 2023-12-07") %>%
  addRasterImage(ndvi_file2_rescaled, colors = ndvi_colors, opacity = 1, group = "NDVI 2023-12-17") %>%
  addRasterImage(ndvi_file3_rescaled, colors = ndvi_colors, opacity = 1, group = "NDVI 2024-01-01") %>%
  addRasterImage(ndvi_file4_rescaled, colors = ndvi_colors, opacity = 1, group = "NDVI 2024-01-11") %>%
  addRasterImage(ndvi_file5_rescaled, colors = ndvi_colors, opacity = 1, group = "NDVI 2024-01-16") %>%
  addRasterImage(ndvi_file6_rescaled, colors = ndvi_colors, opacity = 1, group = "NDVI 2024-01-31") %>%
  addRasterImage(ndvi_file7_rescaled, colors = ndvi_colors, opacity = 1, group = "NDVI 2024-02-10") %>%
  addRasterImage(ndvi_file8_rescaled, colors = ndvi_colors, opacity = 1, group = "NDVI 2024-03-01") %>%
  addRasterImage(ndvi_file9_rescaled, colors = ndvi_colors, opacity = 1, group = "NDVI 2024-03-21") %>%
  addRasterImage(ndvi_file10_rescaled, colors = ndvi_colors, opacity = 1, group = "NDVI 2024-03-26") %>%
  addRasterImage(ndvi_file11_rescaled, colors = ndvi_colors, opacity = 1, group = "NDVI 2024-04-10") %>%
  addRasterImage(ndvi_file12_rescaled, colors = ndvi_colors, opacity = 1, group = "NDVI 2024-04-20") %>%
  addRasterImage(ndvi_file13_rescaled, colors = ndvi_colors, opacity = 1, group = "NDVI 2024-05-05") %>%
  addRasterImage(ndvi_file14_rescaled, colors = ndvi_colors, opacity = 1, group = "NDVI 2024-05-25") %>%
  addLayersControl(
    baseGroups = c("Google Maps", "NDVI 2023-12-07", "NDVI 2023-12-17", "NDVI 2024-01-01", "NDVI 2024-01-11", "NDVI 2024-01-16", "NDVI 2024-01-31", "NDVI 2024-02-10", "NDVI 2024-03-01", "NDVI 2024-03-21", "NDVI 2024-03-26", "NDVI 2024-04-10", "NDVI 2024-04-20", "NDVI 2024-05-05", "NDVI 2024-05-25"),
    overlayGroups = c("Áreas de Muestreo", "Parcela Experimental", "Bloques"),
    options = layersControlOptions(collapsed = FALSE)
  ) %>%
  addLegend(pal = ndvi_colors, values = c(-1, 1), title = "NDVI", position = "bottomleft")  %>%
  addLegend(position = "bottomright", 
            colors = c("red", "blue", "yellow"), 
            labels = c("Parcela Experimental", "Bloques","Áreas de Muestreo"), 
            opacity = 1) %>%
  addMeasure(position = "bottomleft", primaryLengthUnit = "meters",secondaryLengthUnit = "kilometers",primaryAreaUnit = "hectares", localization = "es")%>%
  addPolylines(lng = line_coords[,1], lat = line_coords[,2], color = "blue", weight = 4, group = "Bloques")%>%
  addPolylines(lng = line_coords2[,1], lat = line_coords2[,2], color = "blue", weight = 4, group = "Bloques")%>%
  addPolylines(lng = line_coords3[,1], lat = line_coords3[,2], color = "blue", weight = 4, group = "Bloques")%>%
  addPolylines(lng = line_coords4[,1], lat = line_coords4[,2], color = "blue", weight = 4, group = "Bloques")%>%
  addPolylines(lng = line_coords5[,1], lat = line_coords5[,2], color = "blue", weight = 4, group = "Bloques")%>%
  addPolylines(lng = line_coords6[,1], lat = line_coords6[,2], color = "red", weight = 4, group = "Parcela Experimental")%>%
  addPolylines(lng = line_coords7[,1], lat = line_coords7[,2], color = "red", weight = 4, group = "Parcela Experimental")
Figura D.1: Leaflet.