[R-sig-Geo] Clarification on Neighbours list

Roger Bivand Roger.Bivand at nhh.no
Thu Jan 5 16:33:32 CET 2006


On Thu, 5 Jan 2006, Ronnie Babigumira wrote:

> Someone has pointed out that my question was not clear. With a clear danger of sounding really stupid, I will try to clarify
> 
> Say I have a polygon, which I convert to raster format so each cell is n by n. For some reason I select a set of cells 
> and have a neat rectangle such as below. To get a neighbor list for such a grid I could use cell2nb(3, 8) would be fine
> 
> 1 1 1 0 0 1 0 1
> 0 1 1 0 1 1 1 0
> 1 1 0 0 1 1 1 1
> 
> However say my grid came from the full map and looked something like this
>    0 1 0 0 1
> 0 1 1 0 1 1 1
>      0 0 1 1 1
>        0 0 1 0 1
>    1 1 0 1 1 1
>    1 0 0 1
> 1 1 1 0 0
> 0 1 1
> 
> The number of rows is clear, however, what is the number of columns (and anyway, what R function do you use to get the 
> dimensions of a SpatialGridDataFrame object.
> 

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

> 
> Ronnie
> 
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at stat.math.ethz.ch
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
> 

-- 
Roger Bivand
Economic Geography Section, Department of Economics, Norwegian School of
Economics and Business Administration, Helleveien 30, N-5045 Bergen,
Norway. voice: +47 55 95 93 55; fax +47 55 95 95 43
e-mail: Roger.Bivand at nhh.no




More information about the R-sig-Geo mailing list