I would like to create buffers in polygons, surrounding my original polygon. However, I want to keep only the "outside polygon", not include also original polygon. In Buffer in ArcGIS I can just set function OUTSIDE_ONLY and I have my buffer zone only (http://webhelp.esri.com/arcgisdesktop/9.3/index.cfm?TopicName=Buffer%20(Analysis)).

However I didn't find a way how to make it in R? I've tried both, {rgeos} and {raster}. I know I can delete my original polygon later ({rgeos}), but is there a way how make it happen while creating buffers?

enter image description here

# stack overflow
library(rgeos)

# create polygon
p1 = readWKT("POLYGON((2 2,-2 2,-2 -2,2 -2,2 2))")
# create two buffers - one wth {raster}, one with {rgeos},
# both covers also original polygon !
b.r<-buffer(p1, width = 1, dissolve = T)
gb.r<-gBuffer(p1, width = 0.3, byid = T)

# keep only buffer
buff<- gDifference(b.r, p1)

# how can I create OUTSIDE_ONLY polygon without gDifference inter-step???
up vote 5 down vote accepted

There is no way to define "outside only" in gBuffer. You have to go through the additional step of turning the inner polygon to null, and for a good reason. You can use the raster::erase function to remove the internal polygon. If you really want this as part of the gBuffer function why not just write your own modification of gBuffer that adds an "outside only" argument?

Add required libraries and example data. I subset the first polygon and then use spTransfrom to account for gBuffer not accepting unprojected data.

library(raster)
library(rgeos)
library(rgdal)

p <- shapefile(system.file("external/lux.shp", package="raster"))
  p <- p[1,]
  p <- spTransform(p, CRS=CRS("+proj=merc +ellps=GRS80"))

Here we buffer the polygon and then use erase to turn the internal (original) polygon to null.

b <- gBuffer(p, width=1000, quadsegs=100)
plot(p)
  plot(b,add=TRUE)  

e <- erase(b, p)                             # using raster:erase
e <- rgeos::gDifference(b, p, byid=TRUE)     # using rgeos:gDifference
  plot(e, col="red")    
  • Thank you for the answer @JeffreyEvans. However, this still requires two steps - 1) create buffer, 2) delete interior polygon. Thus, please how is raster::erase better then rgeos::gDifference? – maycca May 24 '16 at 23:35
  • 1
    Just an alternative to 'gDifference'. There is no way to do this in a single step but you could write your own function that does. This is the joy of R. – Jeffrey Evans May 24 '16 at 23:37
  • Oh ! can you please help me how to create this type of function? I need to create polygons in distance 1,2,3,4,5... up to 10, and if I want always to keep only the uoter polygon, it would be pain... I am really beginner in creating my functions... – maycca May 24 '16 at 23:50

Hourrah !! There is a function to create only OUTSIDE buffer !! my first (working) function in R !! :)

# create basic function
outerBuffer<-function(x){
  buff<-buffer(x, width = 1, dissolve = T)
  e<-erase(buff,x)
  return(e)
}

# create data
p1 = readWKT("POLYGON((2 2,-2 2,-2 -2,2 -2,2 2))")

# apply function, create only outside buffer
a2<-outerBuffer(p1)

# tradaaa ! :)
plot(a2, col = "black", main= "Outer buffer only")

(inspired from here: http://www.dummies.com/how-to/content/how-to-create-a-function-in-r.html)

enter image description here

And simple solution, if multiple "Doughnuts" are need (then we need to create buffer = solid polygons and then erase the smaller solid polygon from the larger one. And delete the original polygon from the last buffer = solid polygon)

# create solid polygons
multiBuffer<-sapply(1:10, function(i){
  buffer(p1,width = i, dissolve = T)
  })

# create doughnuts
for (i in length(multiBuffer):2) {
  multiBuffer[[i]]<-erase(multiBuffer[[i]], multiBuffer[[i-1]])
}

# I still need to delete my original polygon
multiBuffer[[1]]<-erase(multiBuffer[[1]], p1)

enter image description here

  • 1
    Good for you! I would add an additional argument to the function that will allow you to define the buffer distance. Just add "d" to the function arguments, after "x" and then in the gBuffer arguments width = d – Jeffrey Evans May 25 '16 at 2:07

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.