Skip to content

Commit

Permalink
remove pfocal
Browse files Browse the repository at this point in the history
  • Loading branch information
see24 committed Jun 23, 2023
1 parent 5797c61 commit 5603c66
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 51 deletions.
5 changes: 1 addition & 4 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
91 changes: 48 additions & 43 deletions R/getDistFromSource.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand All @@ -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)
Expand Down
8 changes: 4 additions & 4 deletions tests/testthat/test-getDistFromSource.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 5603c66

Please sign in to comment.