Skip to content
Prev 4053 / 29559 Next

SpatialPolygons to SpatialGrid

On Sun, 17 Aug 2008, Murray Richardson wrote:

            
Did your subsequent message indicate that you had found a work-around?

If not, this reply copied from a recent posting to the statsgrass list may 
be relevant, using overlay() to assign grid cell centres to polygons:

library(sp)
library(maptools)
polyg <- readShapeSpatial(system.file("shapes/sids.shp",
  package="maptools")[1],  IDvar="FIPSNO",
  proj4string=CRS("+proj=longlat +ellps=clrk66"))
# the SpatialGrid is created here - if you already have one, just use it
grd <- Sobj_SpatialGrid(polyg)$SG
d <- overlay(grd, polyg)
# d contains the indices of the Polygons object into which each cell
# centre falls, or NA
summary(d)
names(polyg)
v <- polyg$SID74[d]
# use d to index the variable of interest
# if grd had been a SpatialGridDataFrame, just assign "[[<-" or "$<-",
# grd[["SID74"]] <- polyg[["SID74"]][d]
# or:
grdd <- SpatialGridDataFrame(grid=slot(grd, "grid"),
  data=data.frame(SID74=v), proj4string=CRS(proj4string(grd)))
summary(grdd)
image(grdd)
spplot(polyg, "SID74")

Roger