From e2da1f12d06f3fcf1d19b579ac30b0c6379822fa Mon Sep 17 00:00:00 2001 From: your name Date: Tue, 20 Aug 2024 01:21:54 -0400 Subject: [PATCH] Fix #91 Also add error message to avoid confusion like #96 and #94 --- R/internal.R | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/R/internal.R b/R/internal.R index 4229cde..f86621d 100644 --- a/R/internal.R +++ b/R/internal.R @@ -95,6 +95,10 @@ loc_check <- function(locations, stop("Please supply a valid crs via locations or prj.") } + stopifnot( + "`locations` must contain column names matching `coords`" = all(coords %in% names(locations)) + ) + locations <- sf::st_as_sf(x = locations, coords = coords, crs = prj) } else if (inherits(locations, c("sf", "sfc", "sfg"))) { sf_crs <- sf::st_crs(locations) @@ -112,11 +116,15 @@ loc_check <- function(locations, } else if (any(class(locations) %in% c("SpatRaster", "SpatVector"))) { sf_crs <- sf::st_crs(locations) - locations <- sf::st_as_sf( - terra::as.points(locations, values = FALSE), - coords = terra::crds(locations, df = TRUE), - crs = sf_crs - ) + coords <- terra::crds(locations, df = TRUE) + + if (inherits(locations, "SpatVector")) { + locations <- terra::as.points(locations) + } else { + locations <- terra::as.points(locations, values = FALSE) + } + + locations <- sf::st_as_sf(locations, coords = coords, crs = sf_crs) if ((is.null(sf_crs) || is.na(sf_crs)) && is.null(prj)) { stop("Please supply a valid crs via locations or prj.")