Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
99 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
#' get_nearestneighbour | ||
#' | ||
#' @description Calculates the minimal euclidian distance between classes for a raster/matrix. | ||
#' | ||
#' @param landscape RasterLayer or matrix (with x,y,id columns) | ||
#' | ||
#' @details | ||
#' Fast and memory safe Rcpp implementation for calcuting minimal euclidian distances between | ||
#' classes in a raster or matrix. | ||
#' | ||
#' @examples | ||
#' # get patches for class 1 from testdata as raster | ||
#' class_1 <- get_patches(landscape,1)[[1]] | ||
#' | ||
#' # calculate the distance between patches | ||
#' get_nearestneighbour(class_1) | ||
#' | ||
#' # do the same with a 3 column matrix (x,y,id) | ||
#' class_1_matrix <- raster::rasterToPoints(class_1) | ||
#' get_nearestneighbour(class_1_matrix) | ||
#' | ||
#' @aliases get_nearestneighbour | ||
#' @rdname get_nearestneighbour | ||
#' | ||
#' @export | ||
get_nearestneighbour <- function(landscape) UseMethod("get_nearestneighbour") | ||
|
||
#' @name get_nearestneighbour | ||
#' @export | ||
get_nearestneighbour.RasterLayer <- function(landscape) { | ||
points_mat <- raster::rasterToPoints(landscape) | ||
|
||
ord <- order(as.matrix(points_mat)[, 1]) | ||
num <- seq_along(ord) | ||
rank <- match(num, ord) | ||
|
||
res <- | ||
rcpp_get_nearest_neighbor(raster::as.matrix(points_mat)[ord, ]) | ||
|
||
min_dist <- | ||
unname(cbind(num, res[rank], as.matrix(points_mat)[, 3])) | ||
|
||
tbl <- tibble::tibble(cell = min_dist[, 1], | ||
dist = min_dist[, 2], | ||
id = min_dist[, 3]) | ||
|
||
dplyr::group_by(tbl, by = id) %>% | ||
dplyr::summarise(value = min(dist)) %>% | ||
purrr::set_names("id", "distance") | ||
} | ||
|
||
|
||
#' @name get_nearestneighbour | ||
#' @export | ||
get_nearestneighbour.matrix <- function(landscape) { | ||
|
||
if ( ncol(landscape) != 3){ | ||
stop("Coordinate matrix must have 3 (x,y,id) columns.", call. = TRUE) | ||
} | ||
|
||
ord <- order(as.matrix(landscape)[, 1]) | ||
num <- seq_along(ord) | ||
rank <- match(num, ord) | ||
|
||
res <- | ||
rcpp_get_nearest_neighbor(raster::as.matrix(landscape)[ord, ]) | ||
|
||
min_dist <- | ||
unname(cbind(num, res[rank], as.matrix(landscape)[, 3])) | ||
|
||
tbl <- tibble::tibble(cell = min_dist[, 1], | ||
dist = min_dist[, 2], | ||
id = min_dist[, 3]) | ||
|
||
dplyr::group_by(tbl, by = id) %>% | ||
dplyr::summarise(value = min(dist)) %>% | ||
purrr::set_names("id", "distance") | ||
|
||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
context("get_nearestneighbour") | ||
|
||
|
||
# get patches for class 1 from testdata as raster | ||
class_1 <- get_patches(landscape,1)[[1]] | ||
# calculate the distance between patches | ||
nn_rast <- get_nearestneighbour(class_1) | ||
# do the same with a 3 column matrix (x,y,id) | ||
class_1_matrix <- raster::rasterToPoints(class_1) | ||
nn_mat<- get_nearestneighbour(class_1_matrix) | ||
|
||
test_that("get_adjacencies runs and returns a matrix", { | ||
expect_is(nn_rast, "tbl_df") | ||
expect_is(nn_mat, "tbl_df") | ||
|
||
expect_true(nn_rast[1,2] == 7) | ||
expect_true(nn_mat[1,2] == 7) | ||
}) |