Code
# --- Centroïdes des fokontany (moyennes des coordonnées GPS des hameaux) ---
# Les coordonnées individuelles des hameaux ne sont pas diffusées pour des
# raisons de confidentialité. Les centroïdes ci-dessous ont été calculés à
# partir du fichier :
# documentation/Coordonnées_GPS_OR_Alaotra_Marovoay_2025.ods
# Code original de calcul (commenté) :
# gps_alaotra <- read_ods(
# "documentation/Coordonnées_GPS_OR_Alaotra_Marovoay_2025.ods",
# sheet = "Alaotra 2025"
# )
# gps_marovoay <- read_ods(
# "documentation/Coordonnées_GPS_OR_Alaotra_Marovoay_2025.ods",
# sheet = "Marovoay 2025"
# )
# parse_dms <- function(dms_str) {
# s <- str_replace_all(dms_str, "\\n", " ")
# s <- str_replace_all(s, "[\"\u201d\u2033]", "'")
# pattern <- "(\\d+)[°](\\d+)'([\\d.]+)'?\\s*([NS])\\s+(\\d+)[°](\\d+)'([\\d.]+)'?\\s*([EW])"
# m <- str_match(s, pattern)
# if (is.na(m[1])) return(c(lat = NA_real_, lon = NA_real_))
# lat <- as.numeric(m[2]) + as.numeric(m[3]) / 60 + as.numeric(m[4]) / 3600
# if (m[5] == "S") lat <- -lat
# lon <- as.numeric(m[6]) + as.numeric(m[7]) / 60 + as.numeric(m[8]) / 3600
# if (m[9] == "W") lon <- -lon
# c(lat = lat, lon = lon)
# }
# coords_alaotra <- gps_alaotra |>
# rename(Localisation = 5) |> rowwise() |>
# mutate(coords = list(parse_dms(Localisation)),
# lat = coords[["lat"]], lon = coords[["lon"]]) |>
# ungroup() |> select(Hameaux, Fokontany, lat, lon) |>
# mutate(Hameaux = str_replace_all(Hameaux, "\\n", " ") |> str_squish(),
# Observatory = "Alaotra") |> filter(!is.na(lat))
# parse_marovoay <- function(gps_str) {
# s <- str_replace_all(gps_str, "\\n", " ")
# lat_match <- str_match(s, "Latitude\\s+(-?[\\d.]+)")
# lon_match <- str_match(s, "Longitude\\s+(-?[\\d.]+)")
# c(lat = as.numeric(lat_match[2]), lon = as.numeric(lon_match[2]))
# }
# coords_marovoay <- gps_marovoay |>
# rename(GPS = `Coordonnées GPS`) |> rowwise() |>
# mutate(coords = list(parse_marovoay(GPS)),
# lat = coords[["lat"]], lon = coords[["lon"]]) |>
# ungroup() |> select(Hameaux, Fokontany, lat, lon) |>
# mutate(Observatory = "Marovoay") |> filter(!is.na(lat))
# all_coords <- bind_rows(coords_alaotra, coords_marovoay)
# fokontany_centroids <- all_coords |>
# summarise(lat = mean(lat), lon = mean(lon), n_hameaux = n(),
# .by = c(Observatory, Fokontany))
fokontany_centroids <- tribble(
~Observatory, ~Fokontany, ~lat, ~lon,
"Alaotra", "Avaradrano", -17.805414, 48.469958,
"Alaotra", "Feramanga Atsimo", -17.844153, 48.373383,
"Alaotra", "Mangabe", -17.879586, 48.405244,
"Alaotra", "Analamiranga", -17.572375, 48.200017,
"Alaotra", "Maritampona", -17.603500, 48.207639,
"Alaotra", "Ambohidrony", -17.729175, 48.208611,
"Alaotra", "Ambatomanga", -17.741178, 48.200647,
"Marovoay", "Ampijoroa", -16.233414, 46.477984,
"Marovoay", "Maroala", -16.228857, 46.539738,
"Marovoay", "Madiromiongana", -16.086480, 46.749270,
"Marovoay", "Bepako", -16.163712, 46.658402
)
# Données WDPA
# On télécharge seulement si pas présentes localement
if (!(dir.exists("data/wdpa/"))) {
wdpa_mdg <- wdpa_fetch("MDG", download_dir = "data/wdpa", wait = TRUE)
write_rds(wdpa_mdg, "data/wdpa/MDG_protected_areas.rds")
wdpa_mdg <- read_rds("data/wdpa/MDG_protected_areas.rds") |>
filter(str_detect(DESIG, "Ramsar", negate = TRUE)) |>
filter(str_detect(DESIG, "UNESCO", negate = TRUE))
} else {
wdpa_mdg <- read_rds("data/wdpa/MDG_protected_areas.rds")
}
wdpa_mdg <- wdpa_mdg |>
filter(str_detect(DESIG, "Ramsar", negate = TRUE)) |>
filter(str_detect(DESIG, "UNESCO", negate = TRUE))
if (!file.exists("data/alaotra_protected_area.geojson" )) {
wdpa_mdg |>
filter(
str_detect(NAME, "Alaotra") &
str_detect(DESIG, "Ramsar", negate = TRUE)
) |>
st_write("data/alaotra_protected_area.geojson", delete_dsn = TRUE)
}
if (!file.exists("data/ankarafantsika_protected_area.geojson" )) {
wdpa_mdg |>
filter(
str_detect(NAME, "Ankarafantsika") &
str_detect(DESIG, "Ramsar", negate = TRUE)
) |>
st_write("data/ankarafantsika_protected_area.geojson", delete_dsn = TRUE)
}
pa_alaotra <- st_read("data/alaotra_protected_area.geojson", quiet = TRUE)
pa_ankarafantsika <- st_read("data/ankarafantsika_protected_area.geojson",
quiet = TRUE)
# --- Filter centroids and protected areas by profile ---
fokontany_centroids <- filter_for_profile(fokontany_centroids)
# --- Carte interactive avec tmap ---
foko_sf <- st_as_sf(fokontany_centroids, coords = c("lon", "lat"), crs = 4326)
# Select protected areas relevant to the active profile
if (REPORT_MODE$is_consolidated) {
pa_display <- bind_rows(pa_alaotra, pa_ankarafantsika)
} else if (REPORT_MODE$observatory == "Alaotra") {
pa_display <- pa_alaotra
} else {
pa_display <- pa_ankarafantsika
}
# Expand bounding box for better framing of centroids
bb_expanded <- tmaptools::bb(foko_sf, ext = 1.3)
if (knitr::is_html_output()) {
tmap_mode("view")
} else {
tmap_mode("plot")
}
tm_shape(foko_sf, bbox = bb_expanded) +
tm_dots(
fill = "Observatory",
fill.scale = tm_scale_categorical(
values = c("Alaotra" = "#E41A1C", "Marovoay" = "#377EB8")
),
fill.legend = tm_legend(title = "Observatoire"),
size = 1,
id = "Fokontany",
popup.vars = c("Observatoire" = "Observatory", "Fokontany" = "Fokontany")
) +
tm_shape(pa_display |> mutate(`Couche` = "Aires protégées")) +
tm_borders(
col = "Couche",
col.scale = tm_scale_categorical(values = c("Aires protégées" = "lightgreen")),
col.legend = tm_legend(title = ""),
lwd = 2
) +
tm_basemap("OpenStreetMap") +
tm_scalebar(position = c("left", "bottom"))Localisation approximative des fokontany enquêtés (centroïde des hameaux)