directions: Calculate distances and directions between many points and...

Description Usage Arguments Details Value Author(s) See Also Examples

Description

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.

Usage

1
2
3
directionFromEachPoint(from, to = NULL, landscape)

.pointDirection(from, to)

Arguments

from

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.

to

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.

landscape

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

Details

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.

Value

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.

Author(s)

Eliot McIntire

See Also

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

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
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)
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")
}

SpaDES.tools documentation built on June 19, 2018, 1:04 a.m.