maptools (version 0.8-10)

readShapePoly: Read polygon shape files into SpatialPolygonsDataFrame objects

Description

The readShapePoly reads data from a polygon shapefile into a SpatialPolygonsDataFrame object. The writePolyShape function writes data from a SpatialPolygonsDataFrame object to a shapefile.

Usage

readShapePoly(fn, IDvar=NULL, proj4string=CRS(as.character(NA)),
 verbose=FALSE, repair=FALSE, force_ring=FALSE, delete_null_obj=FALSE,
 retrieve_ABS_null=FALSE)
writePolyShape(x, fn, factor2char = TRUE, max_nchar=254)

Arguments

fn
shapefile layer name, when writing omitting the extensions *.shp, *.shx and *.dbf, which are added in the function
IDvar
a character string: the name of a column in the shapefile DBF containing the ID values of the shapes - the values will be converted to a character vector
proj4string
Object of class CRS; holding a valid proj4 string
verbose
default TRUE - report type of shapefile and number of shapes
repair
default FALSE: some shapefiles provided by Geolytics Inc. have values of object sizes stored in the *.shx index file that are eight bytes too large, leading the function to try to read past the end of file. If repair=TRUE, an attempt is made to repair the
force_ring
if TRUE, close unclosed input rings
delete_null_obj
if TRUE, null geometries will be removed together with their data.frame rows
retrieve_ABS_null
default FALSE, if TRUE and delete_null_obj also TRUE, the function will return a data frame containing the data from any null geometries inserted by ABS
x
a SpatialPolygonsDataFrame object
factor2char
logical, default TRUE, convert factor columns to character
max_nchar
default 254, may be set to a higher limit and passed through to the DBF writer, please see Details in write.dbf

Value

  • a SpatialPolygonsDataFrame object

Details

If no IDvar argument is given, the shpID values of the shapefile will be used as Polygons ID values; when writing shapefiles, the object data slot row.names are added to the DBF file as column SP_ID.

Examples

Run this code
library(maptools)
xx <- readShapePoly(system.file("shapes/sids.shp", package="maptools")[1],
 IDvar="FIPSNO", proj4string=CRS("+proj=longlat +ellps=clrk66"))
plot(xx, border="blue", axes=TRUE, las=1)
text(coordinates(xx), labels=row.names(xx), cex=0.6)
as(xx, "data.frame")[1:5, 1:6]
xxx <- xx[xx$SID74 < 2,]
plot(xxx, border="red", add=TRUE)
tmpfl <- paste(tempdir(), "xxpoly", sep="/")
writePolyShape(xxx, tmpfl)
getinfo.shape(paste(tmpfl, ".shp", sep=""))
axx <- readShapePoly(tmpfl, proj4string=CRS("+proj=longlat +ellps=clrk66"))
plot(xxx, border="black", lwd=4)
plot(axx, border="yellow", lwd=1, add=TRUE)
unlink(paste(tmpfl, ".*", sep=""))

Run the code above in your browser using DataCamp Workspace