Skip to content

Commit

Permalink
fix bug in habitat codes for resident distributions (PR #56)
Browse files Browse the repository at this point in the history
- Fix bug in `create_spp_info_data()` in assigning habitat types for resident
  distributions of migratory and non-migratory species. This bug meant that (i)
  habitat types for resident distributions of migratory species would include
  those exclusively affiliated with the species' passage distributions and (ii)
  habitat types for resident distributions of non-migratory species would not
  include those exclusively affiliated with the species' passage distributions.
  Thanks to Jianqiao Zhao for bug report.
- Update built-in testing data to include multiple migratory species.
- Update internal R script for creating test dataset
  (i.e., `inst/scripts/test-data.R`) to be compatible with current version of
  the package.
- Update `is_gdal_calc_available()` to be more robust.
- Fix URLs.
  • Loading branch information
jeffreyhanson authored Jul 24, 2024
1 parent 67b414c commit d6e1d75
Show file tree
Hide file tree
Showing 88 changed files with 782 additions and 617 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: aoh
Type: Package
Version: 0.0.2.11
Version: 0.0.2.12
Title: Create Area of Habitat Data
Description: Create Area of Habitat data to characterize species distributions.
Data are produced following procedures outlined by Brooks et al. (2019)
Expand Down
16 changes: 16 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
# aoh 0.0.2.12

- Fix bug in `create_spp_info_data()` in assigning habitat types for resident
distributions of migratory and non-migratory species. This bug meant that (i)
habitat types for resident distributions of migratory species would include
those exclusively affiliated with the species' passage distributions and (ii)
habitat types for resident distributions of non-migratory species would not
include those exclusively affiliated with the species' passage distributions.
Thanks to Jianqiao Zhao for bug report.
- Update built-in testing data to include multiple migratory species.
- Update internal R script for creating test dataset
(i.e., `inst/scripts/test-data.R`) to be compatible with current version of
the package.
- Update `is_gdal_calc_available()` to be more robust.
- Fix URLs.

# aoh 0.0.2.11

- Fix bug in `create_spp_info_data()` so that the IUCN Red List API key can be
Expand Down
35 changes: 32 additions & 3 deletions R/collate_spp_info_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -199,34 +199,63 @@ collate_spp_info_data <- function(x,
spp_habitat_data <- spp_habitat_data[idx, , drop = FALSE]
## if exact matches for habitat codes should not be used then...
if (isTRUE(adjust_habitat_codes)) {
### create new data by merging habitat codes following KBA guidelines
### create new data by merging habitat codes based on KBA guidelines
### and personal communications with Busana M. and Butchart S.H.M. at
### BirdLife International
spp_habitat_data <- dplyr::bind_rows(
list(
### resident (non-migratory species)
#### NOTE: we include habitat types listed for all seasonal
#### distributions. although non-migratory
#### species should not have any habitat types affiliated with
#### non-residential distributions, this can happen due to
#### errors/discrepancies in the IUCN Red List data
prepare_habitat_codes(
dplyr::filter(spp_habitat_data, .data$seasonal %in% migratory_ids),
dplyr::filter(spp_habitat_data, !.data$id_no %in% migratory_ids),
focal_seasonal = 1L,
extra_seasonal = c(2L, 3L, 4L, 5L, NA_integer_)
),
### resident (migratory species)
#### NOTE: we do not include passage (code 4) habitat types here
#### for migratory bird species because these might be substantially
#### different from resident distributions
prepare_habitat_codes(
dplyr::filter(spp_habitat_data, !.data$seasonal %in% migratory_ids),
dplyr::filter(spp_habitat_data, .data$id_no %in% migratory_ids),
focal_seasonal = 1L,
extra_seasonal = c(2L, 3L, 5L, NA_integer_)
),
### breeding
#### NOTE: we include habitat types listed for resident (code 1),
#### breeding (code 2) and seasonality uncertain (code 5) when
#### mapping the breeding distributions, per KBA guidelines
prepare_habitat_codes(
spp_habitat_data,
focal_seasonal = 2L,
extra_seasonal = c(1L, 5L, NA_integer_)
),
### non-breeding
#### NOTE: we include habitat types listed for resident (code 1),
#### non-breeding (code 2) and seasonality uncertain (code 5) when
#### mapping the non-breeding distributions, per KBA guidelines
prepare_habitat_codes(
spp_habitat_data,
focal_seasonal = 3L,
extra_seasonal = c(1L, 5L, NA_integer_)
),
### passage
#### NOTE: we include habitat types listed for resident (code 1),
#### passage (code 24 and seasonality uncertain (code 5) when
#### mapping the passage distributions, following a similar
#### rationale for the breeding and non-breeding distributions.
#### although the resident distribution for migratory species
#### does not consider habitat types listed for passage
#### distributions (based on the rationale that species might
#### have habitat types unique to their passage distribution),
#### we include habitat types for resident distributions here
#### since (i) many species are missing habitat types for their
#### passage distributions and (ii) if the habitat types are
#### used during the resident distribution, then they are
#### likely used during passage (but not vice versa)
prepare_habitat_codes(
spp_habitat_data,
focal_seasonal = 4L,
Expand Down
3 changes: 3 additions & 0 deletions R/internal.R
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ normalize_path <- function(path, winslash = "\\", mustWork = NA) {
if (identical(.Platform$OS.type, "unix")) {
# if on a Unix platform, then we just need to use normalizePath()
out <- normalizePath(path, winslash = winslash, mustWork = mustWork)
# if on a macOS system, this sometimes results in file paths
# that contain "//" instead of "/" for a folder separator
out <- gsub("//", "/", out, fixed = TRUE)
} else {
# if on a Windows platform, then things get a bit more complicated...
if (!grepl("~", path, fixed = TRUE)) {
Expand Down
16 changes: 13 additions & 3 deletions R/is_available.R
Original file line number Diff line number Diff line change
Expand Up @@ -138,11 +138,21 @@ is_gdal_calc_available <- function() {
return(FALSE)
}
}
v <- try(
system(python_gdal_calc("--help"), intern = TRUE),
silent = TRUE
v <- suppressWarnings(
try(
system(python_gdal_calc("--help"), intern = TRUE),
silent = TRUE
)
)
# return FALSE if system command executed with error
if (inherits(v, "try-error")) return(FALSE)
# return FALSE if system command executed with non-zero status
if (!is.null(attr(v, "status"))) {
if (!identical(attr(v, "status"), 0L)) {
return(FALSE)
}
}
# otherwise, return TRUE
TRUE
}

Expand Down
2 changes: 1 addition & 1 deletion R/read_spp_range_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ NULL
#' (see https://www.iucnredlist.org/resources/spatial-data-download).
#' Data for birds can be obtained by requesting data from
#' [BirdLife International](https://www.birdlife.org/)
#' (see <http://datazone.birdlife.org/species/requestdis>).
#' (see <https://datazone.birdlife.org/species/requestdis>).
#' To standardize data from the IUCN Red List and BirdLife International,
#' the `"SISID"` and `"SISRecID"` columns are renamed as `"id_no"`.
#'
Expand Down
11 changes: 11 additions & 0 deletions R/terra_gdal_calc.R
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,8 @@ terra_gdal_calc <- function(x, expr,
if (isTRUE(verbose)) {
cli::cli_alert_info(paste("System command:", cmd))
}

# run gdal_calc via system command
system(cmd, intern = !verbose)

# clean up
Expand All @@ -246,6 +248,15 @@ terra_gdal_calc <- function(x, expr,
unlink(f3, force = TRUE)
}

# verify output file exists
assertthat::assert_that(
file.exists(filename),
msg = paste0(
"failed to run `gdal_calc.py` script: output file \"",
filename, "\" does not exist"
)
)

# return result
if (output_raster) {
return(stats::setNames(terra::rast(filename), nms))
Expand Down
2 changes: 1 addition & 1 deletion README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ print(spp_aoh_rasters)

Finally, let's create some maps to compare the range data with the Area of habitat data.

```{r "map", message = FALSE, warning = FALSE, dpi = 200, fig.width = 5.5, fig.height = 4, out.width = ifelse(isTRUE(knitr::is_html_output(excludes = c("markdown"))), "60%", "90%")}
```{r "map", message = FALSE, warning = FALSE, results = "hide", dpi = 200, fig.width = 5.5, fig.height = 4, out.width = ifelse(isTRUE(knitr::is_html_output(excludes = c("markdown"))), "60%", "90%")}
# create maps
## N.B. you might need to install the ggmap package
map <-
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -366,15 +366,15 @@ produce Area of Habitat data.
relevant data using:

Hanson JO (2024) aoh: Create Area of Habitat Data. R package version
0.0.2.11. Available at https://github.com/prioritizr/aoh.
0.0.2.12. Available at https://github.com/prioritizr/aoh.

IUCN [insert year] IUCN Red List of Threatened Species. Version
[insert version]. Available at www.iucnredlist.org.

BirdLife International and Handbook of the Birds of the World
([insert year]) Bird species distribution maps of the world. Version
[insert version]. Available at
http://datazone.birdlife.org/species/requestdis.
https://datazone.birdlife.org/species/requestdis.

Jung M, Dahal PR, Butchart SHM, Donald PF, De Lamo X, Lesiv M, Kapos
V, Rondinini C, Visconti P (2020). "A global map of terrestrial
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.

22 changes: 11 additions & 11 deletions docs/articles/aoh.html

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

Binary file modified docs/articles/aoh_files/figure-html/unnamed-chunk-10-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit d6e1d75

Please sign in to comment.