Skip to content

Commit

Permalink
fix #68, fix #69 (#70)
Browse files Browse the repository at this point in the history
* fix #68, fix #69
* slightly improve tests
  • Loading branch information
jeffreyhanson authored Jul 16, 2023
1 parent d4c52e4 commit 506c420
Show file tree
Hide file tree
Showing 33 changed files with 265 additions and 219 deletions.
5 changes: 2 additions & 3 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: wdpar
Type: Package
Version: 1.3.4
Version: 1.3.5
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
Expand All @@ -13,7 +13,6 @@ Authors@R: c(person(c('Jeffrey', 'O'), 'Hanson',
email='jeffrey.hanson@uqconnect.edu.au', role = c('aut', 'cre')))
Imports:
utils,
sp,
assertthat (>= 0.2.0),
progress (>= 1.2.0),
curl (>= 3.2),
Expand All @@ -39,7 +38,7 @@ Suggests:
ps (>= 1.5.0)
Depends:
R (>= 3.5.0),
sf (>= 1.0-2)
sf (>= 1.0-13)
License: GPL-3
Encoding: UTF-8
Language: en-US
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ export(wdpa_latest_version)
export(wdpa_read)
export(wdpa_url)
import(sf)
importFrom(rappdirs,user_data_dir)
7 changes: 7 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
# wdpar 1.3.5

- CRAN release.
- Fix compatibility issues with Protected Planet (#69).
- Upate `wdpa_clean()` to be more robust (#68).
- Remove _sp_ package as dependency.

# wdpar 1.3.4

- CRAN release.
Expand Down
6 changes: 0 additions & 6 deletions R/internal.R
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,6 @@ extract_polygons_and_points <- function(x) {
rbind(o, x[ind, , drop = FALSE])
}

# define dummy function to avoid CRAN notes for dependencies
import_deps <- function() {
x <- rappdirs::user_data_dir
x <- sp::spTransform
}

# Empty WDPA data set
#'
#' This function creates an empty WDPA dataset.
Expand Down
4 changes: 4 additions & 0 deletions R/package.R
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,7 @@ NULL

#' @import sf
NULL

# avoid false positive NOTEs during CRAN checks
#' @importFrom rappdirs user_data_dir
NULL
62 changes: 41 additions & 21 deletions R/st_erase_overlaps.R
Original file line number Diff line number Diff line change
Expand Up @@ -60,37 +60,54 @@ st_erase_overlaps <- function(x, verbose = FALSE) {
if (length(ovr) > 0) {
## create union
### run union
u <- sf::st_union(sf::st_set_precision(
suppressWarnings(sf::st_collection_extract(sf::st_buffer(o[ovr], 0))),
precision))
u <- sf::st_union(
sf::st_set_precision(
suppressWarnings(sf::st_collection_extract(sf::st_buffer(o[ovr], 0))),
precision
)
)
### buffer the union to fix any geometry issues
u <- sf::st_buffer(u, dist = 0)
### repair the geometry if there are any issues
if (!all(sf::st_is_valid(u))) {
#nocov start
u <- suppressWarnings(sf::st_collection_extract(
sf::st_make_valid(sf::st_set_precision(u, precision)),
"POLYGON"))
u <- suppressWarnings(
sf::st_collection_extract(
sf::st_make_valid(sf::st_set_precision(u, precision)),
"POLYGON"
)
)
#nocov end
}
## calculate difference
### run difference
d <- sf::st_difference(
suppressWarnings(sf::st_collection_extract(
sf::st_make_valid(sf::st_set_precision(g[i], precision)),
"POLYGON")),
suppressWarnings(sf::st_collection_extract(
sf::st_make_valid(sf::st_set_precision(u, precision)),
"POLYGON")))
if (length(d) == 0L)
d[[1]] <- sf::st_polygon() #nocov
suppressWarnings(
sf::st_collection_extract(
sf::st_make_valid(sf::st_set_precision(g[i], precision)),
"POLYGON"
)
),
suppressWarnings(
sf::st_collection_extract(
sf::st_make_valid(sf::st_set_precision(u, precision)),
"POLYGON"
)
)
)
if (length(d) == 0L) {
d <- sf::st_sfc(sf::st_geometrycollection(), crs = sf::st_crs(d)) #nocov
}
d <- suppressWarnings(sf::st_collection_extract(d, "POLYGON"))
### repair the geometry if there are any issues
if (!all(sf::st_is_valid(d))) {
#nocov start
d <- suppressWarnings(sf::st_collection_extract(
sf::st_make_valid(sf::st_set_precision(d, precision)),
"POLYGON"))
d <- suppressWarnings(
sf::st_collection_extract(
sf::st_make_valid(sf::st_set_precision(d, precision)),
"POLYGON"
)
)
#nocov end
}
} else {
Expand All @@ -106,16 +123,19 @@ st_erase_overlaps <- function(x, verbose = FALSE) {
d <- d[as.numeric(sf::st_area(d)) > 1]
d <- sf::st_cast(d, "MULTIPOLYGON")
if (length(d) == 0) {
d[[1]] <- sf::st_polygon() #nocov
d <- sf::st_sfc(sf::st_geometrycollection(), crs = sf::st_crs(d)) #nocov
}
d <- suppressWarnings(sf::st_collection_extract(d, "POLYGON"))
}
## if d contains multiple geometries, then union them
if (length(d) > 1) {
d <- sf::st_union(d)
d <- suppressWarnings(sf::st_collection_extract(
sf::st_make_valid(sf::st_set_precision(d, precision)),
"POLYGON"))
d <- suppressWarnings(
sf::st_collection_extract(
sf::st_make_valid(sf::st_set_precision(d, precision)),
"POLYGON"
)
)
}
## create empty geometry if empty
if (length(d) == 0) {
Expand Down
2 changes: 1 addition & 1 deletion R/wdpa_url.R
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ wdpa_url <- function(x, wait = FALSE, page_wait = 2) {
pjs <- start_phantomjs()
rd <- webdriver::Session$new(port = pjs$port)
## navigate to download web page
rd$go(paste0("https://protectedplanet.net/country/", x))
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()
Expand Down
32 changes: 16 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -181,24 +181,24 @@ head(mlt_pa_data)
## Geometry type: MULTIPOLYGON
## Dimension: XY
## Bounding box: xmin: 1382584 ymin: 4280853 xmax: 1399759 ymax: 4299615
## CRS: +proj=cea +lon_0=0 +lat_ts=30 +x_0=0 +y_0=0 +datum=WGS84 +ellps=WGS84 +units=m +no_defs
## 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
## WDPAID WDPA_PID PA_DEF NAME ORIG_…¹ DESIG DESIG…² DESIG…³ IUCN_…⁴ INT_C…⁵
## <dbl> <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr>
## 1 194425 194425 PA 'Il-… 'Il-… Rise… Nature… Nation… Ia Not Ap…
## 2 194420 194420 PA Filf… Filfla Rise… Nature… Nation… Ia Not Ap…
## 3 555588631 555588631 PA Il-M… Il-Maj… Park… Nation… Nation… II Not Ap…
## 4 174757 174757 PA Il-Ġ… Il-Ġon… List… List o… Nation… III Not Ap…
## 5 174758 174758 PA Bidn… Bidnij… List… List o… Nation… III Not Ap…
## 6 194415 194415 PA 'Il-… 'Il-Ġo… List… List o… Nation… III Not Ap…
## # … with 23 more variables: 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>, 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_TYPE <chr>, AREA_KM2 <dbl>,
## # geometry <MULTIPOLYGON [m]>, and abbreviated variable names ¹​ORIG_NAME, …
## 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 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
## # ℹ 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>,
## # 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_TYPE <chr>, AREA_KM2 <dbl>,
## # geometry <MULTIPOLYGON [m]>

Finally, after cleaning the data, let’s plot a map showing Malta’s
protected areas and color each area according to its management category
Expand Down
2 changes: 1 addition & 1 deletion docs/404.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/CONTRIBUTING.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/LICENSE-text.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/articles/index.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 506c420

Please sign in to comment.