spokes {SpaDES.tools} | R Documentation |
Identify outward radiating spokes from initial points
Description
This is a generalized version of a notion of a viewshed. The main difference is that there can be many "viewpoints".
Usage
spokes(
landscape,
coords,
loci,
maxRadius = ncol(landscape)/4,
minRadius = maxRadius,
allowOverlap = TRUE,
stopRule = NULL,
includeBehavior = "includePixels",
returnDistances = FALSE,
angles = NA_real_,
nAngles = NA_real_,
returnAngles = FALSE,
returnIndices = TRUE,
...
)
Arguments
landscape |
Raster on which the circles are built. |
coords |
Either a matrix with 2 (or 3) columns, |
loci |
Numeric. An alternative to |
maxRadius |
Numeric vector of length 1 or same length as |
minRadius |
Numeric vector of length 1 or same length as |
allowOverlap |
Logical. Should duplicates across id be removed or kept. Default TRUE. |
stopRule |
A function. If the spokes are to stop. This can be a function
of |
includeBehavior |
Character string. Currently accepts only |
returnDistances |
Logical. If |
angles |
Numeric. Optional vector of angles, in radians, to use. This will create
"spokes" outward from |
nAngles |
Numeric, length one. Alternative to angles. If provided, the function
will create a sequence of angles from |
returnAngles |
Logical. If |
returnIndices |
Logical or numeric. If |
... |
Objects to be used by |
Value
A matrix containing columns id (representing the row numbers of coords
),
angles (from coords
to each point along the spokes), x and y coordinates
of each point along the spokes, the corresponding indices on the landscape
Raster, dists (the distances between each coords
and each point along the
spokes), and stop, indicating if it was a point that caused a spoke to stop
going outwards due to stopRule
.
Author(s)
Eliot McIntire
Examples
library(data.table)
library(terra)
origDTThreads <- data.table::setDTthreads(2L)
origNcpus <- options(Ncpus = 2L)
set.seed(1234)
ras <- terra::rast(terra::ext(0, 10, 0, 10), res = 1, val = 0)
rp <- randomPolygons(ras, numTypes = 10)
if (interactive())
terra::plot(rp)
angles <- seq(0, pi * 2, length.out = 17)
angles <- angles[-length(angles)]
n <- 2
loci <- sample(terra::ncell(rp), n)
coords <- terra::vect(terra::xyFromCell(rp, loci))
stopRule <- function(landscape) landscape < 3
d2 <- spokes(rp, coords = coords, stopRule = stopRule,
minRadius = 0, maxRadius = 50,
returnAngles = TRUE, returnDistances = TRUE,
allowOverlap = TRUE, angles = angles, returnIndices = TRUE)
# Assign values to the "patches" that were in the viewshed of a ray
rasB <- terra::rast(ras)
rasB[] <- 0
rasB[d2[d2[, "stop"] == 1, "indices"]] <- 1
if (interactive()) {
rasB[rasB == 0] <- NA
terra::plot(rasB, add = TRUE, col = "red", legend = FALSE)
}
if (NROW(d2) > 0) {
sp1 <- terra::vect(d2[, c("x", "y")])
if (interactive())
terra::plot(sp1, add = TRUE, pch = 19)
}
if (interactive())
terra::plot(coords, add = TRUE, pch = 19, col = "blue")
# clean up
data.table::setDTthreads(origDTThreads)
options(Ncpus = origNcpus)