Find 4-Neighbour (Rook) Adjacency Matrix for a Spatial Grid
Source:R/find_neighbords.R
find_neighbors.RdBuilds a sparse adjacency matrix \(A\) (patches x patches) where \(A[p, q] = 1\) if patches \(p\) and \(q\) share an edge (rook adjacency), and 0 otherwise. An optional water mask restricts edges so that land patches are excluded from the adjacency structure.
Value
A symmetric sparse matrix (class "dgCMatrix") of dimensions
\(P \times P\) (where \(P = X \times Y\)) with dimnames
"1", "2", ..., "P".
Details
Patch indexing follows the marlin convention:
$$p = (x - 1) \times Y + y \quad \text{(y varies fastest)}$$
which matches the ordering produced by
tidyr::expand_grid(x = 1:X, y = 1:Y) |> mutate(patch = 1:n()).
Used internally during movement-matrix construction in
simmar to identify which patches are adjacent when
updating habitat taxis.