[R-sig-Geo] package for writing shapefiles from SpatialPolygons?

Roger Bivand Roger.Bivand at nhh.no
Thu Aug 31 08:56:26 CEST 2006


On Thu, 31 Aug 2006, Michael Sumner wrote:

> 
> > It is included in the maptools package now - the writePolyShape() function
> > is the one you are looking for. If you also need to write a *.prj, use
> >   
> 
> Ah, thanks.  But how do I convert an "SpatialPolygons" object, or even 
> the @polygons list to a "polylist"?
> 
> Sorry . . . an example?

What is a shapefile? It has a *.shp, a *.shx, _and_ a *.dbf at least. So 
you can't go from a SpatialPolygons object to a shapefile, it has to be 
SpatialPolygonsDataFrame, even if the only column in the data frame is 
just a holder. So create a data frame (with row names equal to the 
Polygons ID values), make a SpatialPolygonsDataFrame, and 
writePolyShape().

library(maptools)
xx <- readShapePoly(system.file("shapes/sids.shp", package="maptools")[1], 
  IDvar="FIPSNO", proj4string=CRS("+proj=longlat +ellps=clrk66"))
SPxx <- as(xx, "SpatialPolygons")
class(SPxx)
IDs <- sapply(slot(SPxx, "polygons"), function(x) slot(x, "ID"))
df <- data.frame(rep(0, length(IDs)), row.names=IDs)
SPDFxx <- SpatialPolygonsDataFrame(SPxx, df)
class(SPDFxx)
summary(SPDFxx)
tf <- tempfile()
writePolyShape(SPDFxx, tf)
getinfo.shape(tf)

The conversion stuff is probably irrelevant, it is internal and subject to 
change. It's in the source if you do need it. 

Roger

> 
> Cheers, Mike.
> 
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at stat.math.ethz.ch
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
> 

-- 
Roger Bivand
Economic Geography Section, Department of Economics, Norwegian School of
Economics and Business Administration, Helleveien 30, N-5045 Bergen,
Norway. voice: +47 55 95 93 55; fax +47 55 95 95 43
e-mail: Roger.Bivand at nhh.no




More information about the R-sig-Geo mailing list