From 8960dcf5c2b0936b9c6c2242cd5e0329c464a0bc Mon Sep 17 00:00:00 2001 From: plantarum Date: Mon, 26 May 2025 12:25:26 -0400 Subject: [PATCH 1/3] Switched terra:extract to terra:relate in cc_sea In my tests this decreased the processing time for 10K records from 94 seconds to < 1 second. --- R/cc_sea.R | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/R/cc_sea.R b/R/cc_sea.R index 6a3dbc2..7edde1e 100644 --- a/R/cc_sea.R +++ b/R/cc_sea.R @@ -137,7 +137,8 @@ cc_sea <- function(x, ## have to make sure they are the same and in this case they already are ## ----- ## point-in-polygon test - ext_dat <- terra::extract(ref, pts) + ##ext_dat <- terra::extract(ref, pts) + ext_dat <- relate(pts, ref, "intersects", pairs = TRUE, na.rm = FALSE) out <- !is.na(ext_dat[!duplicated(ext_dat[, 1]), 2]) out <- data.frame(terra::geom(pts)[, c("x", "y"), drop = FALSE], out) colnames(out)[1:2] <- c(lon, lat) @@ -154,7 +155,8 @@ cc_sea <- function(x, # select relevant columns #point in polygon test - ext_dat <- terra::extract(ref, pts) + ext_dat <- relate(pts, ref, "intersects", pairs = TRUE, na.rm = FALSE) + ##ext_dat <- terra::extract(ref, pts) out <- !is.na(ext_dat[!duplicated(ext_dat[, 1]), 2]) } From f5719fea114d57ff7dfb20870b83da89ea8bbd28 Mon Sep 17 00:00:00 2001 From: plantarum Date: Mon, 26 May 2025 15:11:39 -0400 Subject: [PATCH 2/3] Addressed conflict between naturalearth and gbif country codes. Errors in tests due to naturalearth using CN-TW for Taiwan, GBIF using TW. Also -99 wasn't handled as a missing data value. --- R/cc_outl.R | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/R/cc_outl.R b/R/cc_outl.R index 93b57fe..9f15756 100644 --- a/R/cc_outl.R +++ b/R/cc_outl.R @@ -199,6 +199,13 @@ cc_outl <- function(x, area = terra::expanse(ref)) area <- area[!is.na(area$area), ] area <- area[!is.na(area$country), ] + + ## naturalearth uses -99 for missing values? + area <- area[area$country != "-99", ] + + ## CN-TW (Taiwan) doesn't map to a country in the GBIF database: + ## naturalearth dataset uses CN-TW for Taiwan, GBIF uses TW. + area[area$country == "CN-TW", "country"] <- "TW" # get number of records in GBIF per country as proxy for sampling intensity nrec <- vapply(area$country, From cb235e1cad15488a2346b2a06af28ac28f6f7396 Mon Sep 17 00:00:00 2001 From: plantarum Date: Mon, 26 May 2025 15:24:30 -0400 Subject: [PATCH 3/3] clarified call to `terra::relate` `relate` isn't availbe in the package Namespace, needs to be located in `terra::`. --- R/cc_sea.R | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/R/cc_sea.R b/R/cc_sea.R index 7edde1e..d52cc2c 100644 --- a/R/cc_sea.R +++ b/R/cc_sea.R @@ -138,7 +138,8 @@ cc_sea <- function(x, ## ----- ## point-in-polygon test ##ext_dat <- terra::extract(ref, pts) - ext_dat <- relate(pts, ref, "intersects", pairs = TRUE, na.rm = FALSE) + ext_dat <- terra::relate(pts, ref, "intersects", pairs = TRUE, + na.rm = FALSE) out <- !is.na(ext_dat[!duplicated(ext_dat[, 1]), 2]) out <- data.frame(terra::geom(pts)[, c("x", "y"), drop = FALSE], out) colnames(out)[1:2] <- c(lon, lat) @@ -155,7 +156,8 @@ cc_sea <- function(x, # select relevant columns #point in polygon test - ext_dat <- relate(pts, ref, "intersects", pairs = TRUE, na.rm = FALSE) + ext_dat <- terra::relate(pts, ref, "intersects", pairs = TRUE, + na.rm = FALSE) ##ext_dat <- terra::extract(ref, pts) out <- !is.na(ext_dat[!duplicated(ext_dat[, 1]), 2]) }