Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to get a SpatialPolygons (SP-class) from a set of segments (psp in spatstat)?
    primarykey
    data
    text
    <p>I have a set of random segments drawing a kind of tessellation (of triangles, rectangles ...) in a <a href="https://stackoverflow.com/questions/14362127/how-to-generate-a-list-of-segments-from-a-list-of-random-self-intercepting-lines">window</a> (in spatstat R). I need to convert it into a set of polygons (SpatialPolygons) to calculate some indices (like area, shape indices ...).</p> <p>This is apparently simple but I couldn't find how to do it ...</p> <p>Here is a bit of code from <a href="https://stackoverflow.com/questions/14362127/how-to-generate-a-list-of-segments-from-a-list-of-random-self-intercepting-lines">Carl Witthoft</a> that generate a random pattern of self-intercepting segments :</p> <pre><code>ranpoly &lt;- function(numsegs=10,plotit=TRUE) { require(spatstat) # temp fix: put the first seg into segset. Later make it a constrained random. segset&lt;-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 &lt;-sample(c(-0.2,1.2),1) #force 'x1' outside the frame # watch that sample() gotcha if(segset$n&lt;=5) sampset &lt;- c(5,5) else sampset&lt;-5:segset$n startseg&lt;-sample(sampset,1) #don't select a frame segment # this is slope of segment to be constructed slope &lt;- tan(runif(1)*2*pi-pi) # range +/- Inf # get length of selected segment seglen&lt;-lengths.psp(segset)[startseg] startcut &lt;- runif(1) # grab the coords of starting point (similar triangles) startx&lt;- segset$ends$x0[startseg] + (segset$ends$x1[startseg]-segset$ends$x0[startseg])*startcut #seglen starty&lt;- 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 &lt;- starty + slope*(endx-startx) newpsp&lt;-psp(startx,starty,endx,endy,segset$window,check=FALSE) # don't calc crossing for current element of segset hits &lt;- crossing.psp(segset[-startseg],newpsp) segdist &lt;- 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 &lt;- hits$x[ which.min( as.matrix(segdist)[-1,1] )] cuty &lt;- hits$y[which.min(as.matrix(segdist)[-1,1] )] segset &lt;- superimpose(segset,psp(startx,starty,cutx,cuty,segset$window)) } #end jj loop if(plotit) plot(segset,col=rainbow(numsegs)) return(invisible(segset)) } segset=ranpoly() </code></pre> <p>segset is the psp object from wich I need to create a SpatialPolygons object.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload