How do I get SpatialPolygons (SP class) from a set of segments (psp in spartstat)?

I have a set of random line segments drawing a tessellation view (triangles, rectangles ...) in a window (in ballstat R). I need to convert it to a set of polygons (SpatialPolygons) in order to calculate some indices (e.g. area, shape indices ...).

This seems to be simple, but I couldn't find how to do it ...

Here is some code from Carl Witthoft that generates a random pattern of self- intercepting segments:

ranpoly <- function(numsegs=10,plotit=TRUE) {

require(spatstat)
# temp fix: put the first seg into segset. Later make it a constrained random.
segset<-psp(c(0,1,1,0,.25),c(0,0,1,1,0),c(1,1,0,0,1),c(0,1,1,0,.75),owin(c(0,1),c(0,1)) ) #frame the frame
for (jj in 1: numsegs) {
# randomly select a segment to start from, a point on the seg, the slope,and direction
# later... watch for slopes that immediately exit the frame
endx <-sample(c(-0.2,1.2),1)  #force 'x1' outside the frame
# watch that sample() gotcha
if(segset$n<=5) sampset <- c(5,5) else sampset<-5:segset$n
startseg<-sample(sampset,1) #don't select a frame segment
# this is slope of segment to be constructed
slope <- tan(runif(1)*2*pi-pi) # range +/- Inf 
# get length of selected segment
seglen<-lengths.psp(segset)[startseg]
startcut <- runif(1) 
# grab the coords of starting point (similar triangles)
startx<- segset$ends$x0[startseg] + (segset$ends$x1[startseg]-segset$ends$x0[startseg])*startcut #seglen
starty<- segset$ends$y0[startseg] + (segset$ends$y1[startseg]-segset$ends$y0[startseg])*startcut #seglen
# make a psp object with that startpoint and slope; will adjust it after finding intersections
endy <- starty + slope*(endx-startx)
newpsp<-psp(startx,starty,endx,endy,segset$window,check=FALSE)
# don't calc crossing for current element of segset
hits <- crossing.psp(segset[-startseg],newpsp)
segdist <- dist(cbind(c(startx,hits$x),c(starty,hits$y)))
# dig back to get the crosspoint desired -- have to get matrixlike object out of class "dist" object
# And, as.matrix puts a zero in location 1,1 kill that row.
cutx <- hits$x[ which.min( as.matrix(segdist)[-1,1] )]
cuty <- hits$y[which.min(as.matrix(segdist)[-1,1] )]
segset <- superimpose(segset,psp(startx,starty,cutx,cuty,segset$window))

} #end jj loop
if(plotit) plot(segset,col=rainbow(numsegs))
return(invisible(segset))
}

segset=ranpoly()

      

segset is a psp object from which I need to create a SpatialPolygons object.

+3


source to share


2 answers


Googling for spatstat as spatialPolygons

brings me to this with the first click , which is a vignette in spartstat dealing with shapefile handling. He spends a lot of time sp

figuring out how to convert -classes to spartstat objects. You may be interested in Section 3.2.5: SpatialPolygons and Section 3.2.6: SpatialPolygonsDataFrame Objects.



+1


source


Assuming you have a set of spartstat objects, you can try something like (untested):



require(sp)

# VECTOR OF spatstat OBJECT NAMES
segs <- (seg1,seg2,seg3)

segPolys <- as(segs[1], "SpatialPolygons")
  for( i in 2:length(segs)) {
    y <- as(segs[i], "SpatialPolygons")
      slot(y[[i]], "ID") <- paste(i)
    segPolys <- c(slot(y, "polygons"),segPolys)
   }

      

+1


source







All Articles