rskeletal-mesh

Looking for skeleton or polygon offset algorithm in R


I found some useful links in this SO question An algorithm for inflating/deflating (offsetting, buffering) polygons . Sorry - no 'net access so sos doesn't work, so does anyone have an implementation of the skeleton algorithm in R?

Edit: I would like to generate deflated polygons as in the StackOverflow link (top image); or as seen on http://en.wikipedia.org/wiki/Straight_skeleton .


Solution

  • gBuffer(), from the elegant and powerful rgeos package accepts negative values in its width argument, returning SpatialPolygons that are 'shrunk' by the given amount.

    library(sp)
    library(rgeos)
    
    ## Create a SpatialPolygons object from set of x-y coordinates (the hard part!)
    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))
    SP <- xy2SP(xy)
    
    ## Shrink the SpatialPolygons object by supplying a negative width to gBuffer()
    plot(SP)
    plot(gBuffer(SP, width=-0.2), add=TRUE, border="red")
    

    enter image description here