There are converter functions for Spatial objects in the spatstat and maptools packages that can be used for this. A shape file consists of at least points (or lines or polygons) and attributes for each object.
library(spatstat) library(sp) library(maptools) data(bei)
Binding bei to a Spatial object is simply indicated here without attributes, since there are no "labels" on the ppp object.
spPoints <- as(bei, "SpatialPoints")
A form file requires at least one attribute data column, so create a dummy file.
dummyData <- data.frame(dummy = rep(0, npoints(bei)))
Using a SpatialPoints object and dummy data, generate a SpatialPointsDataFrame .
spDF <- SpatialPointsDataFrame(spPoints, dummyData)
At this point, you should definitely consider what the coordinate system used by bei , and whether you can imagine it using WKT CRS (the well-known coordinate system for text coordinates). You can assign this to a Spatial object as another SpatialPointsDataFrame argument SpatialPointsDataFrame or after creating with proj4string(spDF) <- CRS("+proj=etc...") (but this is a whole problem in itself so that we can write pages).
Download the rgdal package (this is the most common option, as it supports many formats and uses the GDAL library, but may not be available due to system dependencies.
library(rgdal)
(Use writePolyShape in the maptools package if rgdal not available).
The syntax is an object, then โdata source nameโ (here is the current directory, this can be the full path to a .shp or folder), then a layer (for shape files, the file name without extension), and then the name of the output driver.
writeOGR(obj = spDF, dsn = ".", layer = "bei", driver = "ESRI Shapefile")
Note that recording will fail if "bei.shp" already exists and therefore unlink("bei.shp") must first be deleted.
List all the files starting with "bei":
list.files(pattern = "^bei") [1] "bei.dbf" "bei.shp" "bei.shx"
Please note that for ppp objects there is no common "as.Spatial" converter, as decisions must be made as to whether this is a dot with labels, etc. - it may be interesting to try to write one that reports on whether dummy data is needed, etc.
For more information and details on the differences between these data views, see the following vignettes:
Library (SP); vignette ("IP") library (spatstat); vignette ("spatstat")