library(sf)
library(leaflet)
library(dplyr)
library(rmapshaper)
# ── Cargar y simplificar geometrías (keep=5% de vértices) ─────────────────
dept_raw <- sf::st_read("data/col_departamentos.geojson", quiet = TRUE)
mun_raw <- sf::st_read("data/col_municipios.geojson", quiet = TRUE)
dept <- ms_simplify(dept_raw, keep = 0.05, keep_shapes = TRUE)
mun <- ms_simplify(mun_raw, keep = 0.05, keep_shapes = TRUE)
pal_dept <- colorFactor(
palette = "Set3",
domain = dept$departamento
)
leaflet(options = leafletOptions(minZoom = 5)) |>
addProviderTiles("CartoDB.Positron") |>
setView(lng = -74.3, lat = 4.5, zoom = 6) |>
# Capa municipios (visible desde zoom 7)
addPolygons(
data = mun,
group = "Municipios",
fillColor = "#f59e0b",
fillOpacity = 0.25,
color = "#92400e",
weight = 0.6,
label = ~paste0(municipio, " (", departamento, ")"),
labelOptions = labelOptions(
style = list("font-weight" = "normal", padding = "3px 6px"),
textsize = "12px",
direction = "auto"
)
) |>
# Capa departamentos
addPolygons(
data = dept,
group = "Departamentos",
fillColor = ~pal_dept(departamento),
fillOpacity = 0.4,
color = "#1a3c5e",
weight = 1.2,
highlight = highlightOptions(
weight = 2.5,
color = "#c0392b",
fillOpacity = 0.65,
bringToFront = TRUE
),
label = ~departamento,
labelOptions = labelOptions(
style = list("font-weight" = "bold", padding = "4px 8px"),
textsize = "13px",
direction = "auto"
),
popup = ~paste0(
"<b>", departamento, "</b><br>",
"<span style='font-size:11px;color:#555'>", codigo_hasc, "</span>"
)
) |>
addLayersControl(
overlayGroups = c("Departamentos", "Municipios"),
options = layersControlOptions(collapsed = FALSE)
) |>
addScaleBar(position = "bottomleft") |>
addMiniMap(toggleDisplay = TRUE, minimized = TRUE)