#===============================================================
# GEOVISUALIZADOR WEB DE LA RESERVA DE LA BIOSFERA
# MARIPOSA MONARCA
#===============================================================



#===============================================================
# 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(leaflet)
library(leafem)
library(htmlwidgets)



#===============================================================
# 2. RESERVA DE LA BIOSFERA
#===============================================================

reserva <- st_read("C:/MP/MariposaMonarca.shp")
## Reading layer `MariposaMonarca' from data source `C:\MP\MariposaMonarca.shp' using driver `ESRI Shapefile'
## Simple feature collection with 1 feature and 14 fields
## Geometry type: MULTIPOLYGON
## Dimension:     XY
## Bounding box:  xmin: -100.3746 ymin: 19.30963 xmax: -100.1115 ymax: 19.99606
## Geodetic CRS:  WGS 84
mapa <- leaflet() %>%
  addTiles() %>%
  addPolygons(
    data = reserva,
    color = "red",
    fillColor = "red",
    weight = 2,
    fillOpacity = 0.5,
    popup = "Reserva de la Biosfera Mariposa Monarca",
    group = "Reserva"
  )

mapa
#===============================================================
# 3. MUNICIPIOS
#===============================================================

municipios <- st_read("C:/MP/municipios_mariposamonarca.shp")
## Reading layer `municipios_mariposamonarca' from data source 
##   `C:\MP\municipios_mariposamonarca.shp' using driver `ESRI Shapefile'
## Simple feature collection with 11 features and 9 fields
## Geometry type: POLYGON
## Dimension:     XY
## Bounding box:  xmin: -100.4841 ymin: 19.2335 xmax: -99.89085 ymax: 20.083
## Geodetic CRS:  WGS 84
mapa <- mapa %>%
  addPolygons(
    data = municipios,
    color = "black",
    fillColor = "yellow",
    weight = 1,
    fillOpacity = 0.3,
    popup = ~paste("<b>Municipio:</b>", NOMGEO),
    group = "Municipios"
  )

mapa
#===============================================================
# 4. CLIMA
#===============================================================

clima <- st_read("C:/MP/CLIMAMP.shp")
## Reading layer `CLIMAMP' from data source `C:\MP\CLIMAMP.shp' using driver `ESRI Shapefile'
## Simple feature collection with 3 features and 12 fields
## Geometry type: MULTIPOLYGON
## Dimension:     XY
## Bounding box:  xmin: -100.3746 ymin: 19.30963 xmax: -100.1115 ymax: 19.99606
## Geodetic CRS:  WGS 84
pal_clima <- colorFactor(
  palette = c("#FFF176", "#81C784", "#4FC3F7"),
  domain = clima$CLIMA_TIPO
)

mapa <- mapa %>%
  addPolygons(
    data = clima,
    color = "black",
    weight = 0.5,
    fillColor = ~pal_clima(CLIMA_TIPO),
    fillOpacity = 0.6,
    popup = ~paste("<b>Clima:</b>", CLIMA_TIPO),
    group = "Clima"
  )

mapa
#===============================================================
# 5. VEGETACIÓN
#===============================================================

vegetacion <- st_read("C:/MP/VEGETACIONMP.shp")
## Reading layer `VEGETACIONMP' from data source `C:\MP\VEGETACIONMP.shp' using driver `ESRI Shapefile'
## Simple feature collection with 110 features and 4 fields
## Geometry type: MULTIPOLYGON
## Dimension:     XY
## Bounding box:  xmin: -100.3746 ymin: 19.30963 xmax: -100.1115 ymax: 19.99606
## Geodetic CRS:  WGS 84
pal_vegetacion <- colorFactor(
  palette = colorRampPalette(c("#edf8e9","#006d2c"))(14),
  domain = vegetacion$DESCRIPCIO
)

mapa <- mapa %>%
  addPolygons(
    data = vegetacion,
    color = "black",
    weight = 0.5,
    fillColor = ~pal_vegetacion(DESCRIPCIO),
    fillOpacity = 0.7,
    popup = ~paste("<b>Vegetación:</b>", DESCRIPCIO),
    group = "Vegetación"
  )

mapa
#===============================================================
# 6. COBERTURA ARBÓREA
#===============================================================

cobertura <- st_read("C:/MP/COBERTURAARBOREAMP.shp")
## Reading layer `COBERTURAARBOREAMP' from data source `C:\MP\COBERTURAARBOREAMP.shp' using driver `ESRI Shapefile'
## Simple feature collection with 18 features and 6 fields
## Geometry type: MULTIPOLYGON
## Dimension:     XY
## Bounding box:  xmin: -100.3746 ymin: 19.30963 xmax: -100.1115 ymax: 19.99606
## Geodetic CRS:  WGS 84
pal_cobertura <- colorFactor(
  palette = c(
    "#006400",
    "#228B22",
    "#D2B48C",
    "#7CFC00"
  ),
  domain = cobertura$DESC_SAMOF
)

mapa <- mapa %>%
  addPolygons(
    data = cobertura,
    color = "black",
    weight = 0.5,
    fillColor = ~pal_cobertura(DESC_SAMOF),
    fillOpacity = 0.7,
    popup = ~paste("<b>Cobertura:</b>", DESC_SAMOF),
    group = "Cobertura arbórea"
  )

mapa
#===============================================================
# 7. USO DE SUELO
#===============================================================

