Skip to content
Prev 10948 / 29559 Next

convert a PBSmapping grid to shapefile

On Thu, 17 Feb 2011, Armelle ROUYER wrote:

            
library(PBSmapping)
library(maptools)
gt <- GridTopology(c(0.5, 0.5), c(1, 1), c(5, 10))
sp <- as(gt, "SpatialPolygons")
proj4string(sp) <- CRS("+proj=longlat")
PS0 <- SpatialPolygons2PolySet(sp)
PD0 <- as.PolyData(data.frame(PID=1:50, col=rep(rainbow(10), each=5),
   stringsAsFactors=FALSE))
plotPolys(PS0, polyProps=PD0, projection="LL")
# the above to set up a PolySet opject, PolyData object, and plot them
sp1 <- PolySet2SpatialPolygons(PS0)
mo <- match(as.character(PD0$PID), row.names(sp1))
# check mo - this is an easy match, in practice it may need more study
sp2 <- SpatialPolygonsDataFrame(sp1, data=data.frame(col=PD0$col[mo],
   stringsAsFactors=FALSE))
plot(sp2, col=sp2$col, axes=TRUE)

seems to work. You match on the PolyData PID column and the row.names() of 
the SpatialPolygons object, which are the unique PolySet PID values. In 
this case, and probably in general, the PIDs in the PolyData and PolySet 
match by design. If there isn't an exact match, you'll need to construct 
an object of the correct length manually.

Roger