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")