My goal is to create a 3 dimensional polyhedron from a data frame of x, y, and z coordinates. I would like values similar to a SpatialPolygons
object, but other suggestions are welcome. I would then like to extend the gBuffer
function to 3D polyhedrons. gBuffer
perseveres the distance from the original geometry to the new geometry and is a desirable feature for my end goal.
The code below illustrates the case in 2D:
library(sp)
library(rgeos)
#Create a SpatialPolygons object from set of x-y coordinates
xy2SP <- function(xy, ID=NULL) {
if(is.null(ID)) ID <- sample(1e12, size=1)
SpatialPolygons(list(Polygons(list(Polygon(xy)), ID=ID)),
proj4string=CRS("+proj=merc"))
}
xy <- data.frame(x=c(0,2,3,1,0), y=c(0,0,2,2,0))
plot(xy)
sp1 <- xy2SP(xy)
sp2 <- gBuffer(sp1, width=.2)
sp3 <- gBuffer(sp1, width=.4)
sp4 <- gBuffer(sp1, width=.6)
plot(sp1,xlim=c(-1,4),ylim=c(-1,3))
plot(sp2,add=T,border=2)
plot(sp3,add=T,border=2)
plot(sp4,add=T,border=2)
My first attempt is to use my function xy2SP
with 3 coordinates instead of 2 but quickly become thwarted. In any case, there is an example of a data frame that represents the vertices of a 3D polyhedron.
library(scatterplot3d)
xyz <- data.frame(x=c(0,2,3,1,0,2,3,1),y=c(0,0,2,2,0,0,2,2),z=c(0,0,0,0,2,2,2,2))
sp<-scatterplot3d(xyz,pch=16)
sp1 <- xy2SP(xyz)