From 4b8ccae65a12fb344f924efe989ca0761e1ad285 Mon Sep 17 00:00:00 2001 From: "Petutschnig, Andreas" Date: Thu, 25 Jul 2024 11:49:17 +0200 Subject: [PATCH] add interactive report as Quarto html doc --- .gitignore | 4 + interactive-deforestation-map.Rproj | 16 ++++ interactive-deforestation-map.qmd | 116 ++++++++++++++++++++++++++++ 3 files changed, 136 insertions(+) create mode 100644 .gitignore create mode 100644 interactive-deforestation-map.Rproj create mode 100644 interactive-deforestation-map.qmd diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..5b6a065 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +.Rproj.user +.Rhistory +.RData +.Ruserdata diff --git a/interactive-deforestation-map.Rproj b/interactive-deforestation-map.Rproj new file mode 100644 index 0000000..e83436a --- /dev/null +++ b/interactive-deforestation-map.Rproj @@ -0,0 +1,16 @@ +Version: 1.0 + +RestoreWorkspace: Default +SaveWorkspace: Default +AlwaysSaveHistory: Default + +EnableCodeIndexing: Yes +UseSpacesForTab: Yes +NumSpacesForTab: 2 +Encoding: UTF-8 + +RnwWeave: Sweave +LaTeX: pdfLaTeX + +AutoAppendNewline: Yes +StripTrailingWhitespace: Yes diff --git a/interactive-deforestation-map.qmd b/interactive-deforestation-map.qmd new file mode 100644 index 0000000..336e250 --- /dev/null +++ b/interactive-deforestation-map.qmd @@ -0,0 +1,116 @@ +--- +title: "Interactive Deforestation Map" +format: + html: + code-fold: true + fig-width: 10 + fig-height: 10 +editor: source +--- + +```{r setup} +#| output: false + +library(dplyr) +library(leaflet) +library(leaflet.extras) +library(sf) +library(wdpar) +``` + +This script downloads WDPA data for a specified country and generates a web map displaying the selected protected areas (PAs) including a buffer region around it. You can click on +the folded `Code` chunks to explore the source code of this report. + +```{r config} +#| code-fold: false + +######## Configuration ######################################################### +################################################################################ + +# ISO3 code of target country for which to download WDPA data. +country_iso3 <- "BRA" + +# Provide specific WDPA IDs to be displayed here. If left empty, all PAs within the target country will be displayed. +# Caution: The provided IDs must be located within the selected target country. +wdpa_ids <- c("115772") + +# Buffer around the PAs in m. +buffer_m <- 10000 + +# Basic style options can be set here. For more elaborate style changes, you can edit the leaflet parameters below. +line_weight_pa <- 4.0 +line_weight_buffer <- 3.0 +line_color_pa = "#CAE64E" # Color provided as hex code +line_color_buffer = "#CAE64E" # Color provided as hex code + +############################################################################### +############################################################################### +``` + +# Preprocessing WDPA Data + +```{r process-wdpa} +wdir <- getwd() +wdpa <- wdpa_fetch(country_iso3, wait = TRUE, download_dir = file.path(wdir, "WDPA")) +if(!(length(wdpa_ids) == 1 && is.na(wdpa_ids) || all(is.na(wdpa_ids)))) { + wdpa <- wdpa |> + filter(WDPAID %in% wdpa_ids) +} + +wdpa <- wdpa |> + wdpa_clean(retain_status = NULL, + erase_overlaps = FALSE, + exclude_unesco = FALSE, + verbose = FALSE) |> + # Drop geometry POINT + filter(GEOMETRY_TYPE != "POINT") |> + # Remove the PAs that are only proposed, or have geometry type "point" + filter(STATUS != "Proposed") |> + st_transform(4326) + +# Make Buffers around all protected areas +buffer <- wdpa |> + st_buffer(dist = buffer_m) +``` + +# Preparing Leaflet Map + +```{r prepare-map} +basemap_custom <- + leaflet() |> + # add external map providers + addTiles(group = "OpenStreetMap") |> + addProviderTiles(providers$Esri.WorldImagery, group="Satellite") |> + addProviderTiles(providers$CartoDB.Positron, group="CartoDB") |> + addProviderTiles(providers$Esri.WorldShadedRelief, group="Topography") |> + addProviderTiles(providers$NASAGIBS.ViirsEarthAtNight2012, group="Nightlights") |> + addTiles( + "https://tiles.globalforestwatch.org/umd_tree_cover_loss/latest/dynamic/{z}/{x}/{y}.png", + group = "Forest Cover Loss (2001-2020)", + attribution = "Hansen, M. C., P. V. Potapov, R. Moore, M. Hancher, S. A. Turubanova, A. Tyukavina, D. Thau, S. V. Stehman, S. J. Goetz, T. R. Loveland, A. Kommareddy, A. Egorov, L. Chini, C. O. Justice, and J. R. G. Townshend. 2013. “High-Resolution Global Maps of 21st-Century Forest Cover Change.” Science 342 (15 November): 850–53. Data available on-line from: http://earthenginepartners.appspot.com/science-2013-global-forest." + ) |> + addTiles( + "https://tiles.globalforestwatch.org/umd_regional_primary_forest_2001/latest/dynamic/{z}/{x}/{y}.png", + group = "Regional Primary Forests (2001)", + attribution = "Hansen, M. C., P. V. Potapov, R. Moore, M. Hancher, S. A. Turubanova, A. Tyukavina, D. Thau, S. V. Stehman, S. J. Goetz, T. R. Loveland, A. Kommareddy, A. Egorov, L. Chini, C. O. Justice, and J. R. G. Townshend. 2013. “High-Resolution Global Maps of 21st-Century Forest Cover Change.” Science 342 (15 November): 850–53. Data available on-line from: http://earthenginepartners.appspot.com/science-2013-global-forest." + ) |> + addFullscreenControl() |> + # add legend(s) + addLayersControl( + baseGroups = c("Satellite", "CartoDB", "OpenStreetMap", "Topography", "Nightlights"), + overlayGroups = c("WDPA Regions", "Buffers", "Forest Cover Loss (2001-2020)", "Regional Primary Forests (2001)"), + options = layersControlOptions(collapsed = FALSE)) |> + # uncheck some layers in layer control + hideGroup(group = c("Regional Primary Forests (2001)","Labels (PA Names)")) + +deforestation_map <- basemap_custom |> + addPolygons(data = wdpa, fillOpacity = 0.0, opacity = 0.8, color = line_color_pa, smoothFactor = 0, weight = line_weight_pa, group = "WDPA Regions") |> + addPolygons(data = buffer, fillOpacity = 0.0, opacity = 0.8, color = line_color_buffer, smoothFactor = 0, weight = line_weight_buffer, group = "Buffers", + dashArray = "10 8") +``` + +# Interactive Deforestation Map + +```{r plot-map} + deforestation_map +```