Last updated: 2021-04-06
Checks: 6 1
Knit directory: mapme.protectedareas/
This reproducible R Markdown analysis was created with workflowr (version 1.6.2). The Checks tab describes the reproducibility checks that were applied when the results were created. The Past versions tab lists the development history.
The R Markdown is untracked by Git. To know which version of the R Markdown file created these results, you’ll want to first commit it to the Git repo. If you’re still working on the analysis, you can ignore this warning. When you’re finished, you can run wflow_publish
to commit the R Markdown file and build the HTML.
Great job! The global environment was empty. Objects defined in the global environment can affect the analysis in your R Markdown file in unknown ways. For reproduciblity it’s best to always run the code in an empty environment.
The command set.seed(20210305)
was run prior to running the code in the R Markdown file. Setting a seed ensures that any results that rely on randomness, e.g. subsampling or permutations, are reproducible.
Great job! Recording the operating system, R version, and package versions is critical for reproducibility.
Nice! There were no cached chunks for this analysis, so you can be confident that you successfully produced the results during this run.
Great job! Using relative paths to the files within your workflowr project makes it easier to run your code on other machines.
Great! You are using Git for version control. Tracking code development and connecting the code version to the results is critical for reproducibility.
The results in this page were generated with repository version 9421446. See the Past versions tab to see a history of the changes made to the R Markdown and HTML files.
Note that you need to be careful to ensure that all relevant files for the analysis have been committed to Git prior to generating the results (you can use wflow_publish
or wflow_git_commit
). workflowr only checks the R Markdown file, but you know if there are other scripts or data files that it depends on. Below is the status of the Git repository when the results were generated:
Ignored files:
Ignored: .RData
Ignored: .Rhistory
Ignored: .Rproj.user/
Ignored: mapme.protectedareas.Rproj
Ignored: renv/library/
Ignored: renv/staging/
Untracked files:
Untracked: analysis/gmw-mangrove.rmd
Note that any generated files, e.g. HTML, png, CSS, etc., are not included in this status report because it is ok for generated content to have uncommitted changes.
There are no past versions. Publish this analysis with wflow_publish()
to start tracking its development.
# load required libraries
library("terra")
library("sf")
library("wdpar")
library("dplyr")
library("knitr")
starttime<-Sys.time() # mark the start time of this routine to calculate processing time at the end
At first you might want to load the source functions for this routine.
source("code/area_proj.R")
Mangrove is tropical coastal vegetation and considered the most significant part of the marine ecosystem and provides a link between the sea and the land. Hence, considered one of the world’s dominant coastal ecosystem. There have been subsequent changes in the extent of mangroves since decades. The changes might be in the form of gain or loss. Global Mangrove Watch is an open source platform offering remote sensing data and tools for monitoring mangroves around the globe. The main purpose of this routine is to carry out analysis on the protected area level to see whether the extent of mangroves within definite PAs is increasing or decreasing.
The processing routine:
Global mangrove watch provides the mangrove data for the years 1996 to 2016 with periodic updates in between. The datasets have been stored in the datalake as the geopackage. The stored datasets are downloaded directly from the Ocean Data Viewer as mentioned in the metadata above.
For this routine, we will first load the mangrove data for year 1996 to see the extent of mangrove for this particular year.
# load mangrove data for 1996
m <- st_read("/home/ombhandari/shared/datalake/mapme.protectedareas/input/global_mangrove_watch/gmw-v2-1996-valid.gpkg")
Reading layer `gmw-v2-1996-valid' from data source `/home/rstudio/shared/datalake/mapme.protectedareas/input/global_mangrove_watch/gmw-v2-1996-valid.gpkg' using driver `GPKG'
Simple feature collection with 700904 features and 2 fields
Geometry type: POLYGON
Dimension: XY
Bounding box: xmin: -175.3396 ymin: -38.85667 xmax: 179.9796 ymax: 33.79933
Geodetic CRS: WGS 84
For this analysis, we would take one polygon data from Brazil, which is closer to coastal region so that the extent of mangroves can be analyzed.
# fetch the raw data from wdpar of country
br <- wdpar::wdpa_fetch("BRA")
# # subset a wdpa polygon by it's wdpa id
b <- br%>%
filter(WDPAID %in% 555637331)
The next immediate step would be to clean the fetched raw data with the functionality provided with routines from the wdpar
package. Cleaning is done by the package following these steps:
# clean the data
b <- wdpa_clean(
b,
erase_overlaps = FALSE)
Lets have a look at the selected polygon data.
plot(b[1])
After completing mangrove data and WDPA polygon preparation, we should now intersect the layers to crop out the desired extent from the global mangrove polygon. Before doing so, it is important to match the coordinate reference system of the polygons. For this, we will perform the coordinate transformation of the WDPA polygon to match with the CRS of mangrove data.
# reproject wdpa polygon to match mangrove data
b <- st_transform(b, st_crs(m))
Now, we can apply st_intersection
function to get the desired area of intersection between mangrove and wdpa polygon.
# apply intersection
m_subset <- st_intersection(m, b)
Now, we have got the intersection polygon. Next step would be to compute the mangrove area. First we have to adopt adequate projection system. We must choose the projection system which preserves the area of the polygon. We will use the function area_proj
which takes bounding box of the polygon as input parameters and returns the proj4string
, the projection system parameters in Lambert Azimuthal Equal Area projection.
# transform to laea using area_proj
m_sub <- st_transform(m_subset, st_crs(area_proj(b)))
# compute area in square km
area <- st_area(m_sub)%>%
sum()/1000000
area
4898.962 [m^2]
So, from the result above we can see that the area of mangrove for the WDPA ID 555637331 for the year 1996 is 4898.962 square km. Similarly, we can compute the mangrove area of the desired polygon/s following this routine.
In the end we are going to have a look how long the rendering of this file took so that people get an idea about the processing speed of this routine.
stoptime<-Sys.time()
print(starttime-stoptime)
Time difference of -12.37195 mins
[1] Bunting P., Rosenqvist A., Lucas R., Rebelo L-M., Hilarides L., Thomas N., Hardy A., Itoh T., Shimada M. and Finlayson C.M. (2018). The Global Mangrove Watch – a New 2010 Global Baseline of Mangrove Extent. Remote Sensing 10(10): 1669. doi:10.3390/rs1010669.
sessionInfo()
R version 4.0.3 (2020-10-10)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 20.04 LTS
Matrix products: default
BLAS/LAPACK: /usr/lib/x86_64-linux-gnu/openblas-pthread/libopenblasp-r0.3.8.so
locale:
[1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C
[3] LC_TIME=en_US.UTF-8 LC_COLLATE=en_US.UTF-8
[5] LC_MONETARY=en_US.UTF-8 LC_MESSAGES=C
[7] LC_PAPER=en_US.UTF-8 LC_NAME=C
[9] LC_ADDRESS=C LC_TELEPHONE=C
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C
attached base packages:
[1] stats graphics grDevices datasets utils methods base
other attached packages:
[1] knitr_1.31 dplyr_1.0.5 wdpar_1.0.6 sf_0.9-8
[5] terra_1.1-4 workflowr_1.6.2
loaded via a namespace (and not attached):
[1] tidyselect_1.1.0 xfun_0.22 purrr_0.3.4 lattice_0.20-41
[5] vctrs_0.3.7 generics_0.1.0 htmltools_0.5.1.1 yaml_2.2.1
[9] utf8_1.2.1 rlang_0.4.10 e1071_1.7-6 later_1.1.0.1
[13] pillar_1.5.1 glue_1.4.2 DBI_1.1.1 rappdirs_0.3.3
[17] sp_1.4-5 lifecycle_1.0.0 stringr_1.4.0 raster_3.4-5
[21] codetools_0.2-16 evaluate_0.14 httpuv_1.5.5 curl_4.3
[25] class_7.3-17 fansi_0.4.2 highr_0.8 Rcpp_1.0.6
[29] KernSmooth_2.23-17 renv_0.13.0 promises_1.2.0.1 classInt_0.4-3
[33] lwgeom_0.2-5 countrycode_1.2.0 fs_1.5.0 digest_0.6.27
[37] stringi_1.5.3 grid_4.0.3 rprojroot_2.0.2 tools_4.0.3
[41] magrittr_2.0.1 proxy_0.4-25 tibble_3.1.0 crayon_1.4.1
[45] pkgconfig_2.0.3 ellipsis_0.3.1 assertthat_0.2.1 rmarkdown_2.7
[49] httr_1.4.2 R6_2.5.0 units_0.7-1 git2r_0.28.0
[53] compiler_4.0.3