Skip to content

Commit

Permalink
Merge pull request #5 from atsyplenkov/tests
Browse files Browse the repository at this point in the history
fix: removed methods for SpatRaster. Closes #2
  • Loading branch information
atsyplenkov authored Jan 8, 2025
2 parents 282d06a + b35affa commit 009af7c
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 83 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ Imports:
grDevices,
reticulate,
S7 (>= 0.2.0),
terra (>= 1.7),
stats,
cli
Suggests:
terra (>= 1.7),
testthat (>= 3.0.0)
Config/testthat/edition: 3
SystemRequirements: Python (>= 3.8.0), numpy, whitebox-workflows (>= v1.3.3)
Expand Down
137 changes: 62 additions & 75 deletions R/terra.R
Original file line number Diff line number Diff line change
Expand Up @@ -95,86 +95,73 @@ S7::method(as_rast, WhiteboxRaster) <-
#' }
#' @export
as_wbw_raster <-
S7::new_generic(
name = "as_wbw_raster",
dispatch_args = "x",
fun = function(x) {
S7::S7_dispatch()
}
)

if (requireNamespace("terra", quietly = TRUE)) {
S7::method(
as_wbw_raster,
methods::getClass("SpatRaster", where = asNamespace("terra"))
) <-
function(x) {
# Checks
checkmate::assert_class(
wbw,
classes = c(
"python.builtin.module",
"python.builtin.object"
)
)
checkmate::assert_true(
terra::nlyr(x) == 1
function(x) {
# Checks
checkmate::assert_class(x, "SpatRaster")
checkmate::assert_class(
wbw,
classes = c(
"python.builtin.module",
"python.builtin.object"
)
)
checkmate::assert_true(
terra::nlyr(x) == 1
)

# SpatRaster information
na_terra <- terra::NAflag(x)
nodata_value <- if (is.nan(na_terra)) {
-9999
} else {
na_terra
}
res_terra <- terra::res(x)
name_terra <- names(x)
type_terra <- any(c(
terra::is.int(x),
terra::is.bool(x),
terra::is.factor(x)
))
ext_terra <- terra::ext(x)
data_terra <- as.matrix(x, wide = TRUE)
data_terra[is.na(data_terra)] <- nodata_value
# SpatRaster information
na_terra <- terra::NAflag(x)
nodata_value <- if (is.nan(na_terra)) {
-9999
} else {
na_terra
}
res_terra <- terra::res(x)
name_terra <- names(x)
type_terra <- any(c(
terra::is.int(x),
terra::is.bool(x),
terra::is.factor(x)
))
ext_terra <- terra::ext(x)
data_terra <- as.matrix(x, wide = TRUE)
data_terra[is.na(data_terra)] <- nodata_value

# Create new RasterConfigs
new_config <- wbw$RasterConfigs()
new_config$title <- name_terra
# Create new RasterConfigs
new_config <- wbw$RasterConfigs()
new_config$title <- name_terra

# Dimensions
new_config$bands <- as.integer(terra::nlyr(x))
new_config$columns <- as.integer(terra::ncol(x))
new_config$rows <- as.integer(terra::nrow(x))
new_config$west <- as.double(ext_terra[1])
## Note the differences in east and south between reading by
## GDAL (i.e. terra) and WhiteboxTools
new_config$east <- as.double(ext_terra[2]) - res_terra[1]
new_config$south <- as.double(ext_terra[3]) + res_terra[2]
new_config$north <- as.double(ext_terra[4])
# Dimensions
new_config$bands <- as.integer(terra::nlyr(x))
new_config$columns <- as.integer(terra::ncol(x))
new_config$rows <- as.integer(terra::nrow(x))
new_config$west <- as.double(ext_terra[1])
## Note the differences in east and south between reading by
## GDAL (i.e. terra) and WhiteboxTools
new_config$east <- as.double(ext_terra[2]) - res_terra[1]
new_config$south <- as.double(ext_terra[3]) + res_terra[2]
new_config$north <- as.double(ext_terra[4])

# CRS
new_config$coordinate_ref_system_wkt <- terra::crs(x)
new_config$resolution_x <- res_terra[1]
new_config$resolution_y <- res_terra[2]
# CRS
new_config$coordinate_ref_system_wkt <- terra::crs(x)
new_config$resolution_x <- res_terra[1]
new_config$resolution_y <- res_terra[2]

# Data type
new_config$nodata <- nodata_value
new_config$data_type <-
if (type_terra) {
wbw$RasterDataType$I16
} else {
wbw$RasterDataType$F32
}
# Data type
new_config$nodata <- nodata_value
new_config$data_type <-
if (type_terra) {
wbw$RasterDataType$I16
} else {
wbw$RasterDataType$F32
}

# Create WhiteboxRaster
new_raster <- wbe$new_raster(new_config)
wbw_env$matrix_to_wbw(data_terra, new_raster)
# Create WhiteboxRaster
new_raster <- wbe$new_raster(new_config)
wbw_env$matrix_to_wbw(data_terra, new_raster)

WhiteboxRaster(
name = name_terra,
source = new_raster
)
}
}
WhiteboxRaster(
name = name_terra,
source = new_raster
)
}
4 changes: 2 additions & 2 deletions R/zzz.R
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ wbw_version <-
.onAttach <- function(libname, pkgname) {
wbwv <- wbw_version()
suppress <-
!grepl("suppress", Sys.getenv("wbw.message"), ignore.case = TRUE)
!grepl("suppressed", Sys.getenv("wbw.message"), ignore.case = TRUE)

if (is.null(wbwv) && suppress && interactive()) {
cli::cli_alert_warning(c(
Expand All @@ -181,7 +181,7 @@ wbw_version <-
))
} else if (!is.null(wbwv) && suppress) {
cli::cli_alert_success(c(
"wbw v{wbwv} -- using whitebox-workflows v{wbwv}"
"wbw v{utils::packageVersion('wbw')} -- using whitebox-workflows v{wbwv}"
))
}
}
Expand Down
10 changes: 5 additions & 5 deletions tests/test-setup.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ raster_path <-
x <- wbw_read_raster(raster_path)

# Path to terra's files
f <- system.file("ex/elev.tif", package = "terra")
# if (requireNamespace("terra", quietly = TRUE)) {
# library(terra)
# f <- system.file("ex/elev.tif", package = "terra")
# }
# f <- system.file("ex/elev.tif", package = "terra")
if (requireNamespace("terra", quietly = TRUE)) {
library(terra)
f <- system.file("ex/elev.tif", package = "terra")
}

1 comment on commit 009af7c

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.