diff --git a/CODE_DESIGN.md b/CODE_DESIGN.md
index af7b744c..7f4501c3 100644
--- a/CODE_DESIGN.md
+++ b/CODE_DESIGN.md
@@ -4,7 +4,7 @@
- API urls
- are stored in `meta_info`, a tibble created in `data-raw/data_creation.R`
- This is where you can change from main to sandbox versions
- - https://birdscanada.org/api vs. https://sandbox.birdscanada.org/api
+ - https://naturecounts.ca/api vs. https://sandbox.naturecounts.ca/api
- To apply this you must **re-run** `data-raw/data_creation.R` and then
**re-load** the functions/package
diff --git a/DESCRIPTION b/DESCRIPTION
index 8756e987..9068b5e5 100644
--- a/DESCRIPTION
+++ b/DESCRIPTION
@@ -45,12 +45,14 @@ Suggests:
sf (>= 1.0-9),
spelling,
testthat,
- vdiffr
+ vdiffr,
+ rnaturalearthhires
Language: en-US
Roxygen: list(markdown = TRUE)
-RoxygenNote: 7.3.1
+RoxygenNote: 7.3.2
URL: https://github.com/BirdsCanada/naturecounts,
https://naturecounts.ca,
https://birdscanada.github.io/naturecounts/
VignetteBuilder: knitr
Config/testthat/edition: 3
+Remotes: ropensci/rnaturalearthhires
diff --git a/NEWS.md b/NEWS.md
index 497a003c..8154e788 100644
--- a/NEWS.md
+++ b/NEWS.md
@@ -1,3 +1,8 @@
+# naturecounts dev
+* Add option to clip EOO in `cosewic_ranges()` to a particular shapefile before calculating area
+* Add option to scale records in `cosewic_plot()`
+* Change API to naturecounts.ca
+
# naturecounts 0.4.1
* Fix use of species_id and record_id in `cosewic_ranges()`.
* Truly allow different columns
diff --git a/R/cosewic_tools.R b/R/cosewic_tools.R
index 64bf6d23..0f521447 100644
--- a/R/cosewic_tools.R
+++ b/R/cosewic_tools.R
@@ -44,6 +44,9 @@
#' Defaults to 0.95 for a 95% convex hull to ensure outlier points do not
#' artificially inflate the EOO. Note that for a final COSEWIC report, this
#' may not be appropriate. Set to 1 to include all points.
+#' @param eoo_clip sf (Multi)Polygon. A spatial object to clip the EOO to. May
+#' be relevant when calculating EOOs for complex regions (i.e. long curved
+#' areas) to avoid including area which cannot have observations.
#' @param filter_unique Logical. Whether to filter observations to unique
#' locations. Use this only if there are too many data points to work with.
#' This changes the nature of what an observation is, and also may bias
@@ -86,6 +89,18 @@
#' r <- cosewic_ranges(mult)
#' r <- cosewic_ranges(mult, spatial = FALSE)
#'
+#' # Clip to a specific region
+#' @examplesIf requireNamespace("rnaturalearth", quietly = TRUE) & requireNamespace("rnaturalearthhires", quietly = TRUE)
+#'
+#' library(rnaturalearth)
+#' ON <- ne_states("Canada") |>
+#' dplyr::filter(postal == "ON")
+#'
+#' r <- cosewic_ranges(mult)
+#' cosewic_plot(r, map = ON) # No clip
+#'
+#' r <- cosewic_ranges(mult, eoo_clip = ON)
+#' cosewic_plot(r, map = ON) # With clip
#'
#' @export
@@ -96,6 +111,7 @@ cosewic_ranges <- function(df_db,
species = "species_id",
iao_grid_size_km = 2,
eoo_p = 0.95,
+ eoo_clip = NULL,
filter_unique = FALSE,
spatial = TRUE) {
@@ -109,6 +125,12 @@ cosewic_ranges <- function(df_db,
} else if (!all(is.numeric(df[[coord_lat]]), is.numeric(df[[coord_lat]]))) {
stop("`coord_lat` and `coord_lon` must be numeric", call. = FALSE)
}
+
+ # Clip
+ if(!is.null(eoo_clip) && !inherits(eoo_clip, "sf") &&
+ !all(sf::st_is(eoo_clip, c("POLYGON", "MULTIPOLYGON")))) {
+ stop("If provided, `eoo_clip` must be an sf polygon object", call. = FALSE)
+ }
# Columns
if(!is.null(species) && !species %in% names(df)) {
@@ -175,7 +197,7 @@ cosewic_ranges <- function(df_db,
dplyr::left_join(n, by = species) %>%
dplyr::relocate(dplyr::all_of(species), "n_records_total") %>%
dplyr::mutate(
- eoo = purrr::map(.data[["data"]], \(x) cosewic_eoo(x, p = eoo_p, spatial)),
+ eoo = purrr::map(.data[["data"]], \(x) cosewic_eoo(x, p = eoo_p, clip = eoo_clip, spatial)),
iao = purrr::map(.data[["data"]], \(x) cosewic_iao(x, cell_size, record, spatial))) %>%
dplyr::select(-"data")
@@ -269,20 +291,33 @@ cosewic_iao <- function(df_sf, cell_size, record, spatial) {
iao
}
-cosewic_eoo <- function(df_sf, p, spatial) {
+cosewic_eoo <- function(df_sf, p, clip, spatial) {
center <- df_sf %>%
sf::st_union() %>%
sf::st_convex_hull() %>%
sf::st_centroid()
-
+
eoo <- df_sf %>%
dplyr::mutate(dist = sf::st_distance(.data$geometry, .env$center)[, 1]) %>%
dplyr::filter(.data$dist <= stats::quantile(.data$dist, .env$p)) %>%
sf::st_cast(to = "POINT") %>%
sf::st_union() %>%
sf::st_convex_hull() %>%
- sf::st_as_sf() %>%
- dplyr::mutate(eoo = sf::st_area(.),
+ sf::st_as_sf()
+
+ if(!is.null(clip)) {
+ clip <- sf::st_transform(clip, sf::st_crs(eoo))
+ eoo_clipped <- sf::st_intersection(sf::st_set_agr(eoo, "constant"),
+ sf::st_set_agr(clip, "constant"))
+ if(nrow(eoo_clipped) == 0) {
+ warning("Clipping EOO results in no EOO, using non-clipped EOO instead", call. = FALSE)
+ } else {
+ eoo <- eoo_clipped
+ }
+ }
+
+ eoo <- eoo |>
+ dplyr::mutate(eoo = sf::st_area(eoo),
eoo = units::set_units(.data$eoo, "km^2"))
if(!spatial) eoo <- sf::st_drop_geometry(eoo)
@@ -414,6 +449,9 @@ map_canada <- function() {
#' @param grid sf data frame. Optional grid over which to summarize IAO values
#' (useful for species with many points over a broad distribution).
#' @param map sf data frame. Optional base map over which to plot the values.
+#' @param scale Logical. Whether to scale the IAO legends to a proportion for
+#' easier plotting of mutliple species (allows collecting legends by
+#' patchwork).
#' @param species Character. Name of the column containing species
#' identification.
#' @param title Character. Optional title to add to the map. Can be a named by
@@ -425,6 +463,7 @@ map_canada <- function() {
#' @examples
#' r <- cosewic_ranges(bcch)
#' cosewic_plot(r)
+#' cosewic_plot(r, scale = TRUE)
#' cosewic_plot(r, points = bcch)
#' cosewic_plot(r, grid = grid_canada(50), map = map_canada(),
#' title = "Black-capped chickadees")
@@ -432,6 +471,7 @@ map_canada <- function() {
#' m <- rbind(bcch, hofi)
#' r <- cosewic_ranges(m)
#' cosewic_plot(r)
+#' cosewic_plot(r, scale = TRUE)
#' cosewic_plot(r, points = m)
#' p <- cosewic_plot(r, grid = grid_canada(50), map = map_canada(),
#' title = c("14280" = "Black-capped chickadees",
@@ -440,6 +480,7 @@ map_canada <- function() {
#' p[[2]]
#'
cosewic_plot <- function(ranges, points = NULL, grid = NULL, map = NULL,
+ scale = FALSE,
species = "species_id", title = "") {
have_pkg_check("sf")
@@ -490,14 +531,14 @@ cosewic_plot <- function(ranges, points = NULL, grid = NULL, map = NULL,
g <- purrr::pmap(
list(e, i, points, title),
- \(e, i, points, title) cosewic_plot_indiv(e, i, points, grid, map, title))
+ \(e, i, points, title) cosewic_plot_indiv(e, i, points, grid, map, scale, title))
if(length(g) == 1) g <- g[[1]]
g
}
-cosewic_plot_indiv <- function(e, a, points, grid, map, title) {
+cosewic_plot_indiv <- function(e, a, points, grid, map, scale, title) {
size_a <- unique(a$grid_size_km)
@@ -506,6 +547,10 @@ cosewic_plot_indiv <- function(e, a, points, grid, map, title) {
stringr::str_replace("p(\\d{1,3})", "\\1%") %>%
toupper()
+ records <- paste0(a$n_records_total[1],
+ " records\n(", a$min_record[1], "-", a$max_record[1],
+ " per ", size_a, "x", size_a, " km grid)")
+
if(!is.null(grid)) {
a <- a %>%
sf::st_join(grid, ., left = FALSE) %>% # Inner join
@@ -515,6 +560,11 @@ cosewic_plot_indiv <- function(e, a, points, grid, map, title) {
} else {
size_p <- size_a
}
+
+ if(scale) {
+ a <- dplyr::mutate(a, n_records = .data$n_records / max(.data$n_records, na.rm = TRUE))
+ leg_title <- "IAO\nProp. records"
+ } else leg_title <- "IAO\nNo. records"
g <- ggplot2::ggplot() +
ggplot2::theme_minimal() +
@@ -523,8 +573,9 @@ cosewic_plot_indiv <- function(e, a, points, grid, map, title) {
ggplot2::scale_fill_viridis_c() +
ggplot2::scale_colour_manual(name = "", values = "grey20") +
ggplot2::labs(
- fill = "IAO\nNo. records",
+ fill = leg_title,
title = title,
+ subtitle = records,
caption =
paste0("Showing ", size_p, "x", size_p,
"km grids\nAnalysis used ",
diff --git a/R/metadata_utils.R b/R/metadata_utils.R
index c0cab217..153db4ea 100644
--- a/R/metadata_utils.R
+++ b/R/metadata_utils.R
@@ -30,12 +30,12 @@ metadata_v_remote <- function() {
metadata_save <- function(data, path, name = deparse(substitute(data)),
compress = TRUE) {
- save(data, file = file.path(path, paste0(name, ".rds")), compress = compress)
+ save(data, file = file.path(path, paste0("meta_", name, ".rds")), compress = compress)
}
metadata_read <- function(name) {
data <- NULL # load(f) reads data into envir as 'data', use this to avoid NOTE
- f <- system.file("extdata", paste0(name, ".rds"), package = "naturecounts")
+ f <- system.file("extdata", paste0("meta_", name, ".rds"), package = "naturecounts")
if(!file.exists(f)) stop("Could not find metadata file '", name, "'",
call. = FALSE)
load(f)
@@ -156,8 +156,8 @@ nc_metadata_internal <- function(path = "./inst/extdata", force = TRUE,
# Update metadata version
message("Metadata version updated to ", metadata_v_remote())
- metadata_save(metadata_v_remote(), name = "metadata_v_local", path = path)
+ metadata_save(metadata_v_remote(), name = "v_local", path = path)
}
}
-metadata_v_local <- function() {metadata_read("metadata_v_local")}
+metadata_v_local <- function() {metadata_read("v_local")}
diff --git a/R/sysdata.rda b/R/sysdata.rda
index 6bd14f3f..3caf96a0 100644
Binary files a/R/sysdata.rda and b/R/sysdata.rda differ
diff --git a/RELEASE.R b/RELEASE.R
index 04cf6a8b..d5f8ce51 100644
--- a/RELEASE.R
+++ b/RELEASE.R
@@ -7,7 +7,9 @@
devtools::test()
# - Update internal data files
-source("data-raw/data_creation.R")
+source("data-raw/data_internal.R")
+source("data-raw/data_example.R")
+source("data-raw/data_test.R")
# - Update metadata stored in inst/extdata (Check URLS in data-raw/data_creation.R)
# - Utm codes take time to update
diff --git a/data-raw/data_example.R b/data-raw/data_example.R
new file mode 100644
index 00000000..b8036e4b
--- /dev/null
+++ b/data-raw/data_example.R
@@ -0,0 +1,16 @@
+# Get Example Data ------------------------------------------------------------
+
+# Create example databases
+bcch <- nc_data_dl(request_id = 152543, username = "sample")
+usethis::use_data(bcch, internal = FALSE, overwrite = TRUE)
+
+hofi <- nc_data_dl(species = 20350, username = "sample", info = "pkg_data")
+usethis::use_data(hofi, internal = FALSE, overwrite = TRUE)
+
+unlink(file.path("inst", "extdata", "bcch.nc"))
+nc_data_dl(request_id = 152543, username = "sample",
+ sql_db = file.path("inst", "extdata", "bcch"))
+
+unlink(file.path("inst", "extdata", "hofi.nc"))
+nc_data_dl(species = 20350, username = "sample", info = "pkg_data",
+ sql_db = file.path("inst", "extdata", "hofi"))
diff --git a/data-raw/data_creation.R b/data-raw/data_internal.R
similarity index 76%
rename from data-raw/data_creation.R
rename to data-raw/data_internal.R
index 060e82be..c94c634d 100644
--- a/data-raw/data_creation.R
+++ b/data-raw/data_internal.R
@@ -4,7 +4,7 @@ ua <- httr::user_agent(agent = "https://github.com/birdscanada/naturecounts")
# API URLs
meta_info <- dplyr::tribble(
~package_name, ~api_url, ~primary_keys,
- "api", "https://birdscanada.org/api", NA,
+ "api", "https://naturecounts.ca/api", NA,
"auth", "/data/authenticate", NA,
@@ -75,15 +75,6 @@ queries <- dplyr::tribble(
"subnational2", "subNat2", FALSE,
"site_type", "siteType", TRUE)
-# Testing Data ------------------------------------------------------------
-test_rc <- nc_data_dl(request_id = 152518, fields_set = "core",
- username = "sample", info = "sample_data") |>
- dplyr::filter(CommonName %in% c("Monarch",
- "Black Swallowtail",
- "Red Admiral"),
- AllSpeciesReported == "Yes") |>
- dplyr::mutate(presence = as.numeric(ObservationCount > 0)) |>
- format_dates()
# Field order - Non BMDE fields
field_order <- c("record_id", "collection", "project_id", "protocol_id",
@@ -96,22 +87,6 @@ field_order <- c("record_id", "collection", "project_id", "protocol_id",
"source_table", "breeding_rank", "is_unconfirmed")
# Save all internal datasets
-usethis::use_data(ua, api, keys, queries, test_rc, field_order,
+usethis::use_data(ua, api, keys, queries, field_order,
internal = TRUE, overwrite = TRUE)
-# Get Example Data ------------------------------------------------------------
-
-# Create example databases
-bcch <- nc_data_dl(request_id = 152543, username = "sample")
-usethis::use_data(bcch, internal = FALSE, overwrite = TRUE)
-
-hofi <- nc_data_dl(species = 20350, username = "sample", info = "pkg_data")
-usethis::use_data(hofi, internal = FALSE, overwrite = TRUE)
-
-unlink(file.path("inst", "extdata", "bcch.nc"))
-nc_data_dl(request_id = 152543, username = "sample",
- sql_db = file.path("inst", "extdata", "bcch"))
-
-unlink(file.path("inst", "extdata", "hofi.nc"))
-nc_data_dl(species = 20350, username = "sample", info = "pkg_data",
- sql_db = file.path("inst", "extdata", "hofi"))
diff --git a/data-raw/data_test.R b/data-raw/data_test.R
new file mode 100644
index 00000000..cd091c0e
--- /dev/null
+++ b/data-raw/data_test.R
@@ -0,0 +1,11 @@
+# Testing Data ------------------------------------------------------------
+test_rc <- nc_data_dl(request_id = 152518, fields_set = "core",
+ username = "sample", info = "sample_data") |>
+ dplyr::filter(CommonName %in% c("Monarch",
+ "Black Swallowtail",
+ "Red Admiral"),
+ AllSpeciesReported == "Yes") |>
+ dplyr::mutate(presence = as.numeric(ObservationCount > 0)) |>
+ format_dates()
+
+saveRDS(test_rc, file.path(system.file("extdata", package = "naturecounts"), "test_data.rds"))
diff --git a/inst/extdata/bcr_codes.rds b/inst/extdata/bcr_codes.rds
deleted file mode 100644
index 2e3db34c..00000000
Binary files a/inst/extdata/bcr_codes.rds and /dev/null differ
diff --git a/inst/extdata/bmde_fields.rds b/inst/extdata/bmde_fields.rds
deleted file mode 100644
index 1f045367..00000000
Binary files a/inst/extdata/bmde_fields.rds and /dev/null differ
diff --git a/inst/extdata/country_codes.rds b/inst/extdata/country_codes.rds
deleted file mode 100644
index 54b3b81f..00000000
Binary files a/inst/extdata/country_codes.rds and /dev/null differ
diff --git a/inst/extdata/iba_codes.rds b/inst/extdata/iba_codes.rds
deleted file mode 100644
index cfc0cbc6..00000000
Binary files a/inst/extdata/iba_codes.rds and /dev/null differ
diff --git a/inst/extdata/meta_bcr_codes.rds b/inst/extdata/meta_bcr_codes.rds
new file mode 100644
index 00000000..44160bf2
Binary files /dev/null and b/inst/extdata/meta_bcr_codes.rds differ
diff --git a/inst/extdata/meta_bmde_fields.rds b/inst/extdata/meta_bmde_fields.rds
new file mode 100644
index 00000000..d700fcb3
Binary files /dev/null and b/inst/extdata/meta_bmde_fields.rds differ
diff --git a/inst/extdata/meta_country_codes.rds b/inst/extdata/meta_country_codes.rds
new file mode 100644
index 00000000..0f7f9df5
Binary files /dev/null and b/inst/extdata/meta_country_codes.rds differ
diff --git a/inst/extdata/meta_iba_codes.rds b/inst/extdata/meta_iba_codes.rds
new file mode 100644
index 00000000..be2b96e1
Binary files /dev/null and b/inst/extdata/meta_iba_codes.rds differ
diff --git a/inst/extdata/species_authority.rds b/inst/extdata/meta_species_authority.rds
similarity index 62%
rename from inst/extdata/species_authority.rds
rename to inst/extdata/meta_species_authority.rds
index a6444d65..7007a7bb 100644
Binary files a/inst/extdata/species_authority.rds and b/inst/extdata/meta_species_authority.rds differ
diff --git a/inst/extdata/meta_species_codes.rds b/inst/extdata/meta_species_codes.rds
new file mode 100644
index 00000000..d57b2b3c
Binary files /dev/null and b/inst/extdata/meta_species_codes.rds differ
diff --git a/inst/extdata/meta_species_taxonomy.rds b/inst/extdata/meta_species_taxonomy.rds
new file mode 100644
index 00000000..9fda9d0c
Binary files /dev/null and b/inst/extdata/meta_species_taxonomy.rds differ
diff --git a/inst/extdata/meta_statprov_codes.rds b/inst/extdata/meta_statprov_codes.rds
new file mode 100644
index 00000000..3f33e7ad
Binary files /dev/null and b/inst/extdata/meta_statprov_codes.rds differ
diff --git a/inst/extdata/meta_subnational2_codes.rds b/inst/extdata/meta_subnational2_codes.rds
new file mode 100644
index 00000000..63092365
Binary files /dev/null and b/inst/extdata/meta_subnational2_codes.rds differ
diff --git a/inst/extdata/utm_squares.rds b/inst/extdata/meta_utm_squares.rds
similarity index 100%
rename from inst/extdata/utm_squares.rds
rename to inst/extdata/meta_utm_squares.rds
diff --git a/inst/extdata/meta_v_local.rds b/inst/extdata/meta_v_local.rds
new file mode 100644
index 00000000..fe64f4f0
Binary files /dev/null and b/inst/extdata/meta_v_local.rds differ
diff --git a/inst/extdata/metadata_v_local.rds b/inst/extdata/metadata_v_local.rds
index 0f26001c..fe64f4f0 100644
Binary files a/inst/extdata/metadata_v_local.rds and b/inst/extdata/metadata_v_local.rds differ
diff --git a/inst/extdata/species_codes.rds b/inst/extdata/species_codes.rds
deleted file mode 100644
index 83e17728..00000000
Binary files a/inst/extdata/species_codes.rds and /dev/null differ
diff --git a/inst/extdata/species_taxonomy.rds b/inst/extdata/species_taxonomy.rds
deleted file mode 100644
index 9c9b55ff..00000000
Binary files a/inst/extdata/species_taxonomy.rds and /dev/null differ
diff --git a/inst/extdata/statprov_codes.rds b/inst/extdata/statprov_codes.rds
deleted file mode 100644
index dae1d77b..00000000
Binary files a/inst/extdata/statprov_codes.rds and /dev/null differ
diff --git a/inst/extdata/subnational2_codes.rds b/inst/extdata/subnational2_codes.rds
deleted file mode 100644
index 21320d60..00000000
Binary files a/inst/extdata/subnational2_codes.rds and /dev/null differ
diff --git a/inst/extdata/test_data.rds b/inst/extdata/test_data.rds
new file mode 100644
index 00000000..466468dd
Binary files /dev/null and b/inst/extdata/test_data.rds differ
diff --git a/man/cosewic_plot.Rd b/man/cosewic_plot.Rd
index c80422ec..0cdf4e43 100644
--- a/man/cosewic_plot.Rd
+++ b/man/cosewic_plot.Rd
@@ -9,6 +9,7 @@ cosewic_plot(
points = NULL,
grid = NULL,
map = NULL,
+ scale = FALSE,
species = "species_id",
title = ""
)
@@ -24,6 +25,10 @@ Raw data points will be added to the plot if provided.}
\item{map}{sf data frame. Optional base map over which to plot the values.}
+\item{scale}{Logical. Whether to scale the IAO legends to a proportion for
+easier plotting of mutliple species (allows collecting legends by
+patchwork).}
+
\item{species}{Character. Name of the column containing species
identification.}
@@ -39,6 +44,7 @@ Creates a plot of COSEWIC ranges for illustration and checking.
\examples{
r <- cosewic_ranges(bcch)
cosewic_plot(r)
+cosewic_plot(r, scale = TRUE)
cosewic_plot(r, points = bcch)
cosewic_plot(r, grid = grid_canada(50), map = map_canada(),
title = "Black-capped chickadees")
@@ -46,6 +52,7 @@ cosewic_plot(r, grid = grid_canada(50), map = map_canada(),
m <- rbind(bcch, hofi)
r <- cosewic_ranges(m)
cosewic_plot(r)
+cosewic_plot(r, scale = TRUE)
cosewic_plot(r, points = m)
p <- cosewic_plot(r, grid = grid_canada(50), map = map_canada(),
title = c("14280" = "Black-capped chickadees",
diff --git a/man/cosewic_ranges.Rd b/man/cosewic_ranges.Rd
index 3262ef1e..d4f5a9b3 100644
--- a/man/cosewic_ranges.Rd
+++ b/man/cosewic_ranges.Rd
@@ -12,6 +12,7 @@ cosewic_ranges(
species = "species_id",
iao_grid_size_km = 2,
eoo_p = 0.95,
+ eoo_clip = NULL,
filter_unique = FALSE,
spatial = TRUE
)
@@ -38,6 +39,10 @@ Defaults to 0.95 for a 95\% convex hull to ensure outlier points do not
artificially inflate the EOO. Note that for a final COSEWIC report, this
may not be appropriate. Set to 1 to include all points.}
+\item{eoo_clip}{sf (Multi)Polygon. A spatial object to clip the EOO to. May
+be relevant when calculating EOOs for complex regions (i.e. long curved
+areas) to avoid including area which cannot have observations.}
+
\item{filter_unique}{Logical. Whether to filter observations to unique
locations. Use this only if there are too many data points to work with.
This changes the nature of what an observation is, and also may bias
@@ -118,5 +123,17 @@ mult <- rbind(bcch, hofi)
r <- cosewic_ranges(mult)
r <- cosewic_ranges(mult, spatial = FALSE)
+# Clip to a specific region
+\dontshow{if (requireNamespace("rnaturalearth", quietly = TRUE) & requireNamespace("rnaturalearthhires", quietly = TRUE)) (if (getRversion() >= "3.4") withAutoprint else force)(\{ # examplesIf}
+
+library(rnaturalearth)
+ON <- ne_states("Canada") |>
+ dplyr::filter(postal == "ON")
+
+r <- cosewic_ranges(mult)
+cosewic_plot(r, map = ON) # No clip
+r <- cosewic_ranges(mult, eoo_clip = ON)
+cosewic_plot(r, map = ON) # With clip
+\dontshow{\}) # examplesIf}
}
diff --git a/naturecounts.Rproj b/naturecounts.Rproj
index 96f204b5..f09d67b6 100644
--- a/naturecounts.Rproj
+++ b/naturecounts.Rproj
@@ -1,4 +1,5 @@
Version: 1.0
+ProjectId: ed85b2b7-cd25-4773-a28e-1a334954232c
RestoreWorkspace: Default
SaveWorkspace: Default
diff --git a/tests/testthat/_snaps/12_cosewic_tools/p-basic.svg b/tests/testthat/_snaps/12_cosewic_tools/p-basic.svg
index 25db724f..d6fa09ec 100644
--- a/tests/testthat/_snaps/12_cosewic_tools/p-basic.svg
+++ b/tests/testthat/_snaps/12_cosewic_tools/p-basic.svg
@@ -20,99 +20,101 @@
-
-
+
+
-
+
-
-
+
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-45.6
-°
-N
-45.8
-°
-N
-46.0
-°
-N
-46.2
-°
-N
-77.5
-°
-W
-77.0
-°
-W
-76.5
-°
-W
-
-EOO 95%
-IAO
-No. records
-
-
-
-
-
-
-
-10
-20
-30
-14280
-Showing 2x2km grids
-Analysis used 2x2 km
+45.6
+°
+N
+45.8
+°
+N
+46.0
+°
+N
+46.2
+°
+N
+77.5
+°
+W
+77.0
+°
+W
+76.5
+°
+W
+
+EOO 95%
+IAO
+No. records
+
+
+
+
+
+
+
+10
+20
+30
+160 records
+(1-36 per 2x2 km grid)
+14280
+Showing 2x2km grids
+Analysis used 2x2 km
diff --git a/tests/testthat/_snaps/12_cosewic_tools/p-map.svg b/tests/testthat/_snaps/12_cosewic_tools/p-map.svg
index 4d93eecf..a3bdbdbc 100644
--- a/tests/testthat/_snaps/12_cosewic_tools/p-map.svg
+++ b/tests/testthat/_snaps/12_cosewic_tools/p-map.svg
@@ -20,95 +20,97 @@
-
-
+
+
-
+
-
-
+
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-40
-°
-N
-50
-°
-N
-60
-°
-N
-100
-°
-W
-IAO
-No. records
-
-
-
-
-
-
-
-
-
-50
-75
-100
-125
-
-EOO 95%
-Black-capped Chickadees
-Showing 200x200km grids
-Analysis used 2x2 km
+40
+°
+N
+50
+°
+N
+60
+°
+N
+100
+°
+W
+IAO
+No. records
+
+
+
+
+
+
+
+
+
+50
+75
+100
+125
+
+EOO 95%
+160 records
+(1-36 per 2x2 km grid)
+Black-capped Chickadees
+Showing 200x200km grids
+Analysis used 2x2 km
diff --git a/tests/testthat/_snaps/12_cosewic_tools/p-no-cols-basic.svg b/tests/testthat/_snaps/12_cosewic_tools/p-no-cols-basic.svg
index 9c478e1d..e50b6e05 100644
--- a/tests/testthat/_snaps/12_cosewic_tools/p-no-cols-basic.svg
+++ b/tests/testthat/_snaps/12_cosewic_tools/p-no-cols-basic.svg
@@ -20,98 +20,100 @@
-
-
+
+
-
+
-
-
+
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-45.6
-°
-N
-45.8
-°
-N
-46.0
-°
-N
-46.2
-°
-N
-77.5
-°
-W
-77.0
-°
-W
-76.5
-°
-W
-
-EOO 95%
-IAO
-No. records
-
-
-
-
-
-
-
-10
-20
-30
-Showing 2x2km grids
-Analysis used 2x2 km
+45.6
+°
+N
+45.8
+°
+N
+46.0
+°
+N
+46.2
+°
+N
+77.5
+°
+W
+77.0
+°
+W
+76.5
+°
+W
+
+EOO 95%
+IAO
+No. records
+
+
+
+
+
+
+
+10
+20
+30
+160 records
+(1-36 per 2x2 km grid)
+Showing 2x2km grids
+Analysis used 2x2 km
diff --git a/tests/testthat/_snaps/12_cosewic_tools/p-no-cols-map.svg b/tests/testthat/_snaps/12_cosewic_tools/p-no-cols-map.svg
index 4d93eecf..a3bdbdbc 100644
--- a/tests/testthat/_snaps/12_cosewic_tools/p-no-cols-map.svg
+++ b/tests/testthat/_snaps/12_cosewic_tools/p-no-cols-map.svg
@@ -20,95 +20,97 @@
-
-
+
+
-
+
-
-
+
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-40
-°
-N
-50
-°
-N
-60
-°
-N
-100
-°
-W
-IAO
-No. records
-
-
-
-
-
-
-
-
-
-50
-75
-100
-125
-
-EOO 95%
-Black-capped Chickadees
-Showing 200x200km grids
-Analysis used 2x2 km
+40
+°
+N
+50
+°
+N
+60
+°
+N
+100
+°
+W
+IAO
+No. records
+
+
+
+
+
+
+
+
+
+50
+75
+100
+125
+
+EOO 95%
+160 records
+(1-36 per 2x2 km grid)
+Black-capped Chickadees
+Showing 200x200km grids
+Analysis used 2x2 km
diff --git a/tests/testthat/_snaps/12_cosewic_tools/p-no-cols-mult.svg b/tests/testthat/_snaps/12_cosewic_tools/p-no-cols-mult.svg
index f760c35c..d6ea82ca 100644
--- a/tests/testthat/_snaps/12_cosewic_tools/p-no-cols-mult.svg
+++ b/tests/testthat/_snaps/12_cosewic_tools/p-no-cols-mult.svg
@@ -20,131 +20,133 @@
-
-
+
+
-
+
-
-
+
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-42
-°
-N
-43
-°
-N
-44
-°
-N
-45
-°
-N
-46
-°
-N
-47
-°
-N
-83
-°
-W
-82
-°
-W
-81
-°
-W
-80
-°
-W
-79
-°
-W
-78
-°
-W
-77
-°
-W
-
-EOO 95%
-IAO
-No. records
-
-
-
-
-
-
-
-10
-20
-30
-Showing 2x2km grids
-Analysis used 2x2 km
+42
+°
+N
+43
+°
+N
+44
+°
+N
+45
+°
+N
+46
+°
+N
+47
+°
+N
+83
+°
+W
+82
+°
+W
+81
+°
+W
+80
+°
+W
+79
+°
+W
+78
+°
+W
+77
+°
+W
+
+EOO 95%
+IAO
+No. records
+
+
+
+
+
+
+
+10
+20
+30
+179 records
+(1-36 per 2x2 km grid)
+Showing 2x2km grids
+Analysis used 2x2 km
diff --git a/tests/testthat/_snaps/12_cosewic_tools/p-no-cols-points.svg b/tests/testthat/_snaps/12_cosewic_tools/p-no-cols-points.svg
index 1af08592..b4673fcc 100644
--- a/tests/testthat/_snaps/12_cosewic_tools/p-no-cols-points.svg
+++ b/tests/testthat/_snaps/12_cosewic_tools/p-no-cols-points.svg
@@ -20,258 +20,260 @@
-
-
+
+
-
+
-
-
+
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-45.6
-°
-N
-45.8
-°
-N
-46.0
-°
-N
-46.2
-°
-N
-77.5
-°
-W
-77.0
-°
-W
-76.5
-°
-W
-
-EOO 95%
-IAO
-No. records
-
-
-
-
-
-
-
-10
-20
-30
-Showing 2x2km grids
-Analysis used 2x2 km
+45.6
+°
+N
+45.8
+°
+N
+46.0
+°
+N
+46.2
+°
+N
+77.5
+°
+W
+77.0
+°
+W
+76.5
+°
+W
+
+EOO 95%
+IAO
+No. records
+
+
+
+
+
+
+
+10
+20
+30
+160 records
+(1-36 per 2x2 km grid)
+Showing 2x2km grids
+Analysis used 2x2 km
diff --git a/tests/testthat/_snaps/12_cosewic_tools/p-points.svg b/tests/testthat/_snaps/12_cosewic_tools/p-points.svg
index 6699a563..04d23c9b 100644
--- a/tests/testthat/_snaps/12_cosewic_tools/p-points.svg
+++ b/tests/testthat/_snaps/12_cosewic_tools/p-points.svg
@@ -20,259 +20,261 @@
-
-
+
+
-
+
-
-
+
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-45.6
-°
-N
-45.8
-°
-N
-46.0
-°
-N
-46.2
-°
-N
-77.5
-°
-W
-77.0
-°
-W
-76.5
-°
-W
-
-EOO 95%
-IAO
-No. records
-
-
-
-
-
-
-
-10
-20
-30
-14280
-Showing 2x2km grids
-Analysis used 2x2 km
+45.6
+°
+N
+45.8
+°
+N
+46.0
+°
+N
+46.2
+°
+N
+77.5
+°
+W
+77.0
+°
+W
+76.5
+°
+W
+
+EOO 95%
+IAO
+No. records
+
+
+
+
+
+
+
+10
+20
+30
+160 records
+(1-36 per 2x2 km grid)
+14280
+Showing 2x2km grids
+Analysis used 2x2 km
diff --git a/tests/testthat/test_03_metadata.R b/tests/testthat/test_03_metadata.R
index d9f4644e..4e15cc57 100644
--- a/tests/testthat/test_03_metadata.R
+++ b/tests/testthat/test_03_metadata.R
@@ -2,7 +2,7 @@
test_that("Metadata updates", {
# Get original file dates
loc <- list.files(system.file("extdata", package = "naturecounts"),
- pattern = ".rds", full.names = TRUE) %>%
+ pattern = "meta_", full.names = TRUE) %>%
subset(!stringr::str_detect(., "utm")) %>%
file.info()
@@ -11,7 +11,7 @@ test_that("Metadata updates", {
suppressMessages()
loc2 <- list.files(system.file("extdata", package = "naturecounts"),
- pattern = ".rds", full.names = TRUE) %>%
+ pattern = "meta_", full.names = TRUE) %>%
subset(!stringr::str_detect(., "utm")) %>%
file.info()
@@ -23,7 +23,7 @@ test_that("Metadata updates", {
expect_message(nc_metadata(force = FALSE), "already up-to-date with server")
loc3 <- list.files(system.file("extdata", package = "naturecounts"),
- pattern = ".rds", full.names = TRUE) %>%
+ pattern = "meta_", full.names = TRUE) %>%
subset(!stringr::str_detect(., "utm")) %>%
file.info()
diff --git a/tests/testthat/test_10_helpers.R b/tests/testthat/test_10_helpers.R
index 96930af4..e96577e9 100644
--- a/tests/testthat/test_10_helpers.R
+++ b/tests/testthat/test_10_helpers.R
@@ -1,3 +1,7 @@
+expect_silent(
+ test_rc <- readRDS(system.file("extdata", "test_data.rds", package = "naturecounts"))
+)
+
# format_dates() ----------------------------------------------------------
test_that("format_dates() with data frame", {
for(i in 1:2) {
diff --git a/tests/testthat/test_12_cosewic_tools.R b/tests/testthat/test_12_cosewic_tools.R
index 0f6ab85f..5731ab8a 100644
--- a/tests/testthat/test_12_cosewic_tools.R
+++ b/tests/testthat/test_12_cosewic_tools.R
@@ -17,34 +17,34 @@ test_that("prep_spatial() diff cols", {
test_that("cosewic_eoo()", {
df <- prep_spatial(bcch)
- expect_silent(e <- cosewic_eoo(df, p = 0.95, spatial = FALSE))
+ expect_silent(e <- cosewic_eoo(df, p = 0.95, spatial = FALSE, clip = NULL))
expect_s3_class(e, "data.frame")
expect_named(e, "eoo")
expect_equal(e[["eoo"]], units::set_units(1243.421, "km2"), tolerance = 0.001)
- expect_silent(e <- cosewic_eoo(df, p = 0.95, spatial = TRUE))
+ expect_silent(e <- cosewic_eoo(df, p = 0.95, spatial = TRUE, clip = NULL))
expect_s3_class(e, "sf")
expect_equal(nrow(e), 1)
expect_equal(as.character(sf::st_geometry_type(e)), "POLYGON")
- expect_silent(e <- cosewic_eoo(df, p = 1, spatial = FALSE))
+ expect_silent(e <- cosewic_eoo(df, p = 1, spatial = FALSE, clip = NULL))
expect_equal(e[["eoo"]], units::set_units(4861.251, "km2"), tolerance = 0.001)
})
test_that("cosewic_eoo() diff cols", {
df <- dplyr::rename(bcch, sp = species_id, rec = record_id) |>
prep_spatial(extra = "rec")
- expect_silent(e <- cosewic_eoo(df, p = 0.95, spatial = FALSE))
+ expect_silent(e <- cosewic_eoo(df, p = 0.95, spatial = FALSE, clip = NULL))
expect_s3_class(e, "data.frame")
expect_named(e, "eoo")
expect_equal(e[["eoo"]], units::set_units(1243.421, "km2"), tolerance = 0.001)
- expect_silent(e <- cosewic_eoo(df, p = 0.95, spatial = TRUE))
+ expect_silent(e <- cosewic_eoo(df, p = 0.95, spatial = TRUE, clip = NULL))
expect_s3_class(e, "sf")
expect_equal(nrow(e), 1)
expect_equal(as.character(sf::st_geometry_type(e)), "POLYGON")
- expect_silent(e <- cosewic_eoo(df, p = 1, spatial = FALSE))
+ expect_silent(e <- cosewic_eoo(df, p = 1, spatial = FALSE, clip = NULL))
expect_equal(e[["eoo"]], units::set_units(4861.251, "km2"), tolerance = 0.001)
})
@@ -52,17 +52,17 @@ test_that("cosewic_eoo() no cols", {
df <- dplyr::select(bcch, -"species_id") |>
dplyr::mutate(record_id = dplyr::row_number()) |>
prep_spatial()
- expect_silent(e <- cosewic_eoo(df, p = 0.95, spatial = FALSE))
+ expect_silent(e <- cosewic_eoo(df, p = 0.95, spatial = FALSE, clip = NULL))
expect_s3_class(e, "data.frame")
expect_named(e, "eoo")
expect_equal(e[["eoo"]], units::set_units(1243.421, "km2"), tolerance = 0.001)
- expect_silent(e <- cosewic_eoo(df, p = 0.95, spatial = TRUE))
+ expect_silent(e <- cosewic_eoo(df, p = 0.95, spatial = TRUE, clip = NULL))
expect_s3_class(e, "sf")
expect_equal(nrow(e), 1)
expect_equal(as.character(sf::st_geometry_type(e)), "POLYGON")
- expect_silent(e <- cosewic_eoo(df, p = 1, spatial = FALSE))
+ expect_silent(e <- cosewic_eoo(df, p = 1, spatial = FALSE, clip = NULL))
expect_equal(e[["eoo"]], units::set_units(4861.251, "km2"), tolerance = 0.001)
})
@@ -254,6 +254,17 @@ test_that("cosewic_ranges() filter_unique", {
})
+test_that("cosewic_ranges() eoo clip", {
+ ON <- rnaturalearth::ne_states("Canada") |>
+ dplyr::filter(postal == "ON")
+ mult <- rbind(bcch, hofi)
+
+ expect_silent(r0 <- cosewic_ranges(mult))
+ expect_silent(r1 <- cosewic_ranges(mult, eoo_clip = ON))
+ expect_true(all(r0$eoo$eoo_p95 > r1$eoo$eoo_p95))
+})
+
+
test_that("cosewic_plot()", {
expect_silent(r1 <- cosewic_ranges(bcch))