Skip to content

Commit

Permalink
add interactive report as Quarto html doc
Browse files Browse the repository at this point in the history
  • Loading branch information
karpfen committed Jul 25, 2024
0 parents commit 4b8ccae
Show file tree
Hide file tree
Showing 3 changed files with 136 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.Rproj.user
.Rhistory
.RData
.Ruserdata
16 changes: 16 additions & 0 deletions interactive-deforestation-map.Rproj
Original file line number Diff line number Diff line change
@@ -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
116 changes: 116 additions & 0 deletions interactive-deforestation-map.qmd
Original file line number Diff line number Diff line change
@@ -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
```

0 comments on commit 4b8ccae

Please sign in to comment.