# GEOVISUALIZADOR DEL ÁREA NATURAL PROTEGIDA NEVADO DE TOLUCA

# 1. LIBRERÍAS

library(sf)
## Linking to GEOS 3.14.1, GDAL 3.12.1, PROJ 9.7.1; sf_use_s2() is TRUE
library(sp)
library(leaflet)
library(leafem)
library(ggplot2)
library(dplyr)
## 
## Adjuntando el paquete: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(terra)
## terra 1.9.27
library(htmlwidgets)


# 2. CARGA DE SHAPEFILES


ANP <- st_read("C:/FINAL SIG II/ANP Nevado de Toluca.shp")
## Reading layer `ANP Nevado de Toluca' from data source 
##   `C:\FINAL SIG II\ANP Nevado de Toluca.shp' using driver `ESRI Shapefile'
## Simple feature collection with 1 feature and 14 fields
## Geometry type: POLYGON
## Dimension:     XY
## Bounding box:  xmin: 399803 ymin: 2102602 xmax: 432223 ymax: 2134903
## Projected CRS: WGS 84 / UTM zone 14N
Municipios <- st_read("C:/FINAL SIG II/Municipios.shp")
## Reading layer `Municipios' from data source `C:\FINAL SIG II\Municipios.shp' using driver `ESRI Shapefile'
## Simple feature collection with 9 features and 7 fields
## Geometry type: POLYGON
## Dimension:     XY
## Bounding box:  xmin: 2684840 ymin: 758403.4 xmax: 2759661 ymax: 840660.8
## Projected CRS: Mex_INEGI_Lambert_Conformal_Conic
Localidades <- st_read("C:/FINAL SIG II/localidades.shp")
## Reading layer `localidades' from data source `C:\FINAL SIG II\localidades.shp' using driver `ESRI Shapefile'
## Simple feature collection with 13 features and 6 fields
## Geometry type: MULTIPOLYGON
## Dimension:     XY
## Bounding box:  xmin: 2717487 ymin: 788965.4 xmax: 2745352 ymax: 813620.7
## Projected CRS: MEXICO_ITRF_2008_LCC
UsoSuelo <- st_read("C:/FINAL SIG II/Uso de suelo.shp")
## Reading layer `Uso de suelo' from data source `C:\FINAL SIG II\Uso de suelo.shp' using driver `ESRI Shapefile'
## Simple feature collection with 84 features and 4 fields
## Geometry type: MULTIPOLYGON
## Dimension:     XY
## Bounding box:  xmin: 2714577 ymin: 781774.5 xmax: 2747183 ymax: 813722
## Projected CRS: LCC_WGS_1984
Vegetacion <- st_read("C:/FINAL SIG II/Vegetación.shp")
## Reading layer `Vegetación' from data source `C:\FINAL SIG II\Vegetación.shp' using driver `ESRI Shapefile'
## Simple feature collection with 84 features and 4 fields
## Geometry type: MULTIPOLYGON
## Dimension:     XY
## Bounding box:  xmin: 2714577 ymin: 781774.5 xmax: 2747183 ymax: 813722
## Projected CRS: LCC_WGS_1984
Cobertura <- st_read("C:/FINAL SIG II/Cobertura arborea.shp")
## Reading layer `Cobertura arborea' from data source 
##   `C:\FINAL SIG II\Cobertura arborea.shp' using driver `ESRI Shapefile'
## Simple feature collection with 84 features and 4 fields
## Geometry type: MULTIPOLYGON
## Dimension:     XY
## Bounding box:  xmin: 2714577 ymin: 781774.5 xmax: 2747183 ymax: 813722
## Projected CRS: LCC_WGS_1984
Hidrologia <- st_read("C:/FINAL SIG II/Hidrología.shp")
## Reading layer `Hidrología' from data source `C:\FINAL SIG II\Hidrología.shp' using driver `ESRI Shapefile'
## Simple feature collection with 692 features and 9 fields
## Geometry type: MULTILINESTRING
## Dimension:     XY
## Bounding box:  xmin: 399894.5 ymin: 2103382 xmax: 430461.4 ymax: 2134418
## Projected CRS: WGS 84 / UTM zone 14N
Agua <- st_read("C:/FINAL SIG II/Cuerpos de agua.shp")
## Reading layer `Cuerpos de agua' from data source 
##   `C:\FINAL SIG II\Cuerpos de agua.shp' using driver `ESRI Shapefile'
## Simple feature collection with 6 features and 10 fields
## Geometry type: POLYGON
## Dimension:     XY
## Bounding box:  xmin: -99.91083 ymin: 19.10647 xmax: -99.69417 ymax: 19.2309
## Geodetic CRS:  WGS 84
Incendios <- st_read("C:/FINAL SIG II/Incendios Forestales.shp")
## Reading layer `Incendios Forestales' from data source 
##   `C:\FINAL SIG II\Incendios Forestales.shp' using driver `ESRI Shapefile'
## Simple feature collection with 25 features and 15 fields
## Geometry type: MULTIPOLYGON
## Dimension:     XY
## Bounding box:  xmin: -99.94377 ymin: 19.03212 xmax: -99.68736 ymax: 19.27069
## Geodetic CRS:  WGS 84
Clima <- st_read("C:/FINAL SIG II/Clima.shp")
## Reading layer `Clima' from data source `C:\FINAL SIG II\Clima.shp' using driver `ESRI Shapefile'
## Simple feature collection with 3 features and 12 fields
## Geometry type: MULTIPOLYGON
## Dimension:     XY
## Bounding box:  xmin: 399803 ymin: 2102602 xmax: 432223 ymax: 2134903
## Projected CRS: WGS 84 / UTM zone 14N
# 3. TRANSFORMAR A WGS84 (EPSG 4326)


