Reputation: 523
I am trying to create a rooks case adjacency matrix using raster::adjacent(), but I am having trouble understanding the output.
Reproducible Example:
library(raster)
r <- raster(nrow = 3, ncol = 3)
rAdjacent <- raster::adjacent(r, cells = 1:ncell(r), pairs = TRUE, directions = 4)
rAdjacent
from to
[1,] 1 3
[2,] 2 1
[3,] 3 2
[4,] 4 6
...
From my interpretation of the output, the output says that 1 and 3 have a rooks case relationship (and if I understand this type of relationship correctly, they do not).
Question 1. Is this correct? Am I interpreting this output correctly?
Question 2. How do I create an output using adjacent() or something else that gives me pairs of adjacent cell numbers?
Thanks :)
Upvotes: 1
Views: 289
Reputation: 4940
You need to project your raster.
The function connects the outer meridians if the raster is not projeced (in a geographic (lat/lon) "projection") and there is data at longitudes -180 and 180 degrees. (from here).
r <- raster(nrow = 3, ncol = 3)
crs(r) <- CRS("+proj=robin +datum=WGS84")
rAdjacent <- raster::adjacent(r, cells = 1:ncell(r),
pairs = TRUE, directions = 4)
rAdjacent[order(rAdjacent[,1]),]
from to [1,] 1 2 [2,] 1 4 [3,] 2 1 [4,] 2 3 [5,] 2 5 [6,] 3 2 [7,] 3 6 [8,] 4 5 [9,] 4 1 [10,] 4 7 [11,] 5 4 [12,] 5 6 [13,] 5 2 [14,] 5 8 [15,] 6 5 [16,] 6 3 [17,] 6 9 [18,] 7 8 [19,] 7 4 [20,] 8 7 [21,] 8 9 [22,] 8 5 [23,] 9 8 [24,] 9 6
Upvotes: 5