distanceFromPoints
for the case of many points.
This version can often be faster for a single point because it does not return a RasterLayer. This is
different than distanceFromPoints
because it does not take the minimum
distance from the set of points to all cells. Rather this returns the every pair-wise point distance.
As a result, this can be used for doing inverse distance weightings, seed rain, cumulative effects
of distance-based processes etc. If memory limitation is an issue, maxDistance will keep memory
use down, but with the consequences that there will be a maximum distance returned. This function
has the potential to use a lot of memory if there are a lot of from
and to
points.This is meant to be used internally.
directionFromEachPoint(from, to = NULL, landscape)
.pointDirection(from, to)
to
,
it will be matched with that.from
. It makes no sense to
have "id" column here with no "id" column in from
to
is NULL, in which case
all cells are considered to
id
with same number of rows as to
,
but with one extra column, angles
indicating the angle in radians between from and to. For speed, this
angle will be between -pi/2 and 3*pi/2. If the user wants this between
say, 0 and 2*pi, then angles %% (2*pi)
will do the trick. See example.
directionFromEachPoint
calls .pointDirection
, which is not intended to be called
directly by the user.If knowing the which from cell matches with which to cell is important,
put a column "id" (e.g., starting cell) in the from
matrix.
distanceFromEachPoint
, which will also return directions if angles
is TRUE.
library(raster)
N <- 2
dirRas <- raster(extent(0,40,0,40), res = 1)
coords <- cbind(x = round(runif(N, xmin(dirRas), xmax(dirRas)))+0.5,
y = round(runif(N, xmin(dirRas), xmax(dirRas)))+0.5,
id = 1:N)
dirs1 <- directionFromEachPoint(from = coords, landscape = dirRas)
require(CircStats)
dirs1[, "angles"] <- deg(dirs1[,"angles"] %% (2*pi))
indices <- cellFromXY(dirRas,dirs1[, c("x", "y")])
minDir <- tapply(dirs1[,"angles"], indices, function(x) min(x)) # minimum angle
dirRas[] <- as.vector(minDir)
if (interactive()) {
Plot(dirRas, new = TRUE)
library(sp)
start <- SpatialPoints(coords[, c("x", "y"), drop = FALSE])
Plot(start, addTo = "dirRas")
}
Run the code above in your browser using DataLab