From f7ac0f583f6c62aa66f69c8c83c126f9def239d7 Mon Sep 17 00:00:00 2001 From: gisler Date: Sat, 18 Nov 2023 14:15:47 +0100 Subject: [PATCH] all terra now --- DESCRIPTION | 2 -- NAMESPACE | 1 - NEWS.md | 2 +- R/aaa.R | 1 - R/input.R | 40 +++++++++++++++++++--------------------- man/DEMrelatedInput.Rd | 19 +++++++++---------- 6 files changed, 29 insertions(+), 36 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 3e4dc70..06ffaee 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -26,7 +26,6 @@ Imports: checkmate, graphics, methods, - raster (>= 3.6.3), Rcpp, stats, terra, @@ -36,7 +35,6 @@ Imports: Suggests: knitr, rmarkdown, - sp, tinytest, whitebox (>= 2.0.0) LinkingTo: diff --git a/NAMESPACE b/NAMESPACE index 316bb35..36bb440 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -41,7 +41,6 @@ exportMethods(transportCalcOrder) exportMethods(transportPrerequisites) import(checkmate) import(methods) -import(raster) import(terra) importFrom(Rcpp,sourceCpp) importFrom(graphics,abline) diff --git a/NEWS.md b/NEWS.md index fabadbf..cfa802a 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,6 +1,6 @@ # RPhosFate v1.0.4.9000 -* Switched to utilising the `SpatRaster` class from the `terra` package instead of the `RasterLayer` class from the `raster` package. However, the transition from `raster` to `terra` encompasses only the core and not `DEMrelatedInput()` yet. +* Switched to utilising the `SpatRaster` and `SpatVector` classes from the `terra` package instead of the `RasterLayer` class from the `raster` and the `Spatial*DataFrame` classes from the `sp` packages. * Added `is_ths` argument to `RPhosFate()` and `catchment()` constructors: allows for specifying the number of threads to use for processing. * Ceased support for _ERDAS IMAGINE_ (\*.img) raster files. `img2tif()` can be used to convert all _ERDAS IMAGINE_ raster files in a directory and its subdirectories into _GeoTIFF_ raster files. * Dropped backward compatibility to major version zero. diff --git a/R/aaa.R b/R/aaa.R index 11c2964..e176010 100644 --- a/R/aaa.R +++ b/R/aaa.R @@ -1,6 +1,5 @@ #' @import checkmate #' @import methods -#' @import raster #' @import terra #' @importFrom graphics abline clip par #' @importFrom Rcpp sourceCpp diff --git a/R/input.R b/R/input.R index 2035ca7..03b7d27 100644 --- a/R/input.R +++ b/R/input.R @@ -67,13 +67,12 @@ demoProject <- function(cs_dir = tempdir(TRUE)) { #' raster digital elevation model. #' @param cs_cha A character string specifying a path to a potentially large #' raster providing channels. -#' @param sp_msk An [`sp::SpatialPolygonsDataFrame-class`] providing a somewhat -#' oversized catchment mask used to clip the potentially large input rasters -#' for further processing. -#' @param sp_olp An [`sp::SpatialPointsDataFrame-class`] providing the desired -#' catchment outlet(s). -#' @param sp_sds An [`sp::SpatialPointsDataFrame-class`] providing channel -#' sources. +#' @param sp_msk A [`terra::SpatVector-class`] providing a somewhat oversized +#' catchment polygon mask used to clip the potentially large input rasters for +#' further processing. +#' @param sp_olp A [`terra::SpatVector-class`] providing the desired catchment +#' outlet point(s). +#' @param sp_sds A [`terra::SpatVector-class`] providing channel source points. #' @param cs_rds An optional character string specifying a path to a potentially #' large raster providing roads. #' @param cs_wgs An optional character string specifying a path to a potentially @@ -189,8 +188,11 @@ DEMrelatedInput <- function( qassert(cs_dem, "S1") qassert(cs_cha, "S1") assertClass(sp_msk, "SpatVector") + assertTRUE(is.polygons(sp_msk)) assertClass(sp_olp, "SpatVector") + assertTRUE(is.points(sp_olp)) assertClass(sp_sds, "SpatVector") + assertTRUE(is.points(sp_sds)) if (!is.null(cs_rds)) { qassert(cs_rds, "S1") } @@ -491,10 +493,7 @@ DEMrelatedInput <- function( rl_dem_brd <- rast("dem_bnt_brd.tif") rl_dem_brd <- lapp( - c( - x = rl_dem_brd, - y = rl_cha_map_cha - ), + c(x = rl_dem_brd, y = rl_cha_map_cha), fun = function(x, y) { ifelse(is.na(y), x, x + ns_brn) }, @@ -507,13 +506,10 @@ DEMrelatedInput <- function( cells(rl_cha_map), directions = "queen", include = TRUE - )), cells(rl_cha_map_cha == 1L))] <- 1L + )), unlist(cells(rl_cha_map_cha, 1L)))] <- 1L rl_dem_brd <- lapp( - c( - x = rl_dem_brd, - y = rl_cha_map - ), + c(x = rl_dem_brd, y = rl_cha_map), fun = function(x, y) { ifelse(is.na(y), x, x + ns_brn) }, @@ -562,11 +558,13 @@ DEMrelatedInput <- function( slp = rl_slp , wsh = rl_wsh ) - for (i in seq_along(toInput)) { + for (item in names(toInput)) { + set.names(toInput[[item]], item) + writeRaster( - toInput[[i]], - filename = file.path("..", paste0(names(toInput)[i], ".tif")), - datatype = datatype(toInput[[i]]), + toInput[[item]], + filename = file.path("..", paste0(item, ".tif")), + datatype = datatype(toInput[[item]]), overwrite = TRUE ) } @@ -587,7 +585,7 @@ DEMrelatedInput <- function( nm_olc <- xyFromCell( rl_acc, - cells(is.na(rl_slp) & !is.na(rl_cha)) + unlist(cells(is.na(rl_slp) & !is.na(rl_cha), 1L)) ) # Clean up temporary files diff --git a/man/DEMrelatedInput.Rd b/man/DEMrelatedInput.Rd index ba28076..32af2f8 100644 --- a/man/DEMrelatedInput.Rd +++ b/man/DEMrelatedInput.Rd @@ -30,15 +30,14 @@ raster digital elevation model.} \item{cs_cha}{A character string specifying a path to a potentially large raster providing channels.} -\item{sp_msk}{An \code{\link[sp:SpatialPolygonsDataFrame-class]{sp::SpatialPolygonsDataFrame}} providing a somewhat -oversized catchment mask used to clip the potentially large input rasters -for further processing.} +\item{sp_msk}{A \code{\link[terra:SpatVector-class]{terra::SpatVector}} providing a somewhat oversized +catchment polygon mask used to clip the potentially large input rasters for +further processing.} -\item{sp_olp}{An \code{\link[sp:SpatialPointsDataFrame-class]{sp::SpatialPointsDataFrame}} providing the desired -catchment outlet(s).} +\item{sp_olp}{A \code{\link[terra:SpatVector-class]{terra::SpatVector}} providing the desired catchment +outlet point(s).} -\item{sp_sds}{An \code{\link[sp:SpatialPointsDataFrame-class]{sp::SpatialPointsDataFrame}} providing channel -sources.} +\item{sp_sds}{A \code{\link[terra:SpatVector-class]{terra::SpatVector}} providing channel source points.} \item{cs_rds}{An optional character string specifying a path to a potentially large raster providing roads.} @@ -115,9 +114,9 @@ nm_olc <- DEMrelatedInput( cv_dir = cv_dir, cs_dem = file.path(cs_dir_lrg, "dem_lrg.tif"), cs_cha = file.path(cs_dir_lrg, "cha_lrg.tif"), - sp_msk = raster::shapefile(file.path(cs_dir_lrg, "msk.shp")), - sp_olp = raster::shapefile(file.path(cs_dir_lrg, "olp.shp")), - sp_sds = raster::shapefile(file.path(cs_dir_lrg, "sds.shp")), + sp_msk = terra::vect(file.path(cs_dir_lrg, "msk.shp")), + sp_olp = terra::vect(file.path(cs_dir_lrg, "olp.shp")), + sp_sds = terra::vect(file.path(cs_dir_lrg, "sds.shp")), cs_rds = file.path(cs_dir_lrg, "rds_lrg.tif"), cs_wgs = file.path(cs_dir_lrg, "wgs_lrg.tif"), ls_tmp = TRUE