Skip to content
Prev 720 / 29559 Next

Clarification on Neighbours list

On Thu, 5 Jan 2006, Ronnie Babigumira wrote:

            
At an enjoyable workshop south of Leipzig in November, this came up. If 
you have a SpatialGridDataFrame from a raster, with NA where there are no 
values:

x <- read.asciigrid(system.file("external/test.ag", package="sp")[1])
summary(x)
xx <- as(x, "SpatialPixelsDataFrame")
summary(xx)
slot(xx, "grid")
# find grid spacing, 40 distance units for rook neighbours
# for queen, max distance about 57 should work
d40_nb <- dnearneigh(coordinates(xx), 0, 40)
d40_nb
# convert to polygons
xxsp <- as.SpatialPolygons.SpatialPixels(xx)
# generate rook neighbours again - takes longer
rook_nb <- poly2nb(xxsp, queen=FALSE)
rook_nb
# compare, alike apart from different IDs
diffnb(rook_nb, d40_nb)

Hope this helps,

Roger