This is a modification of 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.

Internal function.

directionFromEachPoint(from, to = NULL, landscape)

.pointDirection(from, to)



matrix with 2 or 3 columns, x and y, representing x and y coordinates of from cell, and optional id, which will be returned, and if id column is in to, it will be matched with that.


matrix with 2 or 3 columns (or optionally more, all of which will be returned), x and y, representing x and y coordinates of to cells, and optional id which will be matched with id from from. It makes no sense to have id column here with no id column in from.


(optional) RasterLayer. This is only used if to = NULL, in which case all cells are considered to.


A sorted matrix on 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.

See also

distanceFromEachPoint, which will also return directions if angles = TRUE.


library(raster) library(quickPlot) 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) library(CircStats)
#> Loading required package: MASS
#> #> Attaching package: ‘MASS’
#> The following objects are masked from ‘package:raster’: #> #> area, select
#> Loading required package: boot
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()) { clearPlot() Plot(dirRas) library(sp) start <- SpatialPoints(coords[, c("x", "y"), drop = FALSE]) Plot(start, addTo = "dirRas") }