From 5603c66f5d9af433260c8164dd411bb3f9d548ee Mon Sep 17 00:00:00 2001 From: see24 Date: Fri, 23 Jun 2023 14:28:57 -0400 Subject: [PATCH] remove pfocal --- DESCRIPTION | 5 +- R/getDistFromSource.R | 91 +++++++++++++------------ tests/testthat/test-getDistFromSource.R | 8 +-- 3 files changed, 53 insertions(+), 51 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 6054e6e..9358f27 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -53,10 +53,7 @@ Suggests: rmarkdown, viridis, tmap, - bench, - pfocal -Remotes: LandSciTech/pfocal -Additional_repositories: https://LandSciTech.github.io/drat + bench VignetteBuilder: knitr Depends: R (>= 2.10) Collate: diff --git a/R/getDistFromSource.R b/R/getDistFromSource.R index 00c4888..4e6f867 100644 --- a/R/getDistFromSource.R +++ b/R/getDistFromSource.R @@ -37,8 +37,9 @@ #' of the CRS. #' @param kwidth Integer, for the "pfocal" and "terra" methods the width of the #' moving window. For the "pfocal2" method the aggregation factor. -#' @param method Character, the method to use, one of "terra", "pfocal" or -#' "pfocal2". See below for details. +#' @param method Character, the method to use, currently only "terra" supported +# #' one of "terra", "pfocal" or +# #' "pfocal2". See below for details. #' #' @return A SpatRaster #' @export @@ -75,38 +76,40 @@ getDistFromSource <- function(src, maxDist, kwidth = 3, method = "terra") { src <- terra::rast(src) } if(method == "pfocal2"){ - if(!rlang::is_installed("pfocal")){ - stop("The pfocal2 method requires the pfocal package. ", - "It can be installed with remotes::install_github('LandSciTech/pfocal')", - call. = FALSE) - } - src <- src > 0 - - # aggregate based on kwidth in a way that matches the moving window version - if(kwidth > 1){ - src <- terra::aggregate(src, kwidth, fun = "max", na.rm = TRUE) - } - # convert maxDist to ncells - maxDist <- maxDist/terra::res(src)[1] - - # make kernal for distance from center - kn <- pfocal::euclidean_distance_kernel(maxDist) - kn <- kn*terra::res(src)[1] - - - src2 <- terra::mask(src, src, maskvalues = 0, updatevalue = NA) - dist_src <- pfocal::pfocal(terra::as.matrix(src2, wide = TRUE), kernel = kn, edge_value = NA, na.rm = TRUE, - reduce_function = "min") - dist_src <- terra::rast(src, vals = dist_src) - # min when all NA is Inf so need to convert to NA - dist_src <- terra::mask(dist_src, dist_src > (maxDist * terra::res(src2)[1]), - maskvalues = 1, updatevalue = NA) - - if(kwidth > 1){ - dist_src <- terra::disagg(dist_src, fact = kwidth, method = "bilinear") - } - - return(dist_src) + stop("method pfocal2 is only available with the development version of roads.", + call. = FALSE) + # if(!rlang::is_installed("pfocal")){ + # stop("The pfocal2 method requires the pfocal package. ", + # "It can be installed with remotes::install_github('LandSciTech/pfocal')", + # call. = FALSE) + # } + # src <- src > 0 + # + # # aggregate based on kwidth in a way that matches the moving window version + # if(kwidth > 1){ + # src <- terra::aggregate(src, kwidth, fun = "max", na.rm = TRUE) + # } + # # convert maxDist to ncells + # maxDist <- maxDist/terra::res(src)[1] + # + # # make kernal for distance from center + # kn <- pfocal::euclidean_distance_kernel(maxDist) + # kn <- kn*terra::res(src)[1] + # + # + # src2 <- terra::mask(src, src, maskvalues = 0, updatevalue = NA) + # dist_src <- pfocal::pfocal(terra::as.matrix(src2, wide = TRUE), kernel = kn, edge_value = NA, na.rm = TRUE, + # reduce_function = "min") + # dist_src <- terra::rast(src, vals = dist_src) + # # min when all NA is Inf so need to convert to NA + # dist_src <- terra::mask(dist_src, dist_src > (maxDist * terra::res(src2)[1]), + # maskvalues = 1, updatevalue = NA) + # + # if(kwidth > 1){ + # dist_src <- terra::disagg(dist_src, fact = kwidth, method = "bilinear") + # } + # + # return(dist_src) } src <- src > 0 if (dissag) { @@ -127,15 +130,17 @@ getDistFromSource <- function(src, maxDist, kwidth = 3, method = "terra") { ssD2 <- terra::focal(cPop, w = mm, fun = "sum", na.rm = TRUE) # ssD2 <- terra::init(cPop, fun = ssO2) } else if(method == "pfocal"){ - if(!rlang::is_installed("pfocal")){ - stop("The pfocal method requires the pfocal package. ", - "It can be installed with remotes::install_github('LandSciTech/pfocal')", - call. = FALSE) - } - ssO2 <- pfocal::pfocal(as.matrix(cPop, wide = TRUE), mm, reduce_function = "SUM", - transform_function = "MULTIPLY") - ssD2 <- cPop - terra::values(ssD2) <- ssO2 + stop("method pfocal is only available with the development version of roads.", + call. = FALSE) + # if(!rlang::is_installed("pfocal")){ + # stop("The pfocal method requires the pfocal package. ", + # "It can be installed with remotes::install_github('LandSciTech/pfocal')", + # call. = FALSE) + # } + # ssO2 <- pfocal::pfocal(as.matrix(cPop, wide = TRUE), mm, reduce_function = "SUM", + # transform_function = "MULTIPLY") + # ssD2 <- cPop + # terra::values(ssD2) <- ssO2 } # Add back 0s for existing roads ssD2 <- terra::mask(ssD2, dd, inverse = TRUE, updatevalue = 0) diff --git a/tests/testthat/test-getDistFromSource.R b/tests/testthat/test-getDistFromSource.R index b2287fe..78bef24 100644 --- a/tests/testthat/test-getDistFromSource.R +++ b/tests/testthat/test-getDistFromSource.R @@ -18,10 +18,10 @@ test_that("distance to roads has expected values", { maxDist <- 5 fastRough <- getDistFromSource(src, maxDist, kwidth = 1) wideCircle <- getDistFromSource(src, maxDist, kwidth = 5) - if(requireNamespace("pfocal", quietly = TRUE)){ - slowFine <- getDistFromSource(src, maxDist, kwidth = 1, method = "pfocal2") - smootherCircle <- getDistFromSource(src, maxDist, kwidth = 5, method = "pfocal2") - } + # if(requireNamespace("pfocal", quietly = TRUE)){ + # slowFine <- getDistFromSource(src, maxDist, kwidth = 1, method = "pfocal2") + # smootherCircle <- getDistFromSource(src, maxDist, kwidth = 5, method = "pfocal2") + # } if(interactive()){ res <- c(fastRough, slowFine, wideCircle, smootherCircle)