0

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 .

Community
  • 1
  • 1
Carl Witthoft
  • 20,573
  • 9
  • 43
  • 73
  • @Josh - thanks for the pointer. If I can figure out how to squoosh my vertex data into an `sp` object I'll report back on how that works. – Carl Witthoft Aug 07 '13 at 18:27
  • @JoshO'Brien Sadly, `gBuffer` "fills in" the polygon, turning it into a puffy cloud sort of thing. – Carl Witthoft Aug 07 '13 at 18:47
  • 1
    How do you post this on stack overflow with no net access? – Spacedman Aug 07 '13 at 19:02
  • Am having trouble knowing what you are after with no example data or images. Maybe you could at least indicate which of the answers/images in the link you provided does what you want. (I still think `gBuffer()` might be what you're after. Did you explore the `joinStyle` and `mitreLimit` arguments (to get 'non-puffy' corners for the buffered areas), and did you try setting `width` to a negative value to "deflate" the polygon(s))? – Josh O'Brien Aug 07 '13 at 19:09
  • 1
    @Spacedman I should clarify that I have basic proxy net access, but cannot send general requests out the way `sos` does. – Carl Witthoft Aug 07 '13 at 19:31
  • @JoshO'Brien I will continue playing w/ those parameters to see what happens. – Carl Witthoft Aug 07 '13 at 19:34
  • @JoshO'Brien It turns out my mistake was in the magnitude of `width` . Once I made it a small enough fraction of my current plot size, things look very good indeed. Go ahead and post your answer so I can check it off. (Tho' I must say it's a bit of a pain to turn a N-by-2 `matrix` into a `SpatialPolygons sp` object. I think I need to coerce twice with `bar<- as(my.matrix,'gpc.poly')` followed by `foo<-as(bar,'SpatialPolygons')` . In any case, thanks again for the solution. – Carl Witthoft Aug 07 '13 at 19:51
  • OK, just added an answer. Frankly, the more useful part of it may be the utility function `xy2SP()`, which I have posted before [here](http://stackoverflow.com/questions/16553497/calculating-the-area-of-a-confidence-ellipse-in-a-certain-region-of-space). As you say, creating a SpatialPolygons object in the first place is an unfortunate pain in the ***. – Josh O'Brien Aug 07 '13 at 20:38
  • @CarlWitthoft sos only uses basic http requests, and you can configure R to use a proxy. – Spacedman Aug 08 '13 at 20:41
  • @Spacedman in theory yes, in practice the corp firewall (in concert with Websense) blocks something-or-other and I cannot make contact with the locations that `???foo` is trying to reach. I've spent a lot of time fiddling w/ proxy/noproxy settings for R and no go. – Carl Witthoft Aug 08 '13 at 21:29

1 Answers1

2

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

Josh O'Brien
  • 159,210
  • 26
  • 366
  • 455