Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ LinkingTo: RcppArmadillo, Rcpp
License: GPL (>= 2)
Encoding: UTF-8
LazyLoad: yes
Suggests: sp,
Suggests: terra,
sp,
knitr,
rmarkdown,
testthat
Expand All @@ -46,5 +47,6 @@ Collate:
'clhs.R'
'clhs-sf.R'
'clhs-sp.R'
'clhs-terra.R'
'plot.R'
'similarity.R'
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# Generated by roxygen2: do not edit by hand

S3method(clhs,Raster)
S3method(clhs,SpatRaster)
S3method(clhs,SpatVector)
S3method(clhs,SpatialPointsDataFrame)
S3method(clhs,data.frame)
S3method(clhs,sf)
Expand Down
65 changes: 65 additions & 0 deletions R/clhs-terra.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#' @include clhs-data.frame.R
#' @rdname clhs
#' @export
#' @noRd
clhs.SpatRaster <- function(
x, # data
...,
use.coords = FALSE
){

if (!requireNamespace("terra")) {
stop("package 'terra' is required to convert SpatRaster objects to data.frame")
}

if (use.coords) {
df <- terra::as.data.frame(x, xy = TRUE)
} else {
df <- terra::as.data.frame(x)
}

spl <- clhs.data.frame(x = df, ...)

if (is(spl, "cLHS_result")) {
spl$initial_object <- x # replacing the data.frame by the Spat* object
spl$sampled_data <- x[spl$index_samples, ]
}

spl
}

#' @include clhs-data.frame.R
#' @rdname clhs
#' @export
#' @noRd
clhs.SpatVector <- function(
x, # data
...,
use.coords = FALSE
){

if (!requireNamespace("terra")) {
stop("package 'terra' is required to convert SpatVector objects to data.frame")
}

if (use.coords) {
if (!terra::is.points(x)) {
stop("When `use.coords` is set to TRUE, only POINT geometries are supported",
call. = FALSE)
}
df <- terra::as.data.frame(x, geom = "XY")
} else {

df <- terra::as.data.frame(x)
}

spl <- clhs.data.frame(x = df, ...)

if (is(spl, "cLHS_result")) {
spl$initial_object <- x # replacing the data.frame by the Spat* object
spl$sampled_data <- x[spl$index_samples, ]
}

spl
}

30 changes: 30 additions & 0 deletions tests/testthat/test-terra.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
context("clhs-terra")

test_that("terra SpatRaster and SpatVector methods work", {

skip_if_not_installed("terra")

suppressWarnings(RNGversion("3.5.0"))

x <- terra::rast(system.file("ex", "elev.tif", package = "terra"))

# without cpp
set.seed(1)
res1 <- clhs(x, size = 100, iter = 100, use.cpp = FALSE)

set.seed(1)
res2 <- clhs(terra::as.points(x), size = 100, iter = 100, use.cpp = FALSE)

# with cpp
set.seed(1)
res3 <- clhs(x, size = 100, simple = FALSE)

set.seed(1)
res4 <- clhs(terra::as.points(x), size = 100)

expect_equal(lengths(list(res1, res2, res3$index_samples, res4)), rep(100, 4))

expect_equal(res1, res2)
expect_equal(res3$index_samples, res4)

})