diff --git a/.github/workflows/R-CMD-check-macos.yaml b/.github/workflows/R-CMD-check-macos.yaml index ffa86ac..af7df90 100644 --- a/.github/workflows/R-CMD-check-macos.yaml +++ b/.github/workflows/R-CMD-check-macos.yaml @@ -50,16 +50,27 @@ jobs: brew install mpfr brew install automake brew install gdal + brew install --cask chromium - uses: r-lib/actions/setup-r-dependencies@v2 with: cache-version: 4 extra-packages: | any::rcmdcheck + any::remotes cran/XML + prepr=?ignore needs: | check + - name: Install spatial R packages + run: | + options(repos = "https://cloud.r-project.org/") + install.packages("sf") + install.packages("lwgeom") + remotes::install_github("dickoa/prepr") + shell: Rscript {0} + - name: Session information run: | options(width = 100) diff --git a/.github/workflows/R-CMD-check-ubuntu.yaml b/.github/workflows/R-CMD-check-ubuntu.yaml index f62b894..6358e1e 100644 --- a/.github/workflows/R-CMD-check-ubuntu.yaml +++ b/.github/workflows/R-CMD-check-ubuntu.yaml @@ -52,7 +52,8 @@ jobs: sudo apt update sudo apt-get install \ libgdal-dev libgeos-dev libproj-dev \ - libgmp3-dev libmpfr-dev libudunits2-dev + libgmp3-dev libmpfr-dev libudunits2-dev \ + chromium-browser - uses: r-lib/actions/setup-r-dependencies@v2 with: diff --git a/.github/workflows/R-CMD-check-windows.yaml b/.github/workflows/R-CMD-check-windows.yaml index c83b58a..5dc9d48 100644 --- a/.github/workflows/R-CMD-check-windows.yaml +++ b/.github/workflows/R-CMD-check-windows.yaml @@ -46,6 +46,10 @@ jobs: - uses: r-lib/actions/setup-pandoc@v2 + - uses: browser-actions/setup-chrome@v1 + with: + install-dependencies: true + - uses: r-lib/actions/setup-r@v2 with: r-version: ${{ matrix.config.r }} @@ -56,6 +60,7 @@ jobs: cache-version: 4 extra-packages: | any::rcmdcheck + any::remotes any::XML prepr=?ignore needs: | @@ -63,7 +68,9 @@ jobs: - name: Install spatial R packages run: | - install.packages("remotes") + options(repos = "https://cloud.r-project.org/") + install.packages("sf") + install.packages("lwgeom") remotes::install_github("dickoa/prepr") shell: Rscript {0} diff --git a/.github/workflows/documentation.yaml b/.github/workflows/documentation.yaml index 6087f63..98f45f0 100644 --- a/.github/workflows/documentation.yaml +++ b/.github/workflows/documentation.yaml @@ -50,7 +50,8 @@ jobs: sudo apt update sudo apt-get install \ libgdal-dev libgeos-dev libproj-dev \ - libgmp3-dev libmpfr-dev libudunits2-dev + libgmp3-dev libmpfr-dev libudunits2-dev \ + chromium-browser - uses: r-lib/actions/setup-r-dependencies@v2 with: @@ -84,6 +85,7 @@ jobs: run: | result <- urlchecker::url_check() result <- result[!startsWith(result$URL, "https://doi.org/"), , drop = FALSE] + result <- result[!startsWith(result$URL, "http://geo.abds.is"), , drop = FALSE] if (nrow(result) > 0) { print(result) stop("Invalid URLs detected") diff --git a/DESCRIPTION b/DESCRIPTION index 23dcd65..1ba6f1f 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: wdpar Type: Package -Version: 1.3.7.1 +Version: 1.3.7.2 Title: Interface to the World Database on Protected Areas Description: Fetch and clean data from the World Database on Protected Areas (WDPA) and the World Database on Other Effective Area-Based @@ -18,8 +18,7 @@ Imports: curl (>= 3.2), httr (>= 1.3.1), countrycode (>= 1.1.0), - withr (>= 2.5.0), - webdriver (>= 1.0.6), + chromote (>= 0.2.0), xml2 (>= 1.2.0), cli (>= 1.0.1), lwgeom (>= 0.2-1), @@ -31,9 +30,9 @@ Suggests: knitr (>= 1.2.0), roxygen2 (>= 6.1.1), rmarkdown (>= 1.10), - ggmap (>= 2.6.1), + ggmap (>= 4.0.0), ggplot2 (>= 3.1.0), - prepr (>= 0.1.9000), + prepr (>= 0.3.0), dplyr (>= 1.0.7), ps (>= 1.5.0) Depends: @@ -45,7 +44,7 @@ Language: en-US URL: https://prioritizr.github.io/wdpar/, https://github.com/prioritizr/wdpar BugReports: https://github.com/prioritizr/wdpar/issues VignetteBuilder: knitr -RoxygenNote: 7.2.3 +RoxygenNote: 7.3.1 Collate: 'internal.R' 'package.R' @@ -61,4 +60,4 @@ Collate: 'zzz.R' Roxygen: list(markdown = TRUE) Remotes: - dickoa/prepr + prioritizr/prepr diff --git a/NEWS.md b/NEWS.md index b946bc3..5b464a1 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,3 +1,19 @@ +# wdpar 1.3.7.2 + +- Update `wdpa_fetch()` to use the _chromote_ package to handle web scrapping + (instead of _webdriver_). +- Update `wdpa_fetch()` so that it can download country-level data in either + shapefile or file geodatabase format (using the new `datatype` parameter). + Since file geodatabase data appears to be more robust, `wdpa_fetch()` + now defaults to downloading data in file geodatabase format. +- Update `wdpa_clean()` to standardize column names so that cleaning + either shapefile or file geodatabase data results in the same output. +- Update `wdpa_clean()` so that it removes leading/trailing white space + characters from the `"MANG_PLAN"` field. +- Fix bug in `wdpa_read()` that causes output objects to contain no columns. +- Update README and vignette to be compatible with updates to _ggmap_ package. +- Remove _withr_ package from DESCRIPTION because it is not used. + # wdpar 1.3.7.1 - Update tests to accommodate corrupted data from Protected Planet. @@ -97,7 +113,7 @@ - CRAN release. - Update `read_sf_n` to import data faster. -- Remove withr R package from DESCRIPTION because it is not used. +- Remove _withr_ package from DESCRIPTION because it is not used. # wdpar 1.3.1.6 diff --git a/R/package.R b/R/package.R index e551052..95f727f 100644 --- a/R/package.R +++ b/R/package.R @@ -29,9 +29,9 @@ #' *Science*, **350**: 1255--1258. #' #' @name wdpar -#' @aliases wdpar-package #' @docType package -NULL +#' @aliases wdpar-package +"_PACKAGE" #' @import sf NULL diff --git a/R/wdpa_clean.R b/R/wdpa_clean.R index f68dd0c..c7dfc9b 100644 --- a/R/wdpa_clean.R +++ b/R/wdpa_clean.R @@ -80,6 +80,12 @@ NULL #' This step is only performed if the argument to `exclude_unesco` is #' `TRUE`. #' +#' \item Standardize column names. This is important so that data +#' imported as in shapefile or file geodatabase format have the +#' same column names. Specifically, if present, the `"PARENT_ISO3"` field is +#' renamed to "PARENT_ISO" and the "SHAPE" field is renamed to +#' `"geometry"`. +#' #' \item Create a field (`"GEOMETRY_TYPE"`) indicating if areas are #' represented as point localities (`"POINT"`) or as polygons #' (`"POLYGON"`). @@ -144,6 +150,8 @@ NULL #' \item The size of areas are calculated in square kilometers and stored in #' the field `"AREA_KM2"`. #' +#' \item Trimming extra leading or trailing white space characters +#' from the `"MANG_PLAN"` field (e.g., `" "`, `"\n"`, `"\r"`). #' } #' #' @section Recommended practices for large datasets: @@ -284,6 +292,20 @@ wdpa_clean <- function(x, cli::cli_progress_step("retaining UNESCO Biosphere Reserves") } } + # standardize column names + if (verbose) { + cli::cli_progress_step("standardizing field names") + } + if ("PARENT_ISO3" %in% names(x)) { + names(x)[names(x) == "PARENT_ISO3"] <- "PARENT_ISO" + } + if ("SHAPE" %in% names(x)) { + names(x)[names(x) == "SHAPE"] <- "geometry" + x <- sf::st_set_geometry(x, "geometry") + } + if (verbose) { + cli::cli_progress_step("standardizing field names") + } ## assign column indicating geometry type is_point <- vapply(sf::st_geometry(x), inherits, logical(1), c("POINT", "MULTIPOINT")) @@ -434,6 +456,13 @@ wdpa_clean <- function(x, } areas <- as.numeric(sf::st_area(x)) * 1e-6 x$AREA_KM2 <- as.numeric(areas) + ## trim white space characters + if (verbose) { + cli::cli_progress_step( + "trimming extra white space characters from MANG_PLAN" + ) + } + x$MANG_PLAN <- trimws(x$MANG_PLAN) ## move geometry to last column if ((!"geometry" %in% names(x))) { geom_col <- attr(x, "sf_column") diff --git a/R/wdpa_fetch.R b/R/wdpa_fetch.R index 52fbb05..72fad9a 100644 --- a/R/wdpa_fetch.R +++ b/R/wdpa_fetch.R @@ -42,6 +42,8 @@ NULL #' reported? Defaults to `TRUE` in an interactive session, otherwise #' `FALSE`. #' +#' @inheritParams wdpa_url +#' #' @details #' This function obtains and imports data from Protected Planet. #' By default (per `force_download = FALSE`), it will check to see if the @@ -69,13 +71,33 @@ NULL #' (UNEP-WCMC 2019). #' #' @section Troubleshooting: -#' This function will sometimes return the error message -#' `PhantomJS signals port = 4567 is already in use`. -#' This error message can occur when you have previously run the function and -#' it threw an error, or it terminated early. -#' It can also occur when attempting to run the the function in multiple -#' sessions on the same computer. -#' To address this issue, you will need to restart your computer. +#' The function requires a Chromium-based browser +#' (e.g., Google Chrome, Chromium, or Brave) to be installed. +#' This is because it uses the \pkg{chromote} to find the URL +#' for downloading data from Protected Planet. +#' If you don't have one of these browsers installed, then please try +#' installing Google Chrome. +#' If you do have one of these browsers installed and this function +#' throws an error indicating that it can't find the browser, +#' try setting the `CHROMOTE_CHROME` environment variable to the +#' file path of the executable. For example, you could do this with: +#' ``` +#' Sys.setenv(CHROMOTE_CHROME = "INSERT_FILE_PATH_HERE.exe") +#' ``` +#' +#' Also, the function will sometimes produce a message +#' that complains about a `handle_read_frame` error. Please understand +#' that this message is, in fact, not an error and can be safely ignored +#' (see ). +#' As such, if you see this message when running the function, +#' you can assume that the function still worked correctly. +#' For reference, the misleading message will look something like this: +#' ``` +#' [2024-04-23 12:06:36] [error] handle_read_frame error: websocketpp.transport:7 (End of File) +#' ``` +#' +#' For further help with troubleshooting, please refer to the documentation +#' for the \pkg{chromote} package (https://rstudio.github.io/chromote/). #' #' @return [sf::sf()] object. #' @@ -132,6 +154,7 @@ wdpa_fetch <- function(x, wait = FALSE, check_version = TRUE, n = NULL, page_wait = 2, + datatype = "gdb", verbose = interactive()) { # check that arguments are valid ## check that classes are correct @@ -157,10 +180,18 @@ wdpa_fetch <- function(x, wait = FALSE, ## find latest version of the dataset current_month_year <- wdpa_latest_version() ## find the download link and set file path to save the data - download_url <- wdpa_url(x, wait = wait, page_wait = page_wait) + download_url <- wdpa_url( + x, wait = wait, page_wait = page_wait, datatype = datatype + ) ## note that file name conventions on protectedplanet.net have changed ## (detected on 8th Oct 2020) and so file names are manually changed ## to follow the previous convention + ## + ## also, note that to ensure backwwards compatibility with prevoius + ## versions of wdpar, data that are downloaded in file geodatabase format + ## will also be renamed to end with "-shapefile.zip" (even though they do + ## not contain shapefile data) and we will logic in wdpa_read() to + ## correctly import the data if (!identical(x, "global")) { file_name <- paste0("WDPA_", current_month_year, "_", country_code(x), "-shapefile.zip") diff --git a/R/wdpa_read.R b/R/wdpa_read.R index 50adee1..a545470 100644 --- a/R/wdpa_read.R +++ b/R/wdpa_read.R @@ -117,15 +117,38 @@ wdpa_read <- function(x, n = NULL) { wdpa_point_data <- wdpa_point_data[, point_matching_cols] wdpa_data <- rbind(wdpa_polygon_data, wdpa_point_data) } else { - ## extract any data stored in zip files + ## load country-level data + ### extract data stored in zip files zip_path <- dir(tdir, "^.*\\.zip$", recursive = TRUE, full.names = TRUE) - if (length(zip_path) > 0) + if (length(zip_path) > 0) { result <- Map(utils::unzip, zip_path, exdir = gsub(".zip", "", zip_path, fixed = TRUE)) - ## import shapefile data + } + ### try and find shapefiles and gdb in unzipped files shapefile_path <- dir(tdir, "^.*\\.shp$", recursive = TRUE, full.names = TRUE) - wdpa_data <- lapply(shapefile_path, read_sf_n, n = n) + gdb_path <- dir(tdir, "^.*\\.gdb$", recursive = TRUE, + full.names = TRUE, include.dirs = TRUE) + if (length(shapefile_path) > 0) { + ### if has shapefiles, then... + ### import shapefile data + wdpa_data <- lapply(shapefile_path, read_sf_n, n = n) + ### exclude any shapefiles that are empty and don't contain any data + if (length(wdpa_data) > 1) { + wdpa_data <- wdpa_data[vapply(wdpa_data, nrow, integer(1)) > 0] + } + } else if (length(gdb_path) > 0) { + ### if has file geodatabase, then... + ### determine which layers to import + d <- sf::st_layers(gdb_path) + is_d_spatial <- !vapply(d$crs, is.na, logical(1)) + wdpa_data <- lapply(d$name[is_d_spatial], sf::read_sf, dsn = gdb_path) + } else { + stop( + "Couldn't find shapefile or file geodatabase inside zip file.", + call. = FALSE + ) + } ## merge shapefile data together if (length(wdpa_data) > 1) { col_names <- Reduce(base::intersect, lapply(wdpa_data, names)) diff --git a/R/wdpa_url.R b/R/wdpa_url.R index 849a9b6..d8480a3 100644 --- a/R/wdpa_url.R +++ b/R/wdpa_url.R @@ -34,6 +34,11 @@ NULL #' containing the message `Error: Summary: NoSuchElement`). #' To avoid this, users can try specifying a greater value (e.g. 5 seconds). #' +#' @param datatype `character` denoting the file format for which to download +#' protected area data. Available options include: (`"shp"`) shapefile format +#' and (`"gdb"`) file geodatabase format. Defaults to `"gdb". +#' Note that global data are only available in file geodatabase format. +#' #' @return `character` URL to download the data. #' #' @seealso [wdpa_fetch()], [countrycode::countrycode()]. @@ -53,20 +58,30 @@ NULL #' print(global_url) #' } #' @export -wdpa_url <- function(x, wait = FALSE, page_wait = 2) { +wdpa_url <- function(x, wait = FALSE, page_wait = 2, datatype = "gdb") { # validate arguments assertthat::assert_that( assertthat::is.string(x), assertthat::is.flag(wait), assertthat::is.count(page_wait), assertthat::noNA(page_wait), + assertthat::is.string(datatype), + assertthat::noNA(datatype), is_online() ) assertthat::assert_that( - has_phantomjs(), + datatype %in% c("gdb", "shp"), + msg = "argument to datatype must be \"gdb\" or \"shp\"" + ) + assertthat::assert_that( + !(identical(x, "global") && identical(datatype, "shp")), + msg = "argument to datatype must be \"gdb\" for global data" + ) + assertthat::assert_that( + is_chrome_available(), msg = paste0( - "cannot find PhantomJS; please install it using: ", - "webdriver::install_phantomjs()" + "couldn't find a Chromium-based browser,", + "please try installing Google Chrome" ) ) # declare hidden function @@ -76,34 +91,42 @@ wdpa_url <- function(x, wait = FALSE, page_wait = 2) { ## initialize URL url <- character(0) ## initialize driver - pjs <- start_phantomjs() - rd <- webdriver::Session$new(port = pjs$port) + b <- chromote::ChromoteSession$new() + ## set timeout + b$default_timeout <- 100 ## navigate to download web page - rd$go(paste0("https://www.protectedplanet.net/country/", x)) - Sys.sleep(page_wait) # wait for page to load - elem <- rd$findElement(css = ".download__trigger") - elem$click() + p <- b$Page$loadEventFired(wait_ = FALSE) + b$Page$navigate( + paste0("https://www.protectedplanet.net/country/", x), + wait_ = FALSE + ) + b$wait_for(p) + ## click "Download" button + chromote_click_element(b, ".download__trigger") Sys.sleep(page_wait) # wait for page to load - elem <- rd$findElement(css = "li:nth-child(2) .popup__link") - elem$click() + ## click button to trigger download preparation + if (identical(datatype, "shp")) { + ### click "SHP" button + chromote_click_element(b, "li:nth-child(2) .popup__link") + } else { + ### click "File Geodatabase" button + chromote_click_element(b, "li:nth-child(3) .popup__link") + } Sys.sleep(page_wait) # wait for dialog to open - elem <- rd$findElement(css = ".modal__link-button") - elem$click() + ## click download link button + chromote_click_element(b, ".modal__link-button") Sys.sleep(page_wait) # wait for for dialog to open ## extract html for modal - src <- xml2::read_html(rd$getSource()[[1]][[1]], encoding = "UTF-8") + src <- xml2::read_html(chromote_page_source(b)[[1]], encoding = "UTF-8") divs <- xml2::xml_find_all(src, ".//div") divs <- divs[which(xml2::xml_attr(divs, "class") == "modal__content")] ## parse download link attrs <- xml2::xml_attr(xml2::xml_find_all(divs, ".//a"), "href") - url <- grep("shp.zip", attrs, fixed = TRUE, value = TRUE) + url <- grep("^.*WDPA.*\\.zip$", attrs, value = TRUE) }, finally = { ## clean up web driver - try(rd$delete(), silent = TRUE) - try(rd$delete(), silent = TRUE) - try(stop_phantomjs(pjs), silent = TRUE) - try(stop_phantomjs(pjs), silent = TRUE) + try(suppressMessages(b$parent$close()), silent = TRUE) })) ## prepare output if (length(url) == 0) @@ -140,30 +163,37 @@ wdpa_url <- function(x, wait = FALSE, page_wait = 2) { return(out) } -start_phantomjs <- function() { - # initialize phantomjs - if ( - identical(.Platform$OS.type, "unix") && - identical(Sys.getenv("OPENSSL_CONF"), "") - ) { - withr::with_envvar( - list("OPENSSL_CONF"= "/etc/ssl"), - pjs <- webdriver::run_phantomjs() - ) - } else { - pjs <- suppressMessages(webdriver::run_phantomjs()) - } - # return object - pjs +is_chrome_available <- function() { + x <- chromote::find_chrome() + if (is.null(x)) return(FALSE) + if (!nzchar(x)[[1]]) return(FALSE) + TRUE } -stop_phantomjs <- function(pjs) { - try(pjs$process$kill(), silent = TRUE) - try(pjs$process$kill(), silent = TRUE) +chromote_page_source <- function(b) { + # from https://stackoverflow.com/a/76347768 + b$Runtime$evaluate("document.querySelector('html').outerHTML")$result$value } -has_phantomjs <- function() { - pjs <- suppressMessages(try(start_phantomjs(), silent = TRUE)) - on.exit(suppressMessages(stop_phantomjs(pjs))) - !inherits(pjs, "try-error") +chromote_click_element <- function(b, css) { + # from https://github.com/rstudio/chromote/issues/30 + doc <- b$DOM$getDocument() + node <- b$DOM$querySelector(doc$root$nodeId, css) + assertthat::assert_that( + node$nodeId >= 1, + msg = paste0("couldn't find element using query \", css, \"") + ) + box <- b$DOM$getBoxModel(node$nodeId) + br <- box$model$border + x <- (br[[1]] + br[[5]]) / 2 + y <- (br[[2]] + br[[6]]) / 2 + b$Input$dispatchMouseEvent( + type = "mousePressed", x = x, y = y, button = "left", + clickCount = 1 + ) + Sys.sleep(0.01) + b$Input$dispatchMouseEvent( + type = "mouseReleased", x = x, y = y, button = "left" + ) + NULL } diff --git a/README.Rmd b/README.Rmd index 14ada70..472555c 100644 --- a/README.Rmd +++ b/README.Rmd @@ -44,8 +44,7 @@ install.packages("wdpar", repos = "https://cran.rstudio.com/") Alternatively, the latest developmental version can be installed using the following _R_ code. Please note that while developmental versions may contain additional features not present in the official version, they may also contain coding errors. ```{r, eval = FALSE} -if (!require(remotes)) - install.packages("remotes") +if (!require(remotes)) install.packages("remotes") remotes::install_github("prioritizr/wdpar") ``` @@ -54,9 +53,8 @@ remotes::install_github("prioritizr/wdpar") The _wdpar R_ package can leverage the _prepr R_ package to augment data cleaning procedures. Since the _prepr R_ package is not available on the Comprehensive R Archive Network, it is listed as an optional dependency. In some cases, the _prepr R_ package is required to complete the data cleaning procedures (e.g. to fix especially extreme geometry issues) and the _wdpar R_ package will throw an error if the package is not available. To install the _prepr R_ package, please use the following R code. ```{r, eval = FALSE} -if (!require(remotes)) - install.packages("remotes") -remotes::install_github("dickoa/prepr") +if (!require(remotes)) install.packages("remotes") +remotes::install_github("prioritizr/prepr") ``` Note that the _prepr R_ package has system dependencies that need to be installed before the package itself can be installed (see below for platform-specific instructions). @@ -78,7 +76,7 @@ apt-get install -y libgmp3-dev libmpfr-dev libudunits2-dev libgdal-dev libgeos-d For Unix-alikes, `gmp` (>= 4.2.3), `mpfr` (>= 3.0.0), and `gdal` (>= 3.2.2) are required. -##### _MacOS_ +##### _macOS_ The `gmp`, `mpfr`, and `gdal` libraries are required. The easiest way to install these libraries is using [HomeBrew](https://brew.sh/). After installing HomeBrew, these libraries can be installed using the following commands in the system terminal: @@ -134,8 +132,10 @@ Finally, after cleaning the data, let's plot a map showing Malta's protected are mlt_pa_data <- st_transform(mlt_pa_data, 4326) # download basemap imagery -bg <- get_stamenmap(unname(st_bbox(mlt_pa_data)), zoom = 8, - maptype = "watercolor", force = TRUE) +bg <- get_stadiamap( + unname(st_bbox(mlt_pa_data)), zoom = 8, + maptype = "stamen_terrain_background", force = TRUE +) # make map ggmap(bg) + diff --git a/README.md b/README.md index e7afa5e..87e33ec 100644 --- a/README.md +++ b/README.md @@ -67,8 +67,7 @@ may contain additional features not present in the official version, they may also contain coding errors. ``` r -if (!require(remotes)) - install.packages("remotes") +if (!require(remotes)) install.packages("remotes") remotes::install_github("prioritizr/wdpar") ``` @@ -84,9 +83,8 @@ available. To install the *prepr R* package, please use the following R code. ``` r -if (!require(remotes)) - install.packages("remotes") -remotes::install_github("dickoa/prepr") +if (!require(remotes)) install.packages("remotes") +remotes::install_github("prioritizr/prepr") ``` Note that the *prepr R* package has system dependencies that need to be @@ -115,7 +113,7 @@ following system commands: For Unix-alikes, `gmp` (>= 4.2.3), `mpfr` (>= 3.0.0), and `gdal` (>= 3.2.2) are required. -##### *MacOS* +##### *macOS* The `gmp`, `mpfr`, and `gdal` libraries are required. The easiest way to install these libraries is using [HomeBrew](https://brew.sh/). After @@ -180,7 +178,7 @@ head(mlt_pa_data) ## Simple feature collection with 6 features and 32 fields ## Geometry type: MULTIPOLYGON ## Dimension: XY - ## Bounding box: xmin: 1382584 ymin: 4280853 xmax: 1399759 ymax: 4299615 + ## Bounding box: xmin: 1382584 ymin: 4280853 xmax: 1394227 ymax: 4299615 ## Projected CRS: +proj=cea +lon_0=0 +lat_ts=30 +x_0=0 +y_0=0 +datum=WGS84 +ellps=WGS84 +units=m +no_defs ## Precision: 1500 ## # A tibble: 6 × 33 @@ -189,9 +187,9 @@ head(mlt_pa_data) ## 1 194425 194425 PA 'Il-… 'Il-Gżej… Rise… Nature R… National Ia ## 2 194420 194420 PA Filf… Filfla Rise… Nature R… National Ia ## 3 555588631 555588631 PA Il-M… Il-Majji… Park… National… National II - ## 4 174757 174757 PA Il-Ġ… Il-Ġonna… List… List of … National III - ## 5 174758 174758 PA Bidn… Bidnija,… List… List of … National III - ## 6 194415 194415 PA 'Il-… 'Il-Ġonn… List… List of … National III + ## 4 174758 174758 PA Bidn… Bidnija,… List… List of … National III + ## 5 194415 194415 PA 'Il-… 'Il-Ġonn… List… List of … National III + ## 6 194418 194418 PA Il-B… Il-Ballu… List… List of … National III ## # ℹ 24 more variables: INT_CRIT , MARINE , REP_M_AREA , ## # GIS_M_AREA , REP_AREA , GIS_AREA , NO_TAKE , ## # NO_TK_AREA , STATUS , STATUS_YR , GOV_TYPE , @@ -210,8 +208,10 @@ Nature](https://www.iucn.org/)). mlt_pa_data <- st_transform(mlt_pa_data, 4326) # download basemap imagery -bg <- get_stamenmap(unname(st_bbox(mlt_pa_data)), zoom = 8, - maptype = "watercolor", force = TRUE) +bg <- get_stadiamap( + unname(st_bbox(mlt_pa_data)), zoom = 8, + maptype = "stamen_terrain_background", force = TRUE +) # make map ggmap(bg) + diff --git a/docs/404.html b/docs/404.html index 0f54852..06f9c14 100644 --- a/docs/404.html +++ b/docs/404.html @@ -32,7 +32,7 @@ wdpar - 1.3.7.1 + 1.3.7.2 diff --git a/docs/CONTRIBUTING.html b/docs/CONTRIBUTING.html index 89c9696..abe8a20 100644 --- a/docs/CONTRIBUTING.html +++ b/docs/CONTRIBUTING.html @@ -17,7 +17,7 @@ wdpar - 1.3.7.1 + 1.3.7.2 diff --git a/docs/LICENSE-text.html b/docs/LICENSE-text.html index 9751a36..48e0def 100644 --- a/docs/LICENSE-text.html +++ b/docs/LICENSE-text.html @@ -17,7 +17,7 @@ wdpar - 1.3.7.1 + 1.3.7.2 diff --git a/docs/articles/index.html b/docs/articles/index.html index ddae59b..7d36c7c 100644 --- a/docs/articles/index.html +++ b/docs/articles/index.html @@ -17,7 +17,7 @@ wdpar - 1.3.7.1 + 1.3.7.2 diff --git a/docs/articles/wdpar.html b/docs/articles/wdpar.html index 7a818f1..5c8230c 100644 --- a/docs/articles/wdpar.html +++ b/docs/articles/wdpar.html @@ -33,7 +33,7 @@ wdpar - 1.3.7.1 + 1.3.7.2 @@ -72,7 +72,7 @@

wdpar: Interface to the World Database on Protected Areas

Jeffrey O. Hanson

-

2023-11-01

+

2024-04-25

Source: vignettes/wdpar.Rmd @@ -103,13 +103,12 @@

Tutorialmlt_raw_pa_data <- wdpa_fetch( "Malta", wait = TRUE, download_dir = rappdirs::user_data_dir("wdpar") ) -
## ! importing local data (version Jun 2022); use "force=TRUE" if you need latest version.

Next, we will clean the data set. Briefly, the cleaning steps include: excluding protected areas that are not yet implemented, excluding protected areas with limited conservation value, replacing missing data codes (e.g. "0") with missing data values (i.e. NA), replacing protected areas represented as points with circular protected areas that correspond to their reported extent, repairing any topological issues with the geometries, and erasing overlapping areas. Please note that, by default, spatial data processing is performed at a scale suitable for national scale analyses (see below for recommendations for local scale analyses). For more information on the data cleaning procedures, see wdpa_clean().

-
+
 # clean Malta data
 mlt_pa_data <- wdpa_clean(mlt_raw_pa_data)

After cleaning the data set, we will perform an additional step that involves clipping the terrestrial protected areas to Malta’s coastline. Ideally, we would also clip the marine protected areas to Malta’s Exclusive Economic Zone (EEZ) but such data are not as easy to obtain on a per country basis (but see https://www.marineregions.org/eez.php)).

-
+
 # download Malta boundary from Global Administrative Areas dataset
 file_path <- tempfile(fileext = "rds")
 download.file(
@@ -136,7 +135,7 @@ 

Tutorial## old-style crs object detected; please recreate object with a recent sf::st_crs() ## old-style crs object detected; please recreate object with a recent sf::st_crs() ## old-style crs object detected; please recreate object with a recent sf::st_crs()

-
+
 # clip Malta's protected areas to the coastline
 mlt_pa_data <-
   mlt_pa_data %>%
@@ -151,13 +150,13 @@ 

Tutorial ## Warning: attribute variables are assumed to be spatially constant throughout ## all geometries

-
+
 

Now that we have finished cleaning the data, let’s preview the data. For more information on what these columns mean, please refer to the official manual (available in English, French, Spanish, and Russian).

-
+
 # print first six rows of the data
 head(mlt_pa_data)
## Simple feature collection with 6 features and 32 fields
@@ -181,16 +180,16 @@ 

Tutorial## # METADATAID <int>, SUB_LOC <chr>, PARENT_ISO <chr>, ISO3 <chr>, ## # SUPP_INFO <chr>, CONS_OBJ <chr>, GEOMETRY_TYPE <chr>, AREA_KM2 <dbl>, ## # geometry <GEOMETRY [m]>

-

We will now reproject the data to longitude/latitude coordinates (EPSG:4326) for visualization purposes.

-
+

We will now reproject the data to longitude/latitude coordinates (EPSG:4326) for visualization purposes.

+
 # reproject data
 mlt_pa_data <- st_transform(mlt_pa_data, 4326)

Next, we can plot a map showing the boundaries of Malta’s protected area system.

-
+
 # download basemap for making the map
-bg <- get_stamenmap(
+bg <- get_stadiamap(
   unname(st_bbox(mlt_pa_data)), zoom = 8,
-  maptype = "watercolor", force = TRUE
+  maptype = "stamen_terrain_background", force = TRUE
 )
 
 # print map
@@ -199,7 +198,7 @@ 

Tutorialtheme(axis.title = element_blank())

We can also create a histogram showing the year when each protected area was established.

-
+
 hist(
   mlt_pa_data$STATUS_YR,
   main = "Malta's protected areas",
@@ -207,7 +206,7 @@ 

Tutorial)

Now let’s calculate some statistics. We can calculate the total amount of land and ocean inside Malta’s protected area system (km2).

-
+
 # calculate total amount of area inside protected areas (km^2)
 statistic <-
   mlt_pa_data %>%
@@ -227,7 +226,7 @@ 

Tutorial## 2 terrestrial 84.9 ## 3 partial 13.1

We can also calculate the percentage of land inside its protected area system that are managed under different categories (i.e. using the protected area management categories defined by The International Union for Conservation of Nature).

-
+
 # calculate percentage of land inside protected areas (km^2)
 statistic <-
   mlt_pa_data %>%
@@ -251,7 +250,7 @@ 

Tutorial## 5 III 0.191 0.00451 ## 6 Ia 0.145 0.00343

We can also plot a map showing Malta’s protected areas and color each area according to it’s management category.

-
+
 ggmap(bg) +
 geom_sf(aes(fill = IUCN_CAT), data = mlt_pa_data, inherit.aes = FALSE) +
 theme(axis.title = element_blank(), legend.position = "bottom")
@@ -260,8 +259,8 @@

Tutorial -

The wdpar R package can be used to clean large datasets assuming that sufficient computational resources and time are available. Indeed, it can clean data spanning large countries, multiple countries, and even the full global datatset. When processing the full global dataset, it is recommended to use a computer system with at least 32 GB RAM available and to allow for at least one full day for the data cleaning procedures to complete. It is also recommended to avoid using the computer system for any other tasks while the data cleaning procedures are being completed, because they are very computationally intensive. Additionally, when processing large datasets – and especially for the global dataset – it is strongly recommended to disable the procedure for erasing overlapping areas. This is because the built-in procedure for erasing overlaps is very time consuming when processing many protected areas, so that information on each protected area can be output (e.g. IUCN category, year established). Instead, when cleaning large datasets, it is recommended to run the data cleaning procedures with the procedure for erasing overlapping areas disabled (i.e. with erase_overlaps = FALSE). After the data cleaning procedures have completed, the protected area data can be manually dissolved to remove overlapping areas (e.g. using wdpa_dissolve()). For an example of these procedures, please see below.

-
+

The wdpar R package can be used to clean large datasets assuming that sufficient computational resources and time are available. Indeed, it can clean data spanning large countries, multiple countries, and even the full global dataset. When processing the full global dataset, it is recommended to use a computer system with at least 32 GB RAM available and to allow for at least one full day for the data cleaning procedures to complete. It is also recommended to avoid using the computer system for any other tasks while the data cleaning procedures are being completed, because they are very computationally intensive. Additionally, when processing large datasets – and especially for the global dataset – it is strongly recommended to disable the procedure for erasing overlapping areas. This is because the built-in procedure for erasing overlaps is very time consuming when processing many protected areas, so that information on each protected area can be output (e.g. IUCN category, year established). Instead, when cleaning large datasets, it is recommended to run the data cleaning procedures with the procedure for erasing overlapping areas disabled (i.e. with erase_overlaps = FALSE). After the data cleaning procedures have completed, the protected area data can be manually dissolved to remove overlapping areas (e.g. using wdpa_dissolve()). For an example of these procedures, please see below.

+
 # download protected area data for multiple of countries
 ## (i.e. Portugal, Spain, France)
 raw_pa_data <-
@@ -269,10 +268,7 @@ 
-
## ! importing local data (version Dec 2021); use "force=TRUE" if you need latest version.
-## ! importing local data (version Dec 2021); use "force=TRUE" if you need latest version.
-## ! importing local data (version Dec 2021); use "force=TRUE" if you need latest version.
-
+
 # clean protected area data (with procedure for erasing overlaps disabled)
 full_pa_data <- wdpa_clean(raw_pa_data, erase_overlaps = FALSE)
 
@@ -296,7 +292,7 @@ 
-
+
 # calculate total area
 print(st_area(pa_data))
## 8040807030 [m^2]
@@ -306,7 +302,7 @@

The default parameters for the data cleaning procedures are well suited for national-scale analyses. Although these parameters reduce memory requirements and the time needed to complete the data cleaning procedures, they can produce protected area boundaries that appear overly “blocky” – lacking smooth edges – when viewed at finer scales. As such, it is strongly recommended to increase the level of spatial precision when cleaning data for local scale analyses (via the geometry_precision parameter of the wdpa_clean() function).

Here we will explore the consequences of using the default parameters for the data cleaning procedures when working at a local scale. This will help illustrate why it can be important to adjust the spatial precision of the data cleaning procedures. To begin with, we will obtain data for a small protected area. Specifically, we will extract a protected area from the Malta dataset we downloaded earlier.

-
+
 # find id for smallest reserve in cleaned dataset
 mlt_reserve_id <- mlt_pa_data$WDPAID[which.min(mlt_pa_data$AREA_KM2)]
 
@@ -332,12 +328,12 @@ 
-
+
 # visualize data
 plot(mlt_raw_reserve_data[, 1])
-

+

We can see that the boundary for this protected area has a high level of detail. This suggests that the protected area data is available at a resolution that is sufficient to permit local scale analyses. To help understand the consequences of cleaning data with the default parameters, we will clean this dataset using the default parameters.

-
+
 # clean the data with default parameters
 mlt_default_cleaned_reserve_data <- wdpa_clean(mlt_raw_reserve_data)
 
@@ -359,12 +355,12 @@ 
-
+
 # visualize data
 plot(mlt_default_cleaned_reserve_data[, 1])
-

+

After cleaning the data with the default parameters, we can see that the boundary of the protected area is no longer highly detailed. For example, the smooth edges of the raw protected area data have been replaced with sharp, blocky edges. As such, subsequent analysis performed at the local scale – such as calculating the spatial extent of land cover types within this single protected area – might not be sufficiently precise. Now, let’s clean the data using parameters that are well suited for local scale analysis.

-
+
 # clean the data with default parameters
 mlt_fixed_cleaned_reserve_data <- wdpa_clean(
   mlt_raw_reserve_data, geometry_precision = 10000
@@ -388,10 +384,10 @@ 
-
+
 # visualize data
 plot(mlt_fixed_cleaned_reserve_data[, 1])
-

+

Here, we specified that the spatial data processing should be performed at a much greater level of precision (using the geometry_precision parameter). As a consequence, we can see – after applying the data cleaning procedures – that the protected area boundary still retains a high level of detail. This means that the cleaned protected area data is more suitable for local scale analysis. If a greater level of detail is required, the level of precision could be increased further. Note that the maximum level of detail that can be achieved in the cleaned data is limited by the level of detail in the raw data. This means that increasing the level of precision beyond a certain point will have no impact on the cleaned data, because the raw data do not provide sufficient detail for the increased precision to alter the spatial data processing.

@@ -406,7 +402,7 @@

Additional datasetsCollaborative Australian Protected Area Database (CAPAD) +
  • Collaborative Australian Protected Area Database (CAPAD)
  • Conservation Areas Reporting and Tracking System (CARTS)
  • @@ -421,7 +417,7 @@

    Additional datasetsCitation

    Please cite the wdpar R package and the relevant databases in publications. To see citation details, use the code:

    -
    +
     citation("wdpar")
    diff --git a/docs/articles/wdpar_files/figure-html/unnamed-chunk-13-1.png b/docs/articles/wdpar_files/figure-html/unnamed-chunk-13-1.png index f76124d..df2b9a2 100644 Binary files a/docs/articles/wdpar_files/figure-html/unnamed-chunk-13-1.png and b/docs/articles/wdpar_files/figure-html/unnamed-chunk-13-1.png differ diff --git a/docs/articles/wdpar_files/figure-html/unnamed-chunk-15-1.png b/docs/articles/wdpar_files/figure-html/unnamed-chunk-16-1.png similarity index 100% rename from docs/articles/wdpar_files/figure-html/unnamed-chunk-15-1.png rename to docs/articles/wdpar_files/figure-html/unnamed-chunk-16-1.png diff --git a/docs/articles/wdpar_files/figure-html/unnamed-chunk-17-1.png b/docs/articles/wdpar_files/figure-html/unnamed-chunk-17-1.png deleted file mode 100644 index b821ab5..0000000 Binary files a/docs/articles/wdpar_files/figure-html/unnamed-chunk-17-1.png and /dev/null differ diff --git a/docs/articles/wdpar_files/figure-html/unnamed-chunk-18-1.png b/docs/articles/wdpar_files/figure-html/unnamed-chunk-18-1.png index 93b67f2..b821ab5 100644 Binary files a/docs/articles/wdpar_files/figure-html/unnamed-chunk-18-1.png and b/docs/articles/wdpar_files/figure-html/unnamed-chunk-18-1.png differ diff --git a/docs/articles/wdpar_files/figure-html/unnamed-chunk-19-1.png b/docs/articles/wdpar_files/figure-html/unnamed-chunk-19-1.png new file mode 100644 index 0000000..93b67f2 Binary files /dev/null and b/docs/articles/wdpar_files/figure-html/unnamed-chunk-19-1.png differ diff --git a/docs/articles/wdpar_files/figure-html/unnamed-chunk-9-1.png b/docs/articles/wdpar_files/figure-html/unnamed-chunk-9-1.png index de8ef39..83a339c 100644 Binary files a/docs/articles/wdpar_files/figure-html/unnamed-chunk-9-1.png and b/docs/articles/wdpar_files/figure-html/unnamed-chunk-9-1.png differ diff --git a/docs/authors.html b/docs/authors.html index ca1d1d8..efbb56b 100644 --- a/docs/authors.html +++ b/docs/authors.html @@ -17,7 +17,7 @@ wdpar - 1.3.7.1 + 1.3.7.2
    @@ -78,21 +78,21 @@

    Citation

    pages = {4594}, doi = {10.21105/joss.04594}, }
    -

    UNEP-WCMC and IUCN (2023) Protected Planet: The World Database on Protected Areas (WDPA), [insert month/year of the version downloaded], Cambridge, UK: UNEP-WCMC and IUCN. Available at: www.protectedplanet.net.

    +

    UNEP-WCMC and IUCN (2024) Protected Planet: The World Database on Protected Areas (WDPA), [insert month/year of the version downloaded], Cambridge, UK: UNEP-WCMC and IUCN. Available at: www.protectedplanet.net.

    @Manual{,
       title = {Protected Planet: The World Database on Protected Areas (WDPA)},
       author = {{UNEP-WCMC} and {IUCN}},
    -  year = {2023},
    +  year = {2024},
       version = {[insert month/year of the version downloaded]},
       publisher = {UNEP-WCMC and IUCN},
       address = {Cambridge, UK},
       url = {https://www.protectedplanet.net/en},
     }
    -

    UNEP-WCMC and IUCN (2023) Protected Planet: The world database on other effective area-based conservation measures, [insert month/year of the version downloaded], Cambridge, UK: UNEP-WCMC and IUCN. Available at: www.protectedplanet.net.

    +

    UNEP-WCMC and IUCN (2024) Protected Planet: The world database on other effective area-based conservation measures, [insert month/year of the version downloaded], Cambridge, UK: UNEP-WCMC and IUCN. Available at: www.protectedplanet.net.

    @Manual{,
       title = {Protected Planet: The world database on other effective area-based conservation measures},
       author = {{UNEP-WCMC} and {IUCN}},
    -  year = {2023},
    +  year = {2024},
       version = {[insert month/year of the version downloaded]},
       publisher = {UNEP-WCMC and IUCN},
       address = {Cambridge, UK},
    diff --git a/docs/index.html b/docs/index.html
    index f219119..2ec029e 100644
    --- a/docs/index.html
    +++ b/docs/index.html
    @@ -39,7 +39,7 @@
           
           
             wdpar
    -        1.3.7.1
    +        1.3.7.2
           
         
    @@ -95,8 +95,7 @@

    Package installationinstall.packages("wdpar", repos = "https://cran.rstudio.com/")

    Alternatively, the latest developmental version can be installed using the following R code. Please note that while developmental versions may contain additional features not present in the official version, they may also contain coding errors.

    -if (!require(remotes))
    -  install.packages("remotes")
    +if (!require(remotes)) install.packages("remotes")
     remotes::install_github("prioritizr/wdpar")

    Note that the prepr R package has system dependencies that need to be installed before the package itself can be installed (see below for platform-specific instructions).

    @@ -130,7 +128,7 @@
    -MacOS +macOS

    The gmp, mpfr, and gdal libraries are required. The easiest way to install these libraries is using HomeBrew. After installing HomeBrew, these libraries can be installed using the following commands in the system terminal:

    brew install pkg-config
    @@ -165,7 +163,7 @@ 

    Usage
    ## Simple feature collection with 6 features and 32 fields
     ## Geometry type: MULTIPOLYGON
     ## Dimension:     XY
    -## Bounding box:  xmin: 1382584 ymin: 4280853 xmax: 1399759 ymax: 4299615
    +## Bounding box:  xmin: 1382584 ymin: 4280853 xmax: 1394227 ymax: 4299615
     ## Projected CRS: +proj=cea +lon_0=0 +lat_ts=30 +x_0=0 +y_0=0 +datum=WGS84 +ellps=WGS84 +units=m +no_defs
     ## Precision:     1500 
     ## # A tibble: 6 × 33
    @@ -174,9 +172,9 @@ 

    Usage ## 1 194425 194425 PA 'Il-… 'Il-Gżej… Rise… Nature R… National Ia ## 2 194420 194420 PA Filf… Filfla Rise… Nature R… National Ia ## 3 555588631 555588631 PA Il-M… Il-Majji… Park… National… National II -## 4 174757 174757 PA Il-Ġ… Il-Ġonna… List… List of … National III -## 5 174758 174758 PA Bidn… Bidnija,… List… List of … National III -## 6 194415 194415 PA 'Il-… 'Il-Ġonn… List… List of … National III +## 4 174758 174758 PA Bidn… Bidnija,… List… List of … National III +## 5 194415 194415 PA 'Il-… 'Il-Ġonn… List… List of … National III +## 6 194418 194418 PA Il-B… Il-Ballu… List… List of … National III ## # ℹ 24 more variables: INT_CRIT <chr>, MARINE <chr>, REP_M_AREA <dbl>, ## # GIS_M_AREA <dbl>, REP_AREA <dbl>, GIS_AREA <dbl>, NO_TAKE <chr>, ## # NO_TK_AREA <dbl>, STATUS <chr>, STATUS_YR <dbl>, GOV_TYPE <chr>, @@ -190,8 +188,10 @@

    Usage mlt_pa_data <- st_transform(mlt_pa_data, 4326) # download basemap imagery -bg <- get_stamenmap(unname(st_bbox(mlt_pa_data)), zoom = 8, - maptype = "watercolor", force = TRUE) +bg <- get_stadiamap( + unname(st_bbox(mlt_pa_data)), zoom = 8, + maptype = "stamen_terrain_background", force = TRUE +) # make map ggmap(bg) + diff --git a/docs/news/index.html b/docs/news/index.html index d067fdc..bb9bda6 100644 --- a/docs/news/index.html +++ b/docs/news/index.html @@ -17,7 +17,7 @@ wdpar - 1.3.7.1 + 1.3.7.2

    @@ -50,6 +50,16 @@

    Changelog

    Source: NEWS.md

    +
    + +
    • Update wdpa_fetch() to use the chromote package to handle web scrapping (instead of webdriver).
    • +
    • Update wdpa_fetch() so that it can download country-level data in either shapefile or file geodatabase format (using the new datatype parameter). Since file geodatabase data appears to be more robust, wdpa_fetch() now defaults to downloading data in file geodatabase format.
    • +
    • Update wdpa_clean() to standardize column names so that cleaning either shapefile or file geodatabase data results in the same output.
    • +
    • Update wdpa_clean() so that it removes leading/trailing white space characters from the "MANG_PLAN" field.
    • +
    • Fix bug in wdpa_read() that causes output objects to contain no columns.
    • +
    • Update README and vignette to be compatible with updates to ggmap package.
    • +
    • Remove withr package from DESCRIPTION because it is not used.
    • +
    • Update tests to accommodate corrupted data from Protected Planet.
    @@ -134,7 +144,7 @@
    • CRAN release.
    • Update read_sf_n to import data faster.
    • -
    • Remove withr R package from DESCRIPTION because it is not used.
    • +
    • Remove withr package from DESCRIPTION because it is not used.
    @@ -152,7 +162,7 @@
    • Fix bug in wdpa_read to ensure that all data from global database. This is a bug previously meant that protected areas denoted with polygon geometry data were not imported. It is strongly recommended that users double check processed versions of the global database to verify correctness.
    • Add example script for downloading and cleaning data (see inst/scripts/global-example-script.R)
    • -
    • New st_repair_geometry function to repair geometry using a combination of sf::st_make_valid and prepr::st_prepair. This function is now used by the wdpa_clean function to augment data cleaning procedures.
    • +
    • New st_repair_geometry function to repair geometry using a combination of sf::st_make_valid and prepr::st_prepair. This function is now used by the wdpa_clean function to augment data cleaning procedures.
    • Update wdpa_url and wdpa_fetch to have a page_wait parameter to specify the wait time for loading web pages when finding the download URLs for datasets (#39).
    • Add dplyr package to Suggests because it is used in an example.
    diff --git a/docs/pkgdown.yml b/docs/pkgdown.yml index 6d24ea6..6f1cb81 100644 --- a/docs/pkgdown.yml +++ b/docs/pkgdown.yml @@ -3,5 +3,5 @@ pkgdown: 2.0.7 pkgdown_sha: ~ articles: wdpar: wdpar.html -last_built: 2023-11-01T02:51Z +last_built: 2024-04-25T00:10Z diff --git a/docs/reference/figures/README-readme-map-1.png b/docs/reference/figures/README-readme-map-1.png index fc3fa30..95546f4 100644 Binary files a/docs/reference/figures/README-readme-map-1.png and b/docs/reference/figures/README-readme-map-1.png differ diff --git a/docs/reference/index.html b/docs/reference/index.html index ddd3a77..cfe085d 100644 --- a/docs/reference/index.html +++ b/docs/reference/index.html @@ -17,7 +17,7 @@ wdpar - 1.3.7.1 + 1.3.7.2
    diff --git a/docs/reference/st_erase_overlaps.html b/docs/reference/st_erase_overlaps.html index cd3b7d5..6f1b09a 100644 --- a/docs/reference/st_erase_overlaps.html +++ b/docs/reference/st_erase_overlaps.html @@ -17,7 +17,7 @@ wdpar - 1.3.7.1 + 1.3.7.2
    diff --git a/docs/reference/st_repair_geometry.html b/docs/reference/st_repair_geometry.html index 7946a01..3eea255 100644 --- a/docs/reference/st_repair_geometry.html +++ b/docs/reference/st_repair_geometry.html @@ -17,7 +17,7 @@ wdpar - 1.3.7.1 + 1.3.7.2
    diff --git a/docs/reference/wdpa_clean-1.png b/docs/reference/wdpa_clean-1.png index 37f9e0f..bc38b29 100644 Binary files a/docs/reference/wdpa_clean-1.png and b/docs/reference/wdpa_clean-1.png differ diff --git a/docs/reference/wdpa_clean.html b/docs/reference/wdpa_clean.html index 846a8c5..c47e4fb 100644 --- a/docs/reference/wdpa_clean.html +++ b/docs/reference/wdpa_clean.html @@ -25,7 +25,7 @@ wdpar - 1.3.7.1 + 1.3.7.2
    @@ -176,6 +176,11 @@

    Details

    Organization (UNESCO) Biosphere Reserves (Coetzer et al. 2014). This step is only performed if the argument to exclude_unesco is TRUE.

    +
  • Standardize column names. This is important so that data +imported as in shapefile or file geodatabase format have the +same column names. Specifically, if present, the "PARENT_ISO3" field is +renamed to "PARENT_ISO" and the "SHAPE" field is renamed to +"geometry".

  • Create a field ("GEOMETRY_TYPE") indicating if areas are represented as point localities ("POINT") or as polygons ("POLYGON").

  • @@ -194,7 +199,7 @@

    Details

    et al. 2013).

  • Snap the geometries to a grid to fix any remaining geometry issues (using argument to snap_tolerance and -lwgeom::st_snap_to_grid()).

  • +lwgeom::st_snap_to_grid()).

  • Repair any invalid geometries that have manifested (using st_repair_geometry()).

  • Simplify the protected area geometries to reduce computational burden @@ -222,6 +227,8 @@

    Details

    meters).

  • The size of areas are calculated in square kilometers and stored in the field "AREA_KM2".

  • +
  • Trimming extra leading or trailing white space characters +from the "MANG_PLAN" field (e.g., " ", "\n", "\r").

  • diff --git a/docs/reference/wdpa_fetch-1.png b/docs/reference/wdpa_fetch-1.png index 8e93c8d..f9af7ba 100644 Binary files a/docs/reference/wdpa_fetch-1.png and b/docs/reference/wdpa_fetch-1.png differ diff --git a/docs/reference/wdpa_fetch.html b/docs/reference/wdpa_fetch.html index e2ea4c1..60bb688 100644 --- a/docs/reference/wdpa_fetch.html +++ b/docs/reference/wdpa_fetch.html @@ -22,7 +22,7 @@ wdpar - 1.3.7.1 + 1.3.7.2
    @@ -74,6 +74,7 @@

    Fetch data

    check_version = TRUE, n = NULL, page_wait = 2, + datatype = "gdb", verbose = interactive() )

    @@ -135,6 +136,13 @@

    Arguments

    To avoid this, users can try specifying a greater value (e.g. 5 seconds).

    +
    datatype
    +

    character denoting the file format for which to download +protected area data. Available options include: ("shp") shapefile format +and ("gdb") file geodatabase format. Defaults to `"gdb". +Note that global data are only available in file geodatabase format.

    + +
    verbose

    logical should a progress on downloading data be reported? Defaults to TRUE in an interactive session, otherwise @@ -181,13 +189,27 @@

    Data source

    Troubleshooting

    -

    This function will sometimes return the error message -PhantomJS signals port = 4567 is already in use. -This error message can occur when you have previously run the function and -it threw an error, or it terminated early. -It can also occur when attempting to run the the function in multiple -sessions on the same computer. -To address this issue, you will need to restart your computer.

    +

    The function requires a Chromium-based browser +(e.g., Google Chrome, Chromium, or Brave) to be installed. +This is because it uses the chromote to find the URL +for downloading data from Protected Planet. +If you don't have one of these browsers installed, then please try +installing Google Chrome. +If you do have one of these browsers installed and this function +throws an error indicating that it can't find the browser, +try setting the CHROMOTE_CHROME environment variable to the +file path of the executable. For example, you could do this with:

    +

    Sys.setenv(CHROMOTE_CHROME = "INSERT_FILE_PATH_HERE.exe")

    +

    Also, the function will sometimes produce a message +that complains about a handle_read_frame error. Please understand +that this message is, in fact, not an error and can be safely ignored +(see https://github.com/rstudio/chromote/pull/111). +As such, if you see this message when running the function, +you can assume that the function still worked correctly. +For reference, the misleading message will look something like this:

    +

    [2024-04-23 12:06:36] [error] handle_read_frame error: websocketpp.transport:7 (End of File)

    +

    For further help with troubleshooting, please refer to the documentation +for the chromote package (https://rstudio.github.io/chromote/).

    References

    @@ -216,24 +238,24 @@

    Examples

    #> Geodetic CRS: WGS 84 #> # A tibble: 45 × 31 #> WDPAID WDPA_PID PA_DEF NAME ORIG_NAME DESIG DESIG_ENG DESIG_TYPE IUCN_CAT -#> * <dbl> <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr> -#> 1 18107 18107 1 Ruggell… Ruggelle… Natu… Nature R… National Ia -#> 2 18109 18109 1 Schwabb… Schwabbr… Natu… Nature R… National Ia -#> 3 30747 30747 1 Gamprin… Gamprine… Natu… Nature R… National Ia +#> <dbl> <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr> +#> 1 68093 68093 1 Ruggell… Ruggelle… Rams… Ramsar S… Internati… Ia +#> 2 326426 326426 1 Garsäll… Garsälli… Wald… Forest R… National Ib +#> 3 326453 326453 1 Stachle… Stachler… Sond… Protecte… National IV #> 4 30750 30750 1 Äulehäg Äulehäg Natu… Nature R… National Ia -#> 5 30752 30752 1 Triesne… Triesner… Natu… Nature R… National Ia -#> 6 30753 30753 1 Wisanels Wisanels Natu… Nature R… National Ia -#> 7 30754 30754 1 Birka Birka Natu… Nature R… National Ia -#> 8 30755 30755 1 Schneck… Schnecke… Natu… Nature R… National Ia -#> 9 30756 30756 1 Au Au Natu… Nature R… National Ia -#> 10 30757 30757 1 Pflanze… Pflanzen… Gesc… Protecte… National V +#> 5 326441 326441 1 Ganada … Ganada SO Sond… Protecte… National IV +#> 6 326443 326443 1 Hälos Hälos Sond… Protecte… National IV +#> 7 18109 18109 1 Schwabb… Schwabbr… Natu… Nature R… National Ia +#> 8 326432 326432 1 Steger … Steger B… Wald… Forest R… National Ib +#> 9 30752 30752 1 Triesne… Triesner… Natu… Nature R… National Ia +#> 10 326433 326433 1 Ganada … Ganada WR Wald… Forest R… National Ib #> # ℹ 35 more rows #> # ℹ 22 more variables: INT_CRIT <chr>, MARINE <chr>, REP_M_AREA <dbl>, #> # GIS_M_AREA <dbl>, REP_AREA <dbl>, GIS_AREA <dbl>, NO_TAKE <chr>, #> # NO_TK_AREA <dbl>, STATUS <chr>, STATUS_YR <int>, GOV_TYPE <chr>, #> # OWN_TYPE <chr>, MANG_AUTH <chr>, MANG_PLAN <chr>, VERIF <chr>, -#> # METADATAID <int>, SUB_LOC <chr>, PARENT_ISO <chr>, ISO3 <chr>, -#> # SUPP_INFO <chr>, CONS_OBJ <chr>, geometry <MULTIPOLYGON [°]> +#> # METADATAID <int>, SUB_LOC <chr>, PARENT_ISO3 <chr>, ISO3 <chr>, +#> # SUPP_INFO <chr>, CONS_OBJ <chr>, SHAPE <MULTIPOLYGON [°]> # plot data plot(lie_raw_data) @@ -283,23 +305,23 @@

    Examples

    #> # A tibble: 61 × 31 #> WDPAID WDPA_PID PA_DEF NAME ORIG_NAME DESIG DESIG_ENG DESIG_TYPE IUCN_CAT #> <dbl> <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr> -#> 1 18107 18107 1 Ruggell… Ruggelle… Natu… Nature R… National Ia -#> 2 18109 18109 1 Schwabb… Schwabbr… Natu… Nature R… National Ia -#> 3 30747 30747 1 Gamprin… Gamprine… Natu… Nature R… National Ia +#> 1 68093 68093 1 Ruggell… Ruggelle… Rams… Ramsar S… Internati… Ia +#> 2 326426 326426 1 Garsäll… Garsälli… Wald… Forest R… National Ib +#> 3 326453 326453 1 Stachle… Stachler… Sond… Protecte… National IV #> 4 30750 30750 1 Äulehäg Äulehäg Natu… Nature R… National Ia -#> 5 30752 30752 1 Triesne… Triesner… Natu… Nature R… National Ia -#> 6 30753 30753 1 Wisanels Wisanels Natu… Nature R… National Ia -#> 7 30754 30754 1 Birka Birka Natu… Nature R… National Ia -#> 8 30755 30755 1 Schneck… Schnecke… Natu… Nature R… National Ia -#> 9 30756 30756 1 Au Au Natu… Nature R… National Ia -#> 10 30757 30757 1 Pflanze… Pflanzen… Gesc… Protecte… National V +#> 5 326441 326441 1 Ganada … Ganada SO Sond… Protecte… National IV +#> 6 326443 326443 1 Hälos Hälos Sond… Protecte… National IV +#> 7 18109 18109 1 Schwabb… Schwabbr… Natu… Nature R… National Ia +#> 8 326432 326432 1 Steger … Steger B… Wald… Forest R… National Ib +#> 9 30752 30752 1 Triesne… Triesner… Natu… Nature R… National Ia +#> 10 326433 326433 1 Ganada … Ganada WR Wald… Forest R… National Ib #> # ℹ 51 more rows #> # ℹ 22 more variables: INT_CRIT <chr>, MARINE <chr>, REP_M_AREA <dbl>, #> # GIS_M_AREA <dbl>, REP_AREA <dbl>, GIS_AREA <dbl>, NO_TAKE <chr>, #> # NO_TK_AREA <dbl>, STATUS <chr>, STATUS_YR <int>, GOV_TYPE <chr>, #> # OWN_TYPE <chr>, MANG_AUTH <chr>, MANG_PLAN <chr>, VERIF <chr>, -#> # METADATAID <int>, SUB_LOC <chr>, PARENT_ISO <chr>, ISO3 <chr>, -#> # SUPP_INFO <chr>, CONS_OBJ <chr>, geometry <MULTIPOLYGON [°]> +#> # METADATAID <int>, SUB_LOC <chr>, PARENT_ISO3 <chr>, ISO3 <chr>, +#> # SUPP_INFO <chr>, CONS_OBJ <chr>, SHAPE <MULTIPOLYGON [°]> # }
    diff --git a/docs/reference/wdpa_latest_version.html b/docs/reference/wdpa_latest_version.html index 647576c..300545d 100644 --- a/docs/reference/wdpa_latest_version.html +++ b/docs/reference/wdpa_latest_version.html @@ -22,7 +22,7 @@ wdpar - 1.3.7.1 + 1.3.7.2
    @@ -87,7 +87,7 @@

    Examples

    # \dontrun{
     # find the latest version
     wdpa_latest_version()
    -#> [1] "Oct2023"
    +#> [1] "Apr2024"
     # }
     
     
    diff --git a/docs/reference/wdpa_read-1.png b/docs/reference/wdpa_read-1.png index 8e93c8d..f9af7ba 100644 Binary files a/docs/reference/wdpa_read-1.png and b/docs/reference/wdpa_read-1.png differ diff --git a/docs/reference/wdpa_read.html b/docs/reference/wdpa_read.html index a50667f..77d8e0d 100644 --- a/docs/reference/wdpa_read.html +++ b/docs/reference/wdpa_read.html @@ -22,7 +22,7 @@ wdpar - 1.3.7.1 + 1.3.7.2
    diff --git a/docs/reference/wdpa_url.html b/docs/reference/wdpa_url.html index 9121ce9..69fe71d 100644 --- a/docs/reference/wdpa_url.html +++ b/docs/reference/wdpa_url.html @@ -23,7 +23,7 @@ wdpar - 1.3.7.1 + 1.3.7.2
    @@ -68,7 +68,7 @@

    Download URL

    -
    wdpa_url(x, wait = FALSE, page_wait = 2)
    +
    wdpa_url(x, wait = FALSE, page_wait = 2, datatype = "gdb")
    @@ -101,6 +101,13 @@

    Arguments

    containing the message Error: Summary: NoSuchElement). To avoid this, users can try specifying a greater value (e.g. 5 seconds).

    + +
    datatype
    +

    character denoting the file format for which to download +protected area data. Available options include: ("shp") shapefile format +and ("gdb") file geodatabase format. Defaults to `"gdb". +Note that global data are only available in file geodatabase format.

    +

    Value

    @@ -119,12 +126,13 @@

    Examples

    # obtain url for New Zealand data nzl_url <- wdpa_url("New Zealand", wait = TRUE) print(nzl_url) -#> [1] "https://d1gam3xoknrgr2.cloudfront.net/current/WDPA_WDOECM_Oct2023_Public_NZL_shp.zip" +#> [1] "https://d1gam3xoknrgr2.cloudfront.net/current/WDPA_WDOECM_Apr2024_Public_NZL.zip" # obtain url for New Zealand data using its ISO3 code nzl_url <- wdpa_url("NZL", wait = TRUE) +#> Error in self$check_active(): Chromote has been closed. print(nzl_url) -#> [1] "https://d1gam3xoknrgr2.cloudfront.net/current/WDPA_WDOECM_Oct2023_Public_NZL_shp.zip" +#> [1] "https://d1gam3xoknrgr2.cloudfront.net/current/WDPA_WDOECM_Apr2024_Public_NZL.zip" # obtain url for global data global_url <- wdpa_url("global") diff --git a/docs/reference/wdpar.html b/docs/reference/wdpar.html index bb62b37..7c688ca 100644 --- a/docs/reference/wdpar.html +++ b/docs/reference/wdpar.html @@ -29,7 +29,7 @@ wdpar - 1.3.7.1 + 1.3.7.2
    @@ -93,6 +93,17 @@

    References

    (2015) Protected areas and global conservation of migratory birds. Science, 350: 1255--1258.

    + +
    +

    Author

    +

    Maintainer: Jeffrey O Hanson jeffrey.hanson@uqconnect.edu.au

    +
    -
    ## ! importing local data (version Jun 2022); use "force=TRUE" if you need latest version.

    Next, we will clean the data set. Briefly, the cleaning steps include: excluding protected areas that are not yet implemented, excluding protected areas with limited conservation value, replacing missing data codes (e.g. "0") with missing data values (i.e. NA), replacing protected areas represented as points with circular protected areas that correspond to their reported extent, repairing any topological issues with the geometries, and erasing overlapping areas. Please note that, by default, spatial data processing is performed at a scale suitable for national scale analyses (see below for recommendations for local scale analyses). For more information on the data cleaning procedures, see wdpa_clean().

    -
    # clean Malta data
    -mlt_pa_data <- wdpa_clean(mlt_raw_pa_data)
    +
    # clean Malta data
    +mlt_pa_data <- wdpa_clean(mlt_raw_pa_data)

    After cleaning the data set, we will perform an additional step that involves clipping the terrestrial protected areas to Malta’s coastline. Ideally, we would also clip the marine protected areas to Malta’s Exclusive Economic Zone (EEZ) but such data are not as easy to obtain on a per country basis (but see https://www.marineregions.org/eez.php)).

    -
    # download Malta boundary from Global Administrative Areas dataset
    -file_path <- tempfile(fileext = "rds")
    -download.file(
    -  "https://biogeo.ucdavis.edu/data/gadm3.6/Rsf/gadm36_MLT_0_sf.rds",
    -  file_path
    -)
    -
    -# import Malta's boundary
    -mlt_boundary_data <- readRDS(file_path)
    -
    -# repair any geometry issues, dissolve the border, reproject to same
    -# coordinate system as the protected area data, and repair the geometry again
    -mlt_boundary_data <-
    -  mlt_boundary_data %>%
    -  st_set_precision(1000) %>%
    -  sf::st_make_valid() %>%
    -  st_set_precision(1000) %>%
    -  st_combine() %>%
    -  st_union() %>%
    -  st_set_precision(1000) %>%
    -  sf::st_make_valid() %>%
    -  st_transform(st_crs(mlt_pa_data)) %>%
    -  sf::st_make_valid()
    +
    # download Malta boundary from Global Administrative Areas dataset
    +file_path <- tempfile(fileext = "rds")
    +download.file(
    +  "https://biogeo.ucdavis.edu/data/gadm3.6/Rsf/gadm36_MLT_0_sf.rds",
    +  file_path
    +)
    +
    +# import Malta's boundary
    +mlt_boundary_data <- readRDS(file_path)
    +
    +# repair any geometry issues, dissolve the border, reproject to same
    +# coordinate system as the protected area data, and repair the geometry again
    +mlt_boundary_data <-
    +  mlt_boundary_data %>%
    +  st_set_precision(1000) %>%
    +  sf::st_make_valid() %>%
    +  st_set_precision(1000) %>%
    +  st_combine() %>%
    +  st_union() %>%
    +  st_set_precision(1000) %>%
    +  sf::st_make_valid() %>%
    +  st_transform(st_crs(mlt_pa_data)) %>%
    +  sf::st_make_valid()
    ## old-style crs object detected; please recreate object with a recent sf::st_crs()
     ## old-style crs object detected; please recreate object with a recent sf::st_crs()
     ## old-style crs object detected; please recreate object with a recent sf::st_crs()
    -
    # clip Malta's protected areas to the coastline
    -mlt_pa_data <-
    -  mlt_pa_data %>%
    -  filter(MARINE == "terrestrial") %>%
    -  st_intersection(mlt_boundary_data) %>%
    -  rbind(mlt_pa_data %>%
    -        filter(MARINE == "marine") %>%
    -        st_difference(mlt_boundary_data)) %>%
    -  rbind(mlt_pa_data %>% filter(!MARINE %in% c("terrestrial", "marine")))
    +
    # clip Malta's protected areas to the coastline
    +mlt_pa_data <-
    +  mlt_pa_data %>%
    +  filter(MARINE == "terrestrial") %>%
    +  st_intersection(mlt_boundary_data) %>%
    +  rbind(mlt_pa_data %>%
    +        filter(MARINE == "marine") %>%
    +        st_difference(mlt_boundary_data)) %>%
    +  rbind(mlt_pa_data %>% filter(!MARINE %in% c("terrestrial", "marine")))
    ## Warning: attribute variables are assumed to be spatially constant throughout
     ## all geometries
     
     ## Warning: attribute variables are assumed to be spatially constant throughout
     ## all geometries
    -
    # recalculate the area of each protected area
    -mlt_pa_data <-
    -  mlt_pa_data %>%
    -  mutate(AREA_KM2 = as.numeric(st_area(.)) * 1e-6)
    +
    # recalculate the area of each protected area
    +mlt_pa_data <-
    +  mlt_pa_data %>%
    +  mutate(AREA_KM2 = as.numeric(st_area(.)) * 1e-6)

    Now that we have finished cleaning the data, let’s preview the data. For more information on what these columns mean, please refer to the official manual (available in English, French, Spanish, and Russian).

    -
    # print first six rows of the data
    -head(mlt_pa_data)
    +
    # print first six rows of the data
    +head(mlt_pa_data)
    ## Simple feature collection with 6 features and 32 fields
     ## Geometry type: GEOMETRY
     ## Dimension:     XY
    @@ -244,41 +243,41 @@ 

    Tutorial

    ## # METADATAID <int>, SUB_LOC <chr>, PARENT_ISO <chr>, ISO3 <chr>, ## # SUPP_INFO <chr>, CONS_OBJ <chr>, GEOMETRY_TYPE <chr>, AREA_KM2 <dbl>, ## # geometry <GEOMETRY [m]>
    -

    We will now reproject the data to longitude/latitude coordinates (EPSG:4326) for visualization purposes.

    -
    # reproject data
    -mlt_pa_data <- st_transform(mlt_pa_data, 4326)
    +

    We will now reproject the data to longitude/latitude coordinates (EPSG:4326) for visualization purposes.

    +
    # reproject data
    +mlt_pa_data <- st_transform(mlt_pa_data, 4326)

    Next, we can plot a map showing the boundaries of Malta’s protected area system.

    -
    # download basemap for making the map
    -bg <- get_stamenmap(
    -  unname(st_bbox(mlt_pa_data)), zoom = 8,
    -  maptype = "watercolor", force = TRUE
    -)
    -
    -# print map
    -ggmap(bg) +
    -geom_sf(data = mlt_pa_data, fill = "#31A35480", inherit.aes = FALSE) +
    -theme(axis.title = element_blank())
    -

    +
    # download basemap for making the map
    +bg <- get_stadiamap(
    +  unname(st_bbox(mlt_pa_data)), zoom = 8,
    +  maptype = "stamen_terrain_background", force = TRUE
    +)
    +
    +# print map
    +ggmap(bg) +
    +geom_sf(data = mlt_pa_data, fill = "#31A35480", inherit.aes = FALSE) +
    +theme(axis.title = element_blank())
    +

    We can also create a histogram showing the year when each protected area was established.

    -
    hist(
    -  mlt_pa_data$STATUS_YR,
    -  main = "Malta's protected areas",
    -  xlab = "Year established"
    -)
    +
    hist(
    +  mlt_pa_data$STATUS_YR,
    +  main = "Malta's protected areas",
    +  xlab = "Year established"
    +)

    Now let’s calculate some statistics. We can calculate the total amount of land and ocean inside Malta’s protected area system (km2).

    -
    # calculate total amount of area inside protected areas (km^2)
    -statistic <-
    -  mlt_pa_data %>%
    -  as.data.frame() %>%
    -  select(-geometry) %>%
    -  group_by(MARINE) %>%
    -  summarize(area_km = sum(AREA_KM2)) %>%
    -  ungroup() %>%
    -  arrange(desc(area_km))
    -
    -# print statistic
    -print(statistic)
    +
    # calculate total amount of area inside protected areas (km^2)
    +statistic <-
    +  mlt_pa_data %>%
    +  as.data.frame() %>%
    +  select(-geometry) %>%
    +  group_by(MARINE) %>%
    +  summarize(area_km = sum(AREA_KM2)) %>%
    +  ungroup() %>%
    +  arrange(desc(area_km))
    +
    +# print statistic
    +print(statistic)
    ## # A tibble: 3 × 2
     ##   MARINE      area_km
     ##   <chr>         <dbl>
    @@ -286,19 +285,19 @@ 

    Tutorial

    ## 2 terrestrial 84.9 ## 3 partial 13.1

    We can also calculate the percentage of land inside its protected area system that are managed under different categories (i.e. using the protected area management categories defined by The International Union for Conservation of Nature).

    -
    # calculate percentage of land inside protected areas (km^2)
    -statistic <-
    -  mlt_pa_data %>%
    -  as.data.frame() %>%
    -  select(-geometry) %>%
    -  group_by(IUCN_CAT) %>%
    -  summarize(area_km = sum(AREA_KM2)) %>%
    -  ungroup() %>%
    -  mutate(percentage = (area_km / sum(area_km)) * 100) %>%
    -  arrange(desc(area_km))
    -
    -# print statistic
    -print(statistic)
    +
    # calculate percentage of land inside protected areas (km^2)
    +statistic <-
    +  mlt_pa_data %>%
    +  as.data.frame() %>%
    +  select(-geometry) %>%
    +  group_by(IUCN_CAT) %>%
    +  summarize(area_km = sum(AREA_KM2)) %>%
    +  ungroup() %>%
    +  mutate(percentage = (area_km / sum(area_km)) * 100) %>%
    +  arrange(desc(area_km))
    +
    +# print statistic
    +print(statistic)
    ## # A tibble: 6 × 3
     ##   IUCN_CAT      area_km percentage
     ##   <chr>           <dbl>      <dbl>
    @@ -309,39 +308,36 @@ 

    Tutorial

    ## 5 III 0.191 0.00451 ## 6 Ia 0.145 0.00343

    We can also plot a map showing Malta’s protected areas and color each area according to it’s management category.

    -
    ggmap(bg) +
    -geom_sf(aes(fill = IUCN_CAT), data = mlt_pa_data, inherit.aes = FALSE) +
    -theme(axis.title = element_blank(), legend.position = "bottom")
    -

    +
    ggmap(bg) +
    +geom_sf(aes(fill = IUCN_CAT), data = mlt_pa_data, inherit.aes = FALSE) +
    +theme(axis.title = element_blank(), legend.position = "bottom")
    +

    @@ -450,7 +446,7 @@

    Additional datasets

  • Australia
  • The United States of America @@ -462,7 +458,7 @@

    Additional datasets

    Citation

    Please cite the wdpar R package and the relevant databases in publications. To see citation details, use the code:

    -
    citation("wdpar")
    +
    citation("wdpar")
    diff --git a/man/figures/README-readme-map-1.png b/man/figures/README-readme-map-1.png index fc3fa30..95546f4 100644 Binary files a/man/figures/README-readme-map-1.png and b/man/figures/README-readme-map-1.png differ diff --git a/man/wdpa_clean.Rd b/man/wdpa_clean.Rd index 38ff2d8..0a82826 100644 --- a/man/wdpa_clean.Rd +++ b/man/wdpa_clean.Rd @@ -100,6 +100,12 @@ Organization (UNESCO) Biosphere Reserves (Coetzer \emph{et al.} 2014). This step is only performed if the argument to \code{exclude_unesco} is \code{TRUE}. +\item Standardize column names. This is important so that data +imported as in shapefile or file geodatabase format have the +same column names. Specifically, if present, the \code{"PARENT_ISO3"} field is +renamed to "PARENT_ISO" and the "SHAPE" field is renamed to +\code{"geometry"}. + \item Create a field (\code{"GEOMETRY_TYPE"}) indicating if areas are represented as point localities (\code{"POINT"}) or as polygons (\code{"POLYGON"}). @@ -163,6 +169,8 @@ meters). \item The size of areas are calculated in square kilometers and stored in the field \code{"AREA_KM2"}. +\item Trimming extra leading or trailing white space characters +from the \code{"MANG_PLAN"} field (e.g., \code{" "}, \code{"\\n"}, \code{"\\r"}). } } \section{Recommended practices for large datasets}{ diff --git a/man/wdpa_fetch.Rd b/man/wdpa_fetch.Rd index c5ba798..b0f5b74 100644 --- a/man/wdpa_fetch.Rd +++ b/man/wdpa_fetch.Rd @@ -12,6 +12,7 @@ wdpa_fetch( check_version = TRUE, n = NULL, page_wait = 2, + datatype = "gdb", verbose = interactive() ) } @@ -57,6 +58,11 @@ with the default argument (e.g. resulting in an error containing the message \code{Error: Summary: NoSuchElement}). To avoid this, users can try specifying a greater value (e.g. 5 seconds).} +\item{datatype}{\code{character} denoting the file format for which to download +protected area data. Available options include: (\code{"shp"}) shapefile format +and (\code{"gdb"}) file geodatabase format. Defaults to `"gdb". +Note that global data are only available in file geodatabase format.} + \item{verbose}{\code{logical} should a progress on downloading data be reported? Defaults to \code{TRUE} in an interactive session, otherwise \code{FALSE}.} @@ -102,13 +108,33 @@ For more details on data conventions, please consult the official manual \section{Troubleshooting}{ -This function will sometimes return the error message -\verb{PhantomJS signals port = 4567 is already in use}. -This error message can occur when you have previously run the function and -it threw an error, or it terminated early. -It can also occur when attempting to run the the function in multiple -sessions on the same computer. -To address this issue, you will need to restart your computer. +The function requires a Chromium-based browser +(e.g., Google Chrome, Chromium, or Brave) to be installed. +This is because it uses the \pkg{chromote} to find the URL +for downloading data from Protected Planet. +If you don't have one of these browsers installed, then please try +installing Google Chrome. +If you do have one of these browsers installed and this function +throws an error indicating that it can't find the browser, +try setting the \code{CHROMOTE_CHROME} environment variable to the +file path of the executable. For example, you could do this with: + +\if{html}{\out{
    }}\preformatted{Sys.setenv(CHROMOTE_CHROME = "INSERT_FILE_PATH_HERE.exe") +}\if{html}{\out{
    }} + +Also, the function will sometimes produce a message +that complains about a \code{handle_read_frame} error. Please understand +that this message is, in fact, not an error and can be safely ignored +(see \url{https://github.com/rstudio/chromote/pull/111}). +As such, if you see this message when running the function, +you can assume that the function still worked correctly. +For reference, the misleading message will look something like this: + +\if{html}{\out{
    }}\preformatted{[2024-04-23 12:06:36] [error] handle_read_frame error: websocketpp.transport:7 (End of File) +}\if{html}{\out{
    }} + +For further help with troubleshooting, please refer to the documentation +for the \pkg{chromote} package (https://rstudio.github.io/chromote/). } \examples{ diff --git a/man/wdpa_url.Rd b/man/wdpa_url.Rd index af08407..4d8af6e 100644 --- a/man/wdpa_url.Rd +++ b/man/wdpa_url.Rd @@ -4,7 +4,7 @@ \alias{wdpa_url} \title{Download URL} \usage{ -wdpa_url(x, wait = FALSE, page_wait = 2) +wdpa_url(x, wait = FALSE, page_wait = 2, datatype = "gdb") } \arguments{ \item{x}{\code{character} country for desired data. This argument @@ -29,6 +29,11 @@ Users on slow internet connections may experience issues with the default argument (e.g. resulting in an error containing the message \code{Error: Summary: NoSuchElement}). To avoid this, users can try specifying a greater value (e.g. 5 seconds).} + +\item{datatype}{\code{character} denoting the file format for which to download +protected area data. Available options include: (\code{"shp"}) shapefile format +and (\code{"gdb"}) file geodatabase format. Defaults to `"gdb". +Note that global data are only available in file geodatabase format.} } \value{ \code{character} URL to download the data. diff --git a/man/wdpar.Rd b/man/wdpar.Rd index 50fd8f8..c638134 100644 --- a/man/wdpar.Rd +++ b/man/wdpar.Rd @@ -34,3 +34,16 @@ Runge CA, Watson JEM, Butchart HM, Hanson JO, Possingham HP & Fuller RA (2015) Protected areas and global conservation of migratory birds. \emph{Science}, \strong{350}: 1255--1258. } +\seealso{ +Useful links: +\itemize{ + \item \url{https://prioritizr.github.io/wdpar/} + \item \url{https://github.com/prioritizr/wdpar} + \item Report bugs at \url{https://github.com/prioritizr/wdpar/issues} +} + +} +\author{ +\strong{Maintainer}: Jeffrey O Hanson \email{jeffrey.hanson@uqconnect.edu.au} + +} diff --git a/tests/testthat/helper_skip_functions.R b/tests/testthat/helper_skip_functions.R index e59b12f..35f077f 100644 --- a/tests/testthat/helper_skip_functions.R +++ b/tests/testthat/helper_skip_functions.R @@ -5,8 +5,8 @@ skip_if_local_and_slow_internet <- function(x) { testthat::skip_if(x, paste("On local and slow internet")) } -skip_if_phantomjs_not_available <- function() { +skip_if_chrome_not_available <- function() { testthat::skip_if_not( - is_phantomjs_available, "phantomJS not available" + is_chrome_installed, "chrome not available" ) } diff --git a/tests/testthat/helper_variables.R b/tests/testthat/helper_variables.R index 35f1f3b..368e461 100644 --- a/tests/testthat/helper_variables.R +++ b/tests/testthat/helper_variables.R @@ -1,5 +1,5 @@ -# set default availability for phantomJS -is_phantomjs_available <- FALSE +# set default availability for chrome +is_chrome_installed <- FALSE if (interactive() && identical(Sys.getenv("NOT_CRAN"), "true")) { - is_phantomjs_available <- TRUE + is_chrome_installed <- TRUE } diff --git a/tests/testthat/test_000_setup.R b/tests/testthat/test_000_setup.R index f0dde5f..0ddbce6 100644 --- a/tests/testthat/test_000_setup.R +++ b/tests/testthat/test_000_setup.R @@ -1,14 +1,10 @@ context("setup") # initialize data -test_that("phantomjs", { +test_that("initialize_chrome", { skip_on_cran() - # check if phantomjs installed and install if needed - if (!isTRUE(wdpar:::has_phantomjs())) { - webdriver::install_phantomjs() - } - # update variable - is_phantomjs_available <<- wdpar:::has_phantomjs() + # check if chrome is installed + is_chrome_installed <<- wdpar:::is_chrome_available() # tests - expect_is(is_phantomjs_available, "logical") + expect_is(is_chrome_installed, "logical") }) diff --git a/tests/testthat/test_internal.R b/tests/testthat/test_internal.R index 22f0fc4..99c0cde 100644 --- a/tests/testthat/test_internal.R +++ b/tests/testthat/test_internal.R @@ -71,14 +71,14 @@ test_that("wdpa_file (country - different year same month)", { unlink(td) }) -test_that("wdpa_url (country)", { +test_that("wdpa_url (country) (shp)", { skip_on_cran() skip_if_not(curl::has_internet()) - skip_if_phantomjs_not_available() + skip_if_chrome_not_available() skip_on_github_workflow("Windows") skip_on_github_workflow("macOS") # verify that wdpa_url yields a result - x <- suppressWarnings(wdpa_url("MLT", wait = TRUE)) + x <- suppressWarnings(wdpa_url("MLT", wait = TRUE, datatype = "shp")) expect_is(x, "character") # verify that downloading the url yields a zipped shapefile f1 <- tempfile(fileext = ".zip") @@ -95,10 +95,35 @@ test_that("wdpa_url (country)", { unlink(f2, recursive = TRUE, force = TRUE) }) +test_that("wdpa_url (country) (gdb)", { + skip_on_cran() + skip_if_not(curl::has_internet()) + skip_if_chrome_not_available() + skip_on_github_workflow("Windows") + skip_on_github_workflow("macOS") + # verify that wdpa_url yields a result + x <- suppressWarnings(wdpa_url("MLT", wait = TRUE, datatype = "gdb")) + expect_is(x, "character") + # verify that downloading the url yields a zipped shapefile + f1 <- tempfile(fileext = ".zip") + f2 <- file.path(tempdir(), basename(tempfile())) + download_file(x, f1) + expect_true(file.exists(f1)) + unzip(f1, exdir = f2) + zip_path <- dir(f2, "^.*\\.zip$", recursive = TRUE, full.names = TRUE) + if (length(zip_path) > 0) + expect_length( + dir(f2, "^.*\\.gdb$", recursive = TRUE, include.dirs = TRUE), + 1 + ) + unlink(f1, recursive = TRUE, force = TRUE) + unlink(f2, recursive = TRUE, force = TRUE) +}) + test_that("wdpa_url (global)", { skip_on_cran() skip_if_not(curl::has_internet()) - skip_if_phantomjs_not_available() + skip_if_chrome_not_available() skip_on_github_workflow("Windows") skip_on_github_workflow("macOS") # verify that wdpa_url yields a result diff --git a/tests/testthat/test_wdpa_clean.R b/tests/testthat/test_wdpa_clean.R index 5a863c2..919b081 100644 --- a/tests/testthat/test_wdpa_clean.R +++ b/tests/testthat/test_wdpa_clean.R @@ -15,7 +15,7 @@ default_retain_status <- c("Designated", "Inscribed", "Established") test_that("single country with eez", { skip_on_cran() skip_if_not(curl::has_internet()) - skip_if_phantomjs_not_available() + skip_if_chrome_not_available() skip_on_github_workflow("Windows") skip_on_github_workflow("macOS") # fetch data @@ -40,7 +40,7 @@ test_that("single country with eez", { test_that("single country without eez", { skip_on_cran() skip_if_not(curl::has_internet()) - skip_if_phantomjs_not_available() + skip_if_chrome_not_available() skip_on_github_workflow("Windows") skip_on_github_workflow("macOS") # fetch data @@ -62,7 +62,7 @@ test_that("single country without eez", { test_that("single country with simplification", { skip_on_cran() skip_if_not(curl::has_internet()) - skip_if_phantomjs_not_available() + skip_if_chrome_not_available() skip_on_github_workflow("Windows") skip_on_github_workflow("macOS") # fetch data @@ -84,7 +84,7 @@ test_that("single country with simplification", { test_that("single country without overlap removal", { skip_on_cran() skip_if_not(curl::has_internet()) - skip_if_phantomjs_not_available() + skip_if_chrome_not_available() skip_on_github_workflow("Windows") skip_on_github_workflow("macOS") # fetch data @@ -106,12 +106,12 @@ test_that("single country without overlap removal", { test_that("country with MULTIPOINT protected areas", { skip_on_cran() skip_if_not(curl::has_internet()) - skip_if_phantomjs_not_available() + skip_if_chrome_not_available() skip_on_github_workflow("Windows") skip_on_github_workflow("macOS") # fetch data x <- wdpa_fetch("BOL", wait = TRUE, check_version = FALSE) - x_points <- vapply(x$geometry, inherits, logical(1), + x_points <- vapply(sf::st_geometry(x), inherits, logical(1), c("POINT", "MULTIPOINT")) x <- x[c(which(x_points), 15), , drop = FALSE] y <- suppressWarnings(wdpa_clean(x, erase_overlaps = FALSE)) @@ -122,7 +122,7 @@ test_that("country with MULTIPOINT protected areas", { test_that("country with MULTIPOLYGON protected area", { skip_on_cran() skip_if_not(curl::has_internet()) - skip_if_phantomjs_not_available() + skip_if_chrome_not_available() skip_on_github_workflow("Windows") skip_on_github_workflow("macOS") # fetch data @@ -132,15 +132,15 @@ test_that("country with MULTIPOLYGON protected area", { p1 <- x[x$WDPAID == 555592679, ] p2 <- y[y$WDPAID == 555592679, ] # test that polygons in multipolygon features are retained - expect_gt(length(sf::st_cast(p1$geometry, "POLYGON")), 1) - expect_gt(length(sf::st_cast(p2$geometry, "POLYGON")), 1) + expect_gt(length(sf::st_cast(sf::st_geometry(p1), "POLYGON")), 1) + expect_gt(length(sf::st_cast(sf::st_geometry(p2), "POLYGON")), 1) expect_true(all(y$STATUS %in% default_retain_status)) }) test_that("country with super invalid MULTIPOLYGON data", { skip_on_cran() skip_if_not(curl::has_internet()) - skip_if_phantomjs_not_available() + skip_if_chrome_not_available() skip_on_github_workflow("Windows") skip_on_github_workflow("macOS") x <- wdpa_fetch("GAB", wait = TRUE, check_version = FALSE) @@ -152,7 +152,7 @@ test_that("country with super invalid MULTIPOLYGON data", { test_that("geometries in non-geometry column", { skip_on_cran() skip_if_not(curl::has_internet()) - skip_if_phantomjs_not_available() + skip_if_chrome_not_available() skip_on_github_workflow("Windows") skip_on_github_workflow("macOS") x <- wdpa_fetch("GAB", wait = TRUE, check_version = FALSE) @@ -168,7 +168,7 @@ test_that("geometries in non-geometry column", { test_that("single country with no valid non-empty geometries", { skip_on_cran() skip_if_not(curl::has_internet()) - skip_if_phantomjs_not_available() + skip_if_chrome_not_available() skip_on_github_workflow("Windows") skip_on_github_workflow("macOS") x <- wdpa_clean( @@ -192,7 +192,7 @@ test_that("single country with no valid non-empty geometries", { test_that("retain UNESCO Biosphere reserves", { skip_on_cran() skip_if_not(curl::has_internet()) - skip_if_phantomjs_not_available() + skip_if_chrome_not_available() skip_on_github_workflow("Windows") skip_on_github_workflow("macOS") # fetch data @@ -215,7 +215,7 @@ test_that("retain UNESCO Biosphere reserves", { test_that("protected areas that turn into long rectangles without prepr", { skip_on_cran() skip_if_not(curl::has_internet()) - skip_if_phantomjs_not_available() + skip_if_chrome_not_available() skip_if_not_installed("prepr") skip_on_github_workflow("Windows") skip_on_github_workflow("macOS") @@ -231,14 +231,14 @@ test_that("protected areas that turn into long rectangles without prepr", { test_that("protected areas that massively increase in size without prepr", { skip_on_cran() skip_if_not(curl::has_internet()) - skip_if_phantomjs_not_available() + skip_if_chrome_not_available() skip_if_not_installed("prepr") skip_if_not_installed("dplyr") skip_on_github_workflow("Windows") skip_on_github_workflow("macOS") # fetch data ids <- c(23177, 12352, 555705343, 555705341, 555721495) - x <- wdpa_fetch("DZA", wait = TRUE, check_version = FALSE) + x <- wdpa_fetch("DZA", wait = TRUE, check_version = FALSE, datatype = "gdb") x <- x[x$WDPAID %in% ids, , drop = FALSE] # clean data y <- wdpa_clean(x, erase_overlaps = FALSE) @@ -249,7 +249,7 @@ test_that("protected areas that massively increase in size without prepr", { test_that("custom retain_status", { skip_on_cran() skip_if_not(curl::has_internet()) - skip_if_phantomjs_not_available() + skip_if_chrome_not_available() skip_on_github_workflow("Windows") skip_on_github_workflow("macOS") # fetch data @@ -272,7 +272,7 @@ test_that("custom retain_status", { test_that("NULL retain_status", { skip_on_cran() skip_if_not(curl::has_internet()) - skip_if_phantomjs_not_available() + skip_if_chrome_not_available() skip_on_github_workflow("Windows") skip_on_github_workflow("macOS") # fetch data @@ -293,7 +293,7 @@ test_that("NULL retain_status", { test_that("empty intersections", { skip_on_cran() skip_if_not(curl::has_internet()) - skip_if_phantomjs_not_available() + skip_if_chrome_not_available() skip_on_github_workflow("Windows") skip_on_github_workflow("macOS") # fetch data @@ -305,3 +305,32 @@ test_that("empty intersections", { expect_true(all(names(y) %in% wdpa_column_names)) expect_gt(nrow(y), 0) }) + + +test_that("shp and gdb produce same results", { + skip_on_cran() + skip_if_not(curl::has_internet()) + skip_if_chrome_not_available() + skip_on_github_workflow("Windows") + skip_on_github_workflow("macOS") + # fetch and clean data + x <- wdpa_clean( + suppressWarnings( + wdpa_fetch( + "MHL", wait = TRUE, verbose = TRUE, datatype = "shp", + check_version = FALSE, force = TRUE) + ) + ) + y <- wdpa_clean( + suppressWarnings( + wdpa_fetch( + "MHL", wait = TRUE, verbose = TRUE, datatype = "gdb", + check_version = FALSE, force = TRUE) + ) + ) + # sort data + x <- x[order(x$WDPAID), , drop = FALSE] + y <- y[order(y$WDPAID), , drop = FALSE] + # run tests + expect_equal(x, y) +}) diff --git a/tests/testthat/test_wdpa_fetch.R b/tests/testthat/test_wdpa_fetch.R index 5c655d0..ad542ad 100644 --- a/tests/testthat/test_wdpa_fetch.R +++ b/tests/testthat/test_wdpa_fetch.R @@ -3,10 +3,10 @@ context("wdpa_fetch") test_that("country name", { skip_on_cran() skip_if_not(curl::has_internet()) - skip_if_phantomjs_not_available() + skip_if_chrome_not_available() skip_on_github_workflow("Windows") skip_on_github_workflow("macOS") - x <- suppressWarnings(wdpa_fetch("Liechtenstein", wait = TRUE)) + x <- suppressWarnings(wdpa_fetch("Liechtenstein", wait = TRUE, force = TRUE)) expect_is(x, "sf") expect_true(all(x$ISO3 == "LIE")) }) @@ -14,7 +14,7 @@ test_that("country name", { test_that("ISO3", { skip_on_cran() skip_if_not(curl::has_internet()) - skip_if_phantomjs_not_available() + skip_if_chrome_not_available() skip_on_github_workflow("Windows") skip_on_github_workflow("macOS") x <- suppressWarnings(wdpa_fetch("LIE", wait = TRUE, verbose = TRUE)) @@ -26,22 +26,44 @@ test_that("global", { skip_on_cran() skip_if_not(curl::has_internet()) skip_if_local_and_slow_internet() - skip_if_phantomjs_not_available() + skip_if_chrome_not_available() skip_on_github_workflow("Windows") skip_on_github_workflow("macOS") + expect_error(wdpa_fetch(n = 5, datatype = "shp")) x <- suppressWarnings(wdpa_fetch( "global", wait = TRUE, n = 5, verbose = TRUE)) expect_is(x, "sf") }) -test_that("polygon and point data", { +test_that("polygon and point data (gdb)", { skip_on_cran() skip_if_not(curl::has_internet()) - skip_if_phantomjs_not_available() + skip_if_chrome_not_available() skip_if_local_and_slow_internet() skip_on_github_workflow("Windows") skip_on_github_workflow("macOS") - x <- suppressWarnings(wdpa_fetch("USA", wait = TRUE)) + x <- suppressWarnings( + wdpa_fetch("DZA", wait = TRUE, datatype = "gdb", force = TRUE) + ) + expect_is(x, "sf") + expect_true(any(vapply( + sf::st_geometry(x), inherits, logical(1), c("POLYGON", "MULTIPOLYGON") + ))) + expect_true(any(vapply( + sf::st_geometry(x), inherits, logical(1), c("POINT", "MULTIPOINT") + ))) +}) + +test_that("polygon and point data (shp)", { + skip_on_cran() + skip_if_not(curl::has_internet()) + skip_if_chrome_not_available() + skip_if_local_and_slow_internet() + skip_on_github_workflow("Windows") + skip_on_github_workflow("macOS") + x <- suppressWarnings( + wdpa_fetch("COM", wait = TRUE, datatype = "gdb") + ) expect_is(x, "sf") expect_true(any(vapply( sf::st_geometry(x), inherits, logical(1), c("POLYGON", "MULTIPOLYGON") @@ -54,7 +76,7 @@ test_that("polygon and point data", { test_that("cache", { skip_on_cran() skip_if_not(curl::has_internet()) - skip_if_phantomjs_not_available() + skip_if_chrome_not_available() skip_on_github_workflow("Windows") skip_on_github_workflow("macOS") # fetch data diff --git a/tests/testthat/test_wdpa_read.R b/tests/testthat/test_wdpa_read.R index 5644fa7..6154b4a 100644 --- a/tests/testthat/test_wdpa_read.R +++ b/tests/testthat/test_wdpa_read.R @@ -1,13 +1,13 @@ context("wdpa_read") -test_that("without point data", { +test_that("without point data (gdb)", { skip_on_cran() skip_if_not(curl::has_internet()) - skip_if_phantomjs_not_available() + skip_if_chrome_not_available() skip_on_github_workflow("Windows") skip_on_github_workflow("macOS") # try to find download url - download_url <- wdpa_url("LIE", wait = TRUE) + download_url <- wdpa_url("LIE", wait = TRUE, datatype = "gdb") # path to save file zipfile with data path <- tempfile(pattern = "WDPA_", fileext = ".zip") # download zipfile @@ -21,14 +21,60 @@ test_that("without point data", { names(x))) }) -test_that("with point data", { +test_that("without point data (shp)", { skip_on_cran() skip_if_not(curl::has_internet()) - skip_if_phantomjs_not_available() + skip_if_chrome_not_available() skip_on_github_workflow("Windows") skip_on_github_workflow("macOS") # try to find download url - download_url <- wdpa_url("MHL", wait = TRUE) + download_url <- wdpa_url("LIE", wait = TRUE, datatype = "shp") + # path to save file zipfile with data + path <- tempfile(pattern = "WDPA_", fileext = ".zip") + # download zipfile + result <- utils::download.file(download_url, path) + # load data + x <- wdpa_read(path) + # tests + expect_is(x, "sf") + expect_true(nrow(x) > 0) + expect_true(all(c("ISO3", "STATUS", "DESIG_ENG", "REP_AREA", "MARINE") %in% + names(x))) +}) + +test_that("with point data (gdb)", { + skip_on_cran() + skip_if_not(curl::has_internet()) + skip_if_chrome_not_available() + skip_on_github_workflow("Windows") + skip_on_github_workflow("macOS") + # try to find download url + download_url <- wdpa_url("MHL", wait = TRUE, datatype = "gdb") + # path to save file zipfile with data + path <- tempfile(pattern = "WDPA_", fileext = ".zip") + # download zipfile + result <- utils::download.file(download_url, path) + # load data + x <- wdpa_read(path) + # tests + expect_is(x, "sf") + expect_true(nrow(x) > 0) + expect_true(all(c("ISO3", "STATUS", "DESIG_ENG", "REP_AREA", "MARINE") %in% + names(x))) + is_point <- vapply(sf::st_geometry(x), inherits, logical(1), "POINT") | + vapply(sf::st_geometry(x), inherits, logical(1), "MULTIPOINT") + expect_gt(sum(is_point), 0) + expect_gt(sum(!is_point), 0) +}) + +test_that("with point data (shp)", { + skip_on_cran() + skip_if_not(curl::has_internet()) + skip_if_chrome_not_available() + skip_on_github_workflow("Windows") + skip_on_github_workflow("macOS") + # try to find download url + download_url <- wdpa_url("MHL", wait = TRUE, datatype = "shp") # path to save file zipfile with data path <- tempfile(pattern = "WDPA_", fileext = ".zip") # download zipfile @@ -49,7 +95,7 @@ test_that("with point data", { test_that("global data", { skip_on_cran() skip_if_not(curl::has_internet()) - skip_if_phantomjs_not_available() + skip_if_chrome_not_available() skip_on_github_workflow("Windows") skip_on_github_workflow("macOS") skip_if_local_and_slow_internet() diff --git a/vignettes/wdpar.Rmd b/vignettes/wdpar.Rmd index 54361d8..9c389ae 100644 --- a/vignettes/wdpar.Rmd +++ b/vignettes/wdpar.Rmd @@ -47,7 +47,7 @@ library(ggmap) Now we will download protected area data for Malta from [Protected Planet](https://www.protectedplanet.net/en). We can achieve this by specifying Malta's country name (i.e. `"Malta"`) or Malta's [ISO3 code](https://en.wikipedia.org/wiki/ISO_3166-1_alpha-3) (i.e. `"MLT"`). Since data are downloaded to a temporary directory by default, we will specify that the data should be downloaded to a persistent directory. This means that R won't have to re-download the same dataset every time we restart our R session, and R can simply re-load previously downloaded datasets as needed. -```{r} +```{r, message = FALSE, results = "hide"} # download protected area data for Malta # (excluding areas represented as point localities) mlt_raw_pa_data <- wdpa_fetch( @@ -114,7 +114,7 @@ Now that we have finished cleaning the data, let's preview the data. For more in head(mlt_pa_data) ``` -We will now reproject the data to longitude/latitude coordinates ([EPSG:4326](https://spatialreference.org/ref/epsg/wgs-84/)) for visualization purposes. +We will now reproject the data to longitude/latitude coordinates ([EPSG:4326](https://spatialreference.org/ref/epsg/4326/)) for visualization purposes. ```{r} # reproject data @@ -125,9 +125,9 @@ Next, we can plot a map showing the boundaries of Malta's protected area system. ```{r, message = FALSE, warning = FALSE} # download basemap for making the map -bg <- get_stamenmap( +bg <- get_stadiamap( unname(st_bbox(mlt_pa_data)), zoom = 8, - maptype = "watercolor", force = TRUE + maptype = "stamen_terrain_background", force = TRUE ) # print map @@ -191,9 +191,9 @@ theme(axis.title = element_blank(), legend.position = "bottom") ### Recommended practices for large datasets -The _wdpar R_ package can be used to clean large datasets assuming that sufficient computational resources and time are available. Indeed, it can clean data spanning large countries, multiple countries, and even the full global datatset. When processing the full global dataset, it is recommended to use a computer system with at least 32 GB RAM available and to allow for at least one full day for the data cleaning procedures to complete. It is also recommended to avoid using the computer system for any other tasks while the data cleaning procedures are being completed, because they are very computationally intensive. Additionally, when processing large datasets -- and especially for the global dataset -- it is strongly recommended to disable the procedure for erasing overlapping areas. This is because the built-in procedure for erasing overlaps is very time consuming when processing many protected areas, so that information on each protected area can be output (e.g. IUCN category, year established). Instead, when cleaning large datasets, it is recommended to run the data cleaning procedures with the procedure for erasing overlapping areas disabled (i.e. with `erase_overlaps = FALSE`). After the data cleaning procedures have completed, the protected area data can be manually dissolved to remove overlapping areas (e.g. using `wdpa_dissolve()`). For an example of these procedures, please see below. +The _wdpar R_ package can be used to clean large datasets assuming that sufficient computational resources and time are available. Indeed, it can clean data spanning large countries, multiple countries, and even the full global dataset. When processing the full global dataset, it is recommended to use a computer system with at least 32 GB RAM available and to allow for at least one full day for the data cleaning procedures to complete. It is also recommended to avoid using the computer system for any other tasks while the data cleaning procedures are being completed, because they are very computationally intensive. Additionally, when processing large datasets -- and especially for the global dataset -- it is strongly recommended to disable the procedure for erasing overlapping areas. This is because the built-in procedure for erasing overlaps is very time consuming when processing many protected areas, so that information on each protected area can be output (e.g. IUCN category, year established). Instead, when cleaning large datasets, it is recommended to run the data cleaning procedures with the procedure for erasing overlapping areas disabled (i.e. with `erase_overlaps = FALSE`). After the data cleaning procedures have completed, the protected area data can be manually dissolved to remove overlapping areas (e.g. using `wdpa_dissolve()`). For an example of these procedures, please see below. -```{r, warning = FALSE} +```{r, warning = FALSE, message = FALSE, results = "hide"} # download protected area data for multiple of countries ## (i.e. Portugal, Spain, France) raw_pa_data <- @@ -201,7 +201,9 @@ raw_pa_data <- lapply(wdpa_fetch, wait = TRUE, download_dir = rappdirs::user_data_dir("wdpar")) %>% bind_rows() +``` +```{r} # clean protected area data (with procedure for erasing overlaps disabled) full_pa_data <- wdpa_clean(raw_pa_data, erase_overlaps = FALSE) @@ -288,7 +290,7 @@ Although the [World Database on Protected Areas (WDPA)](https://www.protectedpla * Arctic + [Arctic Protected Areas - 2017](http://geo.abds.is/geonetwork/srv/eng/catalog.search#/metadata/2e56ee1f-50a9-4983-88f4-edaa8588950d) * Australia - + [Collaborative Australian Protected Area Database (CAPAD)](http://www.environment.gov.au/fed/catalog/search/resource/details.page?uuid=%7B4448CACD-9DA8-43D1-A48F-48149FD5FCFD%7D) + + [Collaborative Australian Protected Area Database (CAPAD)](https://fed.dcceew.gov.au/datasets/erin::collaborative-australian-protected-areas-database-capad-2022-terrestrial) + [Conservation Areas Reporting and Tracking System (CARTS)](https://ccea-ccae.org) * The United States of America + [Protected Areas Database of the US (PAD-US)](https://doi.org/10.5066/P9Q9LQ4B)