Skip to content

Commit

Permalink
new function to calculate the distance between centroids of two spatr…
Browse files Browse the repository at this point in the history
…asters
  • Loading branch information
flaviomoc committed Aug 26, 2024
1 parent 7952536 commit 8047f6f
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 1 deletion.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,4 @@ VignetteBuilder:
Config/testthat/edition: 3
Encoding: UTF-8
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.3.1
RoxygenNote: 7.3.2
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Generated by roxygen2: do not edit by hand

export(area.calc)
export(dist.centroids)
export(load.data)
export(spat.alpha)
export(spat.alpha2)
Expand Down
79 changes: 79 additions & 0 deletions R/dist_centroids.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#' Function to calculate distances between centroids of multiple SpatRasters
#'
#' @param ... N number of single or multilayer spatrasters
#' @param ref Spatraster of reference
#'
#' @return A data frame with distance values
#' @export
#'
#' @examples
#' \donttest{
#' library(terra)
#' r1 <- terra::rast(system.file("extdata", "ref.tif",
#' package = "divraster"))
#' r2 <- terra::rast(system.file("extdata", "fut.tif",
#' package = "divraster"))
#' dist.centroids(r1, r2)
#' }
dist.centroids <- function(..., ref = 1) {
# Collect all SpatRasters into a list
rasters <- list(...)

# Ensure there are at least two SpatRasters
if (length(rasters) < 2) {
stop("At least two SpatRaster objects are required.")
}

# Ensure all SpatRasters have the same number of layers
n_layers <- terra::nlyr(rasters[[1]])
if (!all(sapply(rasters, terra::nlyr) == n_layers)) {
stop("All SpatRaster objects must have the same number of layers.")
}

# Initialize a list to store the distances for each layer
distances <- list()

# Get the base raster for comparison
base_raster <- rasters[[ref]]

# Loop over each layer
for (i in seq_len(n_layers)) {
# Extract the i-th layer from the base raster
base_layer <- base_raster[[i]]

# Convert base raster layer to binary and then to polygons
base_true <- base_layer == 1
base_polygons <- terra::as.polygons(base_true)
base_centroids <- terra::centroids(base_polygons)

# Initialize a data frame to store distances for this layer
dist_df <- data.frame(Layer = i)

# Loop over the other rasters and calculate distances
for (j in seq_along(rasters)) {
if (j != ref) {
# Extract the i-th layer from the current raster
current_layer <- rasters[[j]][[i]]

# Convert the current raster layer to binary and then to polygons
current_true <- current_layer == 1
current_polygons <- terra::as.polygons(current_true)
current_centroids <- terra::centroids(current_polygons)

# Calculate the distance between centroids of base and current raster layers
dist <- terra::distance(base_centroids, current_centroids, pairwise = FALSE, unit = "m")[2, 2]

# Add the distance to the data frame
dist_df[[paste0("Distance_r", ref, "_r", j)]] <- dist
}
}

# Store the distance data frame in the list
distances[[i]] <- dist_df
}

# Combine the list into a single data.frame
distances_df <- do.call(rbind, distances)

return(distances_df)
}
29 changes: 29 additions & 0 deletions man/dist.centroids.Rd

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

0 comments on commit 8047f6f

Please sign in to comment.