[R-sig-Geo] Creating another grid from n*n grid

Takatsugu Kobayashi tkobayas at indiana.edu
Sat Jan 12 22:45:20 CET 2008


Hi,

(As an exercise)I am trying to create a SpatialPolygons object from n*n 
grid. By reading the material (from vignette{'sp'), I managed to convert 
grid to polygons. Now I would like plot this polygons, and here is what 
I have done so far:

Step1: Create a 9*9 grid
n<-9 # number of cells in each axis
x <- 1:n+1
y <- 1:n+1
r.grid <- makeGrid(x,y)

Step2: Convert the grid to SpatialPolygon
Srs <- list()
for (i in 1:n^2)
{
x.coord <- c(r.grid$X[(1+4*(i-1)):(1+4*(i-1)+3)],r.grid$X[(1+4*(i-1))])
y.coord <- c(r.grid$Y[(1+4*(i-1)):(1+4*(i-1)+3)],r.grid$Y[(1+4*(i-1))])
subPoly <- Polygon(cbind(x.coord, y.coord),hole=FALSE)
Srs[[i]] <- Polygons(list(subPoly), paste("SP",i,sep=''))
}

SpP <- SpatialPolygons(list(Srs),1:n^2)
Error in is.vector(X) : cannot get a slot ("Polygons") from an object of 
type "list"

I am trying to what the following message tells me. class(Srs) returns 
"list" as class and Src has an object of class "Polygons" as follows:

[[81]]
An object of class “Polygons”
Slot "Polygons":
[[1]]
An object of class “Polygon”
Slot "labpt":
[1] 9.5 9.5

Slot "area":
[1] 1

Slot "hole":
[1] FALSE

Slot "ringDir":
[1] 1

Slot "coords":
x.coord y.coord
[1,] 9 9
[2,] 9 10
[3,] 10 10
[4,] 10 9
[5,] 9 9

Slot "plotOrder":
[1] 1

Slot "labpt":
[1] 9.5 9.5

Slot "ID":
[1] "SP81"

Slot "area":
[1] 1

I am sure I am doing fundamental mistakes here. As I am thinking to 
increase n later, I wouldn't want to type Srs[[1]..... by hand.

Also, I am going to convert this polygons into shapefile to get an 
polygon-adjacency matrix through GeoDA. I have reading a few threads 
about a polygon adjacency matrix in R, but it seems GeoDA is a way to 
go? For my case, I would assign 1 if 2 polygons share multiple 
coordinates and store neighbors in list if this is a good way to do in R.

Sorry for this very fundamental question, but I appreciate help.

Thank you.

Taka




More information about the R-sig-Geo mailing list