Create a matrix showing which planning units are within a certain spatial proximity to each other.
proximity_matrix(x, distance)
# S3 method for Raster
proximity_matrix(x, distance)
# S3 method for SpatialPolygons
proximity_matrix(x, distance)
# S3 method for SpatialLines
proximity_matrix(x, distance)
# S3 method for SpatialPoints
proximity_matrix(x, distance)
# S3 method for sf
proximity_matrix(x, distance)
# S3 method for default
proximity_matrix(x, distance)
dsCMatrix
symmetric sparse matrix object.
Each row and column represents a planning unit.
Cells values indicate if the pair-wise distances between different
planning units are within the distance threshold or not (using ones and
zeros). To reduce computational burden, cells among the matrix diagonal are
set to zero. Furthermore, if the argument to x
is a
Raster
object, then cells with NA
values are set to zero too.
Proximity calculations are performed using
sf::st_is_within_distance()
.
# load data
data(sim_pu_raster, sim_pu_sf, sim_pu_lines, sim_pu_points)
# create proximity matrix using raster data
## crop raster to 9 cells to provide a small example
r <- crop(sim_pu_raster, c(0, 0.3, 0, 0.3))
## make proximity matrix using a distance threshold of 2
cm_raster <- proximity_matrix(r, distance = 2)
# create proximity matrix using polygon (sf) data
## subset 9 polygons to provide a small example
ply <- sim_pu_sf[c(1:2, 10:12, 20:22), ]
## make proximity matrix using a distance threshold of 2
cm_ply <- proximity_matrix(ply, distance = 2)
# create proximity matrix using line (Spatial) data
## subset 9 lines to provide a small example
lns <- sim_pu_lines[c(1:2, 10:12, 20:22), ]
## make proximity matrix
cm_lns <- proximity_matrix(lns, distance = 2)
## create proximity matrix using point (Spatial) data
## subset 9 points to provide a small example
pts <- sim_pu_points[c(1:2, 10:12, 20:22), ]
# make proximity matrix
cm_pts <- proximity_matrix(pts, distance = 2)
# plot data and the proximity matrices
# \dontrun{
par(mfrow = c(4,2))
## plot raster and proximity matrix
plot(r, main = "raster", axes = FALSE, box = FALSE)
plot(raster(as.matrix(cm_raster)), main = "proximity matrix", axes = FALSE,
box = FALSE)
## plot polygons and proximity matrix
plot(r, main = "polygons (sf)", axes = FALSE, box = FALSE)
plot(raster(as.matrix(cm_ply)), main = "proximity matrix", axes = FALSE,
box = FALSE)
## plot lines and proximity matrix
plot(r, main = "lines (Spatial)", axes = FALSE, box = FALSE)
plot(raster(as.matrix(cm_lns)), main = "proximity matrix", axes = FALSE,
box = FALSE)
## plot points and proximity matrix
plot(r, main = "points (Spatial)", axes = FALSE, box = FALSE)
plot(raster(as.matrix(cm_pts)), main = "proximity matrix", axes = FALSE,
box = FALSE)
# }