ANP <- st_transform(ANP,4326)
Municipios <- st_transform(Municipios,4326)
Localidades <- st_transform(Localidades,4326)
UsoSuelo <- st_transform(UsoSuelo,4326)
Vegetacion <- st_transform(Vegetacion,4326)
Cobertura <- st_transform(Cobertura,4326)
Hidrologia <- st_transform(Hidrologia,4326)
Agua <- st_transform(Agua,4326)
Incendios <- st_transform(Incendios,4326)
Clima <- st_transform(Clima,4326)


# 4. EXTENSIÓN DEL MAPA

caja <- st_bbox(ANP) %>% as.vector()

# 5. LOGOS

logo_uaemex <- "C:/FINAL SIG II/log_uni.png"


# 6. MAPA LEAFLET

m <- leaflet() %>%
  
  addProviderTiles("OpenStreetMap") %>%
  
  # ANP
  
  addPolygons(
    data = ANP,
    color = "red",
    weight = 4,
    fill = FALSE,
    label = ~NOMBRE,
    group = "ANP Nevado de Toluca"
  ) %>%
  
  # Municipios
  
  addPolygons(
    data = Municipios,
    color = "black",
    weight = 1,
    fillOpacity = 0.15,
    label = ~NOMGEO,
    group = "Municipios"
  ) %>%
  
  # Localidades
  
  addPolygons(
    data = Localidades,
    color = "orange",
    weight = 1,
    fillOpacity = 0.5,
    label = ~NOMGEO,
    group = "Localidades"
  ) %>%
  
  # Uso de suelo
  
  addPolygons(
    data = UsoSuelo,
    color = "gold",
    weight = 1,
    fillOpacity = 0.4,
    label = ~DESCRIPCIO,
    group = "Uso de suelo"
  ) %>%
  
  # Vegetación
  
  addPolygons(
    data = Vegetacion,
    color = "darkgreen",
    weight = 1,
    fillOpacity = 0.4,
    label = ~DESCRIPCIO,
    group = "Vegetación"
  ) %>%
  
  # Cobertura arbórea
  
  addPolygons(
    data = Cobertura,
    color = "green",
    weight = 1,
    fillOpacity = 0.4,
    label = ~DESCRIPCIO,
    group = "Cobertura arbórea"
  ) %>%
  
  # Hidrología
  
  addPolylines(
    data = Hidrologia,
    color = "blue",
    weight = 2,
    label = ~DESCRIPCIO,
    group = "Hidrología"
  ) %>%
  
  # Cuerpos de agua
  
  addPolygons(
    data = Agua,
    color = "cyan",
    fillColor = "cyan",
    fillOpacity = 0.6,
    label = ~nombre,
    group = "Cuerpos de agua"
  ) %>%
  
  # Incendios forestales
  
  addPolygons(
    data = Incendios,
    color = "hotpink",
    fillColor = "hotpink",
    fillOpacity = 0.6,
    label = ~TIPOINC,
    group = "Incendios forestales"
  ) %>%
  
  # Clima
  
  addPolygons(
    data = Clima,
    color = "purple",
    weight = 1,
    fillOpacity = 0.3,
    label = ~DESC_CLIMA,
    group = "Clima"
  ) %>%
  
  

# 7. CONTROL DE CAPAS

addLayersControl(
  overlayGroups = c(
    "ANP Nevado de Toluca",
    "Municipios",
    "Localidades",
    "Uso de suelo",
    "Vegetación",
    "Cobertura arbórea",
    "Hidrología",
    "Cuerpos de agua",
    "Incendios forestales",
    "Clima"
  ),
  options = layersControlOptions(collapsed = FALSE)
) %>%
  

# 8. LEYENDA

addLegend(
  position = "bottomright",
  colors = c(
    "red",
    "black",
    "orange",
    "gold",
    "darkgreen",
    "green",
    "blue",
    "cyan",
    "hotpink",
    "purple"
  ),
  labels = c(
    "ANP",
    "Municipios",
    "Localidades",
    "Uso de suelo",
    "Vegetación",
    "Cobertura arbórea",
    "Hidrología",
    "Cuerpos de agua",
    "Incendios forestales",
    "Clima"
  ),
  title = "Leyenda"
  
) %>%
  

# 9. ZOOM

fitBounds(
  lng1 = caja[1],
  lat1 = caja[2],
  lng2 = caja[3],
  lat2 = caja[4]
)


# 10. LOGOS

file.exists("C:/FINAL SIG II/logo_fgeo.jpg")
## [1] TRUE
file.exists("C:/FINAL SIG II/logo_uaemex.jpg")
## [1] TRUE
logo_fgeo <- "C:/FINAL SIG II/logo_fgeo.jpg"
logo_uaemex <- "C:/FINAL SIG II/logo_uaemex.jpg"


m <- m %>%
  leafem::addLogo(
    img = logo_uaemex,
    width = 120,
    position = "topleft"
  ) 

m <- m %>%
  leafem::addLogo(
    img = "C:/FINAL SIG II/logo_fgeo.jpg",
    width = 100,
    position = "bottomleft"
  ) 
m
# 12. EXPORTAR A HTML

saveWidget(
  m,
  file = "C:/FINAL SIG II/Geovisualizador_Nevado_Toluca.html",
  selfcontained = TRUE
)

file.exists("C:/FINAL SIG II/Geovisualizador_Nevado_Toluca.html")
## [1] TRUE