shapefiles (version 0.7)

shapefiles: Read and write ESRI shapefiles


This package includes functions to read and write ESRI shapefiles.


read.shapefile( read.shp( read.shx( read.dbf(, header=FALSE) write.shapefile(shapefile,, arcgis=FALSE) write.shp(shp, write.shx(shx, write.dbf(dbf,, arcgis=FALSE) calc.header(shapefile) add.xy(shapefile) scaleXY(shapefile, scale.factor), attTable, field, type), newFieldAsVector) dp(points, tolerance)

String of the shapefile file name without an extension
String of the shp file name with an extension
String of the shx file name with an extension
String of the dbf file name with an extension
The shapefile object of lists created by read.shapefile
Filename to write the data to
shp portion (list) of the shapefile object of lists
shx portion (list) of the shapefile object of lists
dbf portion (list) of the shapefile object of lists
Number to divide the shapefile geography by
Replace "." with "\_" in column names for ArcGIS
data.frame with columns in order Id, X, and Y
data.frame with first column names "Id" - polygon id (key)
ESRI Shape type 1=point, 3=polyLine, 5=polygon
A field name in the attTable
A vector of Ids to replace to the Ids in the shpTable
A named list of two vectors (x and y) representing points
A tolerance setting for the DP polyLine simplification algorithm
Should read.dbf return the header?


read.shapefile list
shapefile list object read.shp
list shp list object
read.shx list
shx list object read.dbf
list DBF list object
write.shapefile NA
Nothing returned write.shp
NA Nothing returned
write.shx NA
Nothing returned write.dbf
NA Nothing returned
calc.header list
shapefile list object add.xy
list shapefile list object
scaleXY list
shapefile list object
list shapefile list object list
data.frame list data.frame
list data.frame list data.frame
dp list
data.frame list data.frame read.shapefile


ESRI shapefiles consist of three files. The first file (*.shp) contains the geography of each shape. The second file (*.shx) is an index file which contains record offsets. The third file (*.dbf) contains feature attributes with one record per feature. read.shapefile calls read.shp, read.shx, and read.dbf to read in an entire shapefile. The result of read.shapefile is a list of many more lists. The sublists are shp, shx, and dbf. Each sublist contains a header list and some sort of data list. The shp list is a list of $shp$header and $shp$shp. The shx list is a list of $shx$header and $shx$index. The dbf list is a list of $dbf$header and $dbf$dbf. The write functions write out a shp, shx, and dbf file from the shapefile list structure. To write out a shapefile from simple R data, you need to run The inputs to this function are a simple data frame of points (for points, polyLines, or polygons) and a data frame representing the dbf file. Examples are below. The package reads shape types 1 (point), 3 (polyLine), 5 (polygon), 13 (polyLineZ), and 15 (polygonZ). Reading of shape type 13 and 15 from Don MacQueen, The package writes shape types 1 (point), 3 (polyLine), 5 (polygon), 13 (polyLineZ), and 15 (polygonZ). Conversion of simple polygons to shapefile format from Manuel Chirouze, For simple features, the only difference between polyLines and polygons is that the first and last point is the same for a polygon. The function can be used to simplify the shp file to a simple data.frame. The function can then be used to change the Id field for the simple shp data.frame to a field from a data.frame (dbf). For details about the ESRI shapefile structure refer to A detailed description of DBF files can be found at The arcgis argument to write.dbf replaces "." with "\_" in field names since ArcGIS does not allow the former. Note that the read.dbf and write.dbf functions in the foreign package are now used for reading and writing dbfs, which greatly improves the speed of reading/writing dbfs. Function dp is an implementation of the Douglas-Peucker polyLine simplification algorithm. Douglas, D. and Peucker, T. (1973). "Algorithms for the reduction of the number of points required to represent a digitized line or its caricature." The Canadian Cartographer 10(2). 112-122. dp currently uses the line, not the line segment to determine the distance of the points from the line. This can result in the omission of extreme "outlier-like" points. See for more information.


## Not run: 
# #Read entire shapefile
# shapefile <- read.shapefile("links")
# #Write entire shapefile
# write.shapefile(shapefile, "temp", T)
# #Read shp, shx, or dbf file
# dbf <- read.dbf("links.dbf")
# #Write shp, shx, or dbf file
# write.dbf(dbf, "links.dbf", T)
# #Calculate header (to clean up GeoMedia shapefile exports)
# shapefile <- calc.header(shapefile)
# #Add the X and Y coordinates to the dbf list of the shapefile list object
# shapefile <- add.xy(shapefile)
# #Scale the shapefile by scale.factor
# shapefile <- scaleXY(shapefile, scale.factor)
# #Samples of using the function to write out simple shapefiles
# #from basic R data.frames
# #Point
# dd <- data.frame(Id=c(1,2),X=c(3,5),Y=c(9,6))
# ddTable <- data.frame(Id=c(1,2),Name=c("Item1","Item2"))
# ddShapefile <-, ddTable, "Id", 1)
# write.shapefile(ddShapefile, "c:/test", arcgis=T)
# #PolyLine
# dd <- data.frame(Id=c(1,1,1,2,2,2),X=c(3,5,8,6,7,8),Y=c(9,8,3,6,7,4))
# ddTable <- data.frame(Id=c(1,2),Name=c("Item1","Item2"))
# ddShapefile <-, ddTable, "Id", 3)
# write.shapefile(ddShapefile, "c:/test", arcgis=T)
# #Polygon
# dd <- data.frame(Id=c(1,1,1,1,2,2,2,2),X=c(3,5,8,3,6,7,8,6),Y=c(9,8,3,9,6,7,4,6))
# ddTable <- data.frame(Id=c(1,2),Name=c("Item1","Item2"))
# ddShapefile <-, ddTable, "Id", 5)
# write.shapefile(ddShapefile, "c:/test", arcgis=T)
# #Convert to list of shapes
# ddAsList <- by(dd,dd$Id, function(x) x)
# #Convert to data.frame
# dd <-, ddAsList)
# #Read in shp file and convert to simple format
# shpTest <- read.shp("c:/test.shp")
# simpleShpFormat <-
# simpleShpFormat <-, c("a","b"))
# simpleAsList <- by(simpleShpFormat, simpleShpFormat[,1], function(x) x)
# backToShape <-, 
# 	data.frame(index=c("a","b")), "index", 5)
# write.shapefile(backToShape, "c:/test", arcgis=T)
# #Polyline simplification with dp algorithm
# x <- c(5,3,4,1,8,9,10,11)
# y <- c(6,4,2,1,1,5,2,3)
# points <- list(x=x,y=y)
# plot(points, type="l")
# simpleLine <- dp(points, 2)
# lines(simpleLine, type="l", col="blue")
# ## End(Not run)