The use of this function is deprecated and it is not being maintained. Use rgdal::readOGR() or sf::st_read() instead - both of these read the coordinate reference system from the input file, while this deprecated function does not. For writing, use rgdal::writeOGR() or sf::st_write() instead.

The readShapeSpatial reads data from a shapefile into a Spatial*DataFrame object. The writeSpatialShape function writes data from a Spatial*DataFrame object to a shapefile. Note DBF file restrictions in write.dbf.

readShapeSpatial(fn, proj4string=CRS(as.character(NA)),
        verbose=FALSE, repair=FALSE, IDvar=NULL, force_ring=FALSE,
        delete_null_obj=FALSE, retrieve_ABS_null=FALSE)
writeSpatialShape(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

proj4string

Object of class CRS; holding a valid proj4 string

verbose

default FALSE - 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 internal values, permitting such files to be read.

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 (Polygons only)

force_ring

if TRUE, close unclosed input rings (Polygons only)

delete_null_obj

if TRUE, null geometries inserted by ABS will be removed together with their data.frame rows (Polygons and Lines)

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 (Polygons only)

x

a vector data Spatial*DataFrame 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

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.

Value

a Spatial*DataFrame object of a class corresponding to the input shapefile

See also

Examples

library(maptools) xx <- readShapeSpatial(system.file("shapes/sids.shp", package="maptools")[1], IDvar="FIPSNO", proj4string=CRS("+proj=longlat +ellps=clrk66"))
#> Warning: readShapeSpatial is deprecated; use rgdal::readOGR or sf::st_read
#> Warning: readShapePoly is deprecated; use rgdal::readOGR or sf::st_read
#> Object of class SpatialPolygonsDataFrame #> Coordinates: #> min max #> x -84.32385 -75.45698 #> y 33.88199 36.58965 #> Is projected: FALSE #> proj4string : [+proj=longlat +ellps=clrk66] #> Data attributes: #> AREA PERIMETER CNTY_ CNTY_ID NAME #> Min. :0.0420 Min. :0.999 Min. :1825 Min. :1825 Alamance : 1 #> 1st Qu.:0.0910 1st Qu.:1.324 1st Qu.:1902 1st Qu.:1902 Alexander: 1 #> Median :0.1205 Median :1.609 Median :1982 Median :1982 Alleghany: 1 #> Mean :0.1263 Mean :1.673 Mean :1986 Mean :1986 Anson : 1 #> 3rd Qu.:0.1542 3rd Qu.:1.859 3rd Qu.:2067 3rd Qu.:2067 Ashe : 1 #> Max. :0.2410 Max. :3.640 Max. :2241 Max. :2241 Avery : 1 #> (Other) :94 #> FIPS FIPSNO CRESS_ID BIR74 SID74 #> 37001 : 1 Min. :37001 Min. : 1.00 Min. : 248 Min. : 0.00 #> 37003 : 1 1st Qu.:37050 1st Qu.: 25.75 1st Qu.: 1077 1st Qu.: 2.00 #> 37005 : 1 Median :37100 Median : 50.50 Median : 2180 Median : 4.00 #> 37007 : 1 Mean :37100 Mean : 50.50 Mean : 3300 Mean : 6.67 #> 37009 : 1 3rd Qu.:37150 3rd Qu.: 75.25 3rd Qu.: 3936 3rd Qu.: 8.25 #> 37011 : 1 Max. :37199 Max. :100.00 Max. :21588 Max. :44.00 #> (Other):94 #> NWBIR74 BIR79 SID79 NWBIR79 #> Min. : 1.0 Min. : 319 Min. : 0.00 Min. : 3.0 #> 1st Qu.: 190.0 1st Qu.: 1336 1st Qu.: 2.00 1st Qu.: 250.5 #> Median : 697.5 Median : 2636 Median : 5.00 Median : 874.5 #> Mean :1050.8 Mean : 4224 Mean : 8.36 Mean : 1352.8 #> 3rd Qu.:1168.5 3rd Qu.: 4889 3rd Qu.:10.25 3rd Qu.: 1406.8 #> Max. :8027.0 Max. :30757 Max. :57.00 Max. :11631.0 #>
xxx <- xx[xx$SID74 < 2,] tmpfl <- paste(tempdir(), "xxpoly", sep="/") writeSpatialShape(xxx, tmpfl)
#> Warning: writeSpatialShape is deprecated; use rgdal::writeOGR or sf::st_write
#> Warning: writePolyShape is deprecated; use rgdal::writeOGR or sf::st_write
getinfo.shape(paste(tmpfl, ".shp", sep=""))
#> Shapefile type: Polygon, (5), # of Shapes: 24
unlink(paste(tmpfl, ".*", sep="")) xx <- readShapeSpatial(system.file("shapes/fylk-val.shp", package="maptools")[1], proj4string=CRS("+proj=utm +zone=33 +datum=WGS84"))
#> Warning: readShapeSpatial is deprecated; use rgdal::readOGR or sf::st_read
#> Warning: readShapeLines is deprecated; use rgdal::readOGR or sf::st_read
#> Object of class SpatialLinesDataFrame #> Coordinates: #> min max #> x -4867.832 1084722 #> y 6456207.000 7841997 #> Is projected: TRUE #> proj4string : #> [+proj=utm +zone=33 +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0] #> Data attributes: #> FNODE_ TNODE_ LPOLY_ RPOLY_ #> Min. : 1.00 Min. : 2.00 Min. :1.00 Min. :1.00 #> 1st Qu.: 40.00 1st Qu.: 39.00 1st Qu.:2.00 1st Qu.:2.00 #> Median : 74.00 Median : 77.00 Median :2.00 Median :2.00 #> Mean : 77.62 Mean : 77.55 Mean :1.99 Mean :1.99 #> 3rd Qu.:115.00 3rd Qu.:114.00 3rd Qu.:2.00 3rd Qu.:2.00 #> Max. :160.00 Max. :159.00 Max. :2.00 Max. :2.00 #> LENGTH VALINJE_ VALINJE_ID LTEMA VANNBR #> Min. : 106.4 Min. : 1 Min. : 55.00 Min. :3211 Min. :13 #> 1st Qu.: 11866.5 1st Qu.:25 1st Qu.: 74.00 1st Qu.:3211 1st Qu.:13 #> Median : 31910.9 Median :49 Median : 95.00 Median :3211 Median :13 #> Mean : 41374.7 Mean :49 Mean : 94.32 Mean :3211 Mean :13 #> 3rd Qu.: 59311.8 3rd Qu.:73 3rd Qu.:114.00 3rd Qu.:3211 3rd Qu.:13 #> Max. :176540.1 Max. :97 Max. :135.00 Max. :3211 Max. :13 #> DATO #> Min. :19970630 #> 1st Qu.:19970630 #> Median :19970630 #> Mean :19970630 #> 3rd Qu.:19970630 #> Max. :19970630
xxx <- xx[xx$LENGTH > 30000,] plot(xxx, col="red", add=TRUE)
#> Error in plot.xy(xy.coords(x, y), type = type, ...): plot.new has not been called yet
tmpfl <- paste(tempdir(), "xxline", sep="/") writeSpatialShape(xxx, tmpfl)
#> Warning: writeSpatialShape is deprecated; use rgdal::writeOGR or sf::st_write
#> Warning: writeLinesShape is deprecated; use rgdal::writeOGR or sf::st_write
getinfo.shape(paste(tmpfl, ".shp", sep=""))
#> Shapefile type: PolyLine, (3), # of Shapes: 51
unlink(paste(tmpfl, ".*", sep="")) xx <- readShapeSpatial(system.file("shapes/baltim.shp", package="maptools")[1])
#> Warning: readShapeSpatial is deprecated; use rgdal::readOGR or sf::st_read
#> Warning: readShapePoints is deprecated; use rgdal::readOGR or sf::st_read
#> Object of class SpatialPointsDataFrame #> Coordinates: #> min max #> coords.x1 860.0 987.5 #> coords.x2 505.5 581.0 #> Is projected: NA #> proj4string : [NA] #> Number of points: 211 #> Data attributes: #> STATION PRICE NROOM DWELL #> Min. : 1.0 Min. : 3.50 Min. : 3.000 Min. :0.0000 #> 1st Qu.: 53.5 1st Qu.: 30.95 1st Qu.: 5.000 1st Qu.:0.0000 #> Median :106.0 Median : 40.00 Median : 5.000 Median :1.0000 #> Mean :106.0 Mean : 44.31 Mean : 5.199 Mean :0.5355 #> 3rd Qu.:158.5 3rd Qu.: 53.75 3rd Qu.: 6.000 3rd Qu.:1.0000 #> Max. :211.0 Max. :165.00 Max. :10.000 Max. :1.0000 #> NBATH PATIO FIREPL AC #> Min. :1.000 Min. :0.0000 Min. :0.0000 Min. :0.0000 #> 1st Qu.:1.000 1st Qu.:0.0000 1st Qu.:0.0000 1st Qu.:0.0000 #> Median :1.500 Median :0.0000 Median :0.0000 Median :0.0000 #> Mean :1.573 Mean :0.1469 Mean :0.2417 Mean :0.2417 #> 3rd Qu.:2.000 3rd Qu.:0.0000 3rd Qu.:0.0000 3rd Qu.:0.0000 #> Max. :5.000 Max. :1.0000 Max. :1.0000 Max. :1.0000 #> BMENT NSTOR GAR AGE #> Min. :0.000 Min. :1.000 Min. :0.0000 Min. : 0.0 #> 1st Qu.:2.000 1st Qu.:2.000 1st Qu.:0.0000 1st Qu.: 20.0 #> Median :2.000 Median :2.000 Median :0.0000 Median : 25.0 #> Mean :1.981 Mean :1.905 Mean :0.2512 Mean : 30.1 #> 3rd Qu.:3.000 3rd Qu.:2.000 3rd Qu.:0.0000 3rd Qu.: 40.0 #> Max. :3.000 Max. :3.000 Max. :3.0000 Max. :148.0 #> CITCOU LOTSZ SQFT X #> Min. :0.0000 Min. : 5.70 Min. : 5.76 Min. :860.0 #> 1st Qu.:0.0000 1st Qu.: 20.76 1st Qu.:11.02 1st Qu.:889.0 #> Median :1.0000 Median : 56.25 Median :13.44 Median :910.0 #> Mean :0.6066 Mean : 72.28 Mean :16.43 Mean :911.6 #> 3rd Qu.:1.0000 3rd Qu.: 84.32 3rd Qu.:19.94 3rd Qu.:933.5 #> Max. :1.0000 Max. :400.37 Max. :47.61 Max. :987.5 #> Y #> Min. :505.5 #> 1st Qu.:528.8 #> Median :544.5 #> Mean :544.2 #> 3rd Qu.:559.0 #> Max. :581.0
xxx <- xx[xx$PRICE < 40,] tmpfl <- paste(tempdir(), "xxpts", sep="/") writeSpatialShape(xxx, tmpfl)
#> Warning: writeSpatialShape is deprecated; use rgdal::writeOGR or sf::st_write
#> Warning: writePointsShape is deprecated; use rgdal::writeOGR or sf::st_write
getinfo.shape(paste(tmpfl, ".shp", sep=""))
#> Shapefile type: Point, (1), # of Shapes: 103
unlink(paste(tmpfl, ".*", sep=""))