usosuelo <- st_read("C:/MP/USODESUELOMP.shp")
## Reading layer `USODESUELOMP' from data source `C:\MP\USODESUELOMP.shp' using driver `ESRI Shapefile'
## Simple feature collection with 110 features and 4 fields
## Geometry type: MULTIPOLYGON
## Dimension:     XY
## Bounding box:  xmin: -100.3746 ymin: 19.30963 xmax: -100.1115 ymax: 19.99606
## Geodetic CRS:  WGS 84
pal_uso <- colorFactor(
  palette = rainbow(length(unique(usosuelo$DESCRIPCIO))),
  domain = usosuelo$DESCRIPCIO
)

mapa <- mapa %>%
  addPolygons(
    data = usosuelo,
    color = "black",
    weight = 0.5,
    fillColor = ~pal_uso(DESCRIPCIO),
    fillOpacity = 0.7,
    popup = ~paste("<b>Uso de suelo:</b>", DESCRIPCIO),
    group = "Uso de suelo"
  )

mapa
#===============================================================
# 8. HIDROLOGÍA
#===============================================================

hidrologia <- st_read("C:/MP/HIDROLOGIAMP.shp")
## Reading layer `HIDROLOGIAMP' from data source `C:\MP\HIDROLOGIAMP.shp' using driver `ESRI Shapefile'
## Simple feature collection with 62 features and 11 fields
## Geometry type: MULTILINESTRING
## Dimension:     XY
## Bounding box:  xmin: -100.3669 ymin: 19.3144 xmax: -100.1117 ymax: 19.99586
## Geodetic CRS:  WGS 84
mapa <- mapa %>%
  addPolylines(
    data = hidrologia,
    color = "blue",
    weight = 2,
    popup = ~NOMBRE,
    group = "Hidrología"
  )

mapa
#===============================================================
# 9. INCENDIOS
#===============================================================

incendios <- st_read("C:/MP/INCENDIOSMP.shp")
## Reading layer `INCENDIOSMP' from data source `C:\MP\INCENDIOSMP.shp' using driver `ESRI Shapefile'
## Simple feature collection with 20 features and 15 fields
## Geometry type: MULTIPOLYGON
## Dimension:     XY
## Bounding box:  xmin: -100.3687 ymin: 19.31577 xmax: -100.1667 ymax: 19.67642
## Geodetic CRS:  WGS 84
pal_incendios <- colorFactor(
  palette = c("green","orange","blue","purple","red"),
  domain = incendios$CAUSA
)

mapa <- mapa %>%
  addPolygons(
    data = incendios,
    color = "black",
    weight = 0.5,
    fillColor = ~pal_incendios(CAUSA),
    fillOpacity = 0.7,
    popup = ~paste("<b>Causa:</b>", CAUSA),
    group = "Incendios"
  )

mapa
#===============================================================
# 10. DEGRADACIÓN DEL SUELO
#===============================================================

degradacion <- st_read("C:/MP/DEGRADACIONDELSUELOMP.shp")
## Reading layer `DEGRADACIONDELSUELOMP' from data source 
##   `C:\MP\DEGRADACIONDELSUELOMP.shp' using driver `ESRI Shapefile'
## Simple feature collection with 13 features and 6 fields
## Geometry type: MULTIPOLYGON
## Dimension:     XY
## Bounding box:  xmin: -100.3746 ymin: 19.30963 xmax: -100.1528 ymax: 19.97919
## Geodetic CRS:  WGS 84
pal_degradacion <- colorFactor(
  palette = c("yellow","orange","brown","gray","red"),
  domain = degradacion$causa
)

mapa <- mapa %>%
  addPolygons(
    data = degradacion,
    color = "black",
    weight = 0.5,
    fillColor = ~pal_degradacion(causa),
    fillOpacity = 0.7,
    popup = ~paste("<b>Degradación:</b>", causa),
    group = "Degradación del suelo"
  )

mapa
#===============================================================
# 11. CONTROL DE CAPAS
#===============================================================

mapa <- mapa %>%
  addLayersControl(
    overlayGroups = c(
      "Reserva",
      "Municipios",
      "Clima",
      "Vegetación",
      "Cobertura arbórea",
      "Uso de suelo",
      "Hidrología",
      "Incendios",
      "Degradación del suelo"
    ),
    options = layersControlOptions(collapsed = FALSE)
  )

mapa
#===============================================================
# 12. LOGOS
#===============================================================

mapa <- leafem::addLogo(
  mapa,
  "C:/MP/uaemex.png",
  position = "bottomleft",
  width = 70
)

mapa <- leafem::addLogo(
  mapa,
  "C:/MP/geo.jpg",
  position = "bottomright",
  width = 70
)

mapa
#===============================================================
# 13. BARRA DE ESCALA
#===============================================================

mapa <- mapa %>%
  addScaleBar(position = "bottomleft")

mapa
#===============================================================
# 14. LEYENDAS
#===============================================================

mapa <- mapa %>%
  addLegend(
    "bottomright",
    pal = pal_clima,
    values = clima$CLIMA_TIPO,
    title = "Clima"
  ) %>%
  addLegend(
    "bottomright",
    pal = pal_vegetacion,
    values = vegetacion$DESCRIPCIO,
    title = "Vegetación"
  ) %>%
  addLegend(
    "bottomright",
    pal = pal_cobertura,
    values = cobertura$DESC_SAMOF,
    title = "Cobertura arbórea"
  ) %>%
  addLegend(
    "bottomright",
    pal = pal_uso,
    values = usosuelo$DESCRIPCIO,
    title = "Uso de suelo"
  )

mapa