Hi,

I would like to align two polygon grids, one with 100x100 cells and the other 
with 50x50 cells.  The idea is that they would align so that 4 50x50 cells 
would fall within one 100x100 cell.  Preferably, they would have a common 
coordinate origin so that I could also make a grid of 200x200 cells and they 
would also align properly.  It seems to be something to do with where the 
centroids of each grid cell is placed?  Below is a reproducible example of 
where I am at.  I'm new with the sp package so sorry for the length of the 
script!  I'm using R 9.1 with XP.  Thanks!

Code:

### Range of coordinate space
x.range = c(1924835, 1926426)
y.range = c(484673, 485490)

### Create 100x100 grid
cell.size = 100
grd100 <- expand.grid(x=seq(from=x.range[1], to=x.range[2], by=cell.size),
     y=seq(from=y.range[1], to=y.range[2], by=cell.size))
coordinates(grd100) <- ~ x+y
gridded(grd100) <- TRUE
grd100.SP <- as.SpatialPolygons.SpatialPixels(grd100)
centroids <- coordinates(grd100.SP)
x <- centroids[,1]
y <- centroids[,2]
row.names100 <- sapply(slot(grd100.SP, "polygons"), function(i) slot(i, "ID"))
grd100.SPDF <- SpatialPolygonsDataFrame(grd100.SP,
     data=data.frame(x=x, y=y, row.names=row.names100))


### Create 50x50 grid
cell.size = 50
grd50 <- expand.grid(x=seq(from=x.range[1], to=x.range[2], by=cell.size),
     y=seq(from=y.range[1], to=y.range[2], by=cell.size))
coordinates(grd50) <- ~ x+y
gridded(grd50) <- TRUE
grd50.SP<-as.SpatialPolygons.SpatialPixels(grd50)
centroids <- coordinates(grd50.SP)
x <- centroids[,1]
y <- centroids[,2]
row.names50 <- sapply(slot(grd50.SP, "polygons"), function(i) slot(i, "ID"))
grd50.SPDF <- SpatialPolygonsDataFrame(grd50.SP,
     data=data.frame(x=x, y=y, row.names=row.names50))


### Plot both grids
s100 <- list("sp.lines", as(grd100.SPDF, "SpatialLinesDataFrame"), "magenta",
    lwd=2)
spplot(as(grd50.SPDF, "SpatialLinesDataFrame"), "x", col.regions="black",
    sp.layout=list(s100),colorkey = F) 

_________________________________________________________________


_sync:082009
        [[alternative HTML version deleted]]

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

Reply via email to