Skip to content

Commit

Permalink
implemented get_nearestneighbour
Browse files Browse the repository at this point in the history
Related to Issue #19:
#19
  • Loading branch information
marcosci committed Jul 31, 2018
1 parent d4127af commit bb254b7
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 2 deletions.
79 changes: 79 additions & 0 deletions R/get_nearestneighbour.R
@@ -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")

}
2 changes: 1 addition & 1 deletion man/get_patches.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/rcpp_get_nearest_neighbor.cpp
Expand Up @@ -112,7 +112,7 @@ dplyr::arrange(id,-y)
num <- seq_along(ord)
rank <- match(num, ord)
res <- rcpp_get_nearest_neighbor2(X[ord,])
res <- rcpp_get_nearest_neighbor(X[ord,])
unname(cbind(num, res[rank], X[, 3]))
}
Expand Down
18 changes: 18 additions & 0 deletions tests/testthat/test-get-nearestneighbour.R
@@ -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)
})

0 comments on commit bb254b7

Please sign in to comment.