[R] Getting polygons from contiguous grid cells having same value in sp

Waichler, Scott R Scott.Waichler at pnl.gov
Tue Mar 21 21:02:40 CET 2006

I have been using the sp package to deal with gridded data.  I would
like to know how to extract the vertices (x,y) of polygons that outline
areas of like-valued cells in a grid.  Here is a simple 3x3 grid:

2  2  1
1  2  1
1  1  3

x <- c(1,1,1,2,2,2,3,3,3) # define a 3 x 3 array of points
y <- c(1,2,3,1,2,3,1,2,3)
h <- SpatialPoints(cbind(x,y))  # make these an sp object of the points
h$z<- c(1,1,2,1,2,2,1,1,3) # add field values
gridded(h) <- T  # make it a grid

How could I get the vertices for the region of value=2 cells?  Here is
what they would be, in clockwise order from the upper left corner:
(0,3) (2,3) (2,1) (1,1) (1,2) (0,2)

Scott Waichler
Pacific Northwest National Laboratory
scott.waichler _at_ pnl.gov

More information about the R-help mailing list