Search code examples
rr-spr-sf

How can a buffer around a point not be a circle? (Or, strange behavior of rgeos::gBuffer() )


I've switched from package sp to sf for many of my geo-things in R, and only now I noticed a strange behavior in sp/rgeos. Here it is:

library(sp)
library(sf)
library(rgeos)

# crs
WGS84M = "+proj=merc +lon_0=0 +k=1 +x_0=0 +y_0=0 +datum=WGS84 +units=m +no_defs"

# a point
point = data.frame(x = 0, y = 0)

# simple feature from point
f.point = st_as_sf(point, coords = c('x', 'y'))
st_crs(f.point) = WGS84M
f.buf = st_geometry(st_buffer(f.point, dist = 5 * 1000))

# spatial object from point
s.point = SpatialPoints(point, proj4string = CRS(WGS84M))
s.buf = gBuffer(s.point, width = 5 * 1000)

plot(s.buf, border = "blue")
plot(f.buf, border = "red", add = T)

I get this:

enter image description here

Perhaps the resolution of the image I uploaded isn't great, but the point here is: there's a circle in red, and a non-circle (I tried to count the sides, seems to be a 20-gon) in blue. If you convert the sf object to sp you still get a circle, and if you convert the sp object to sf, you still get a 20-gon (so it isn't just a difference, say, in plot methods).

I expected gBuffer() around a point to return a circle. Is there a reason I shouldn't expect that? Can anyone explain what's going on?


Solution

  • The fine rgeos::gBuffer documentation includes

    quadsegs [default=5] Number of line segments to use to approximate a quarter circle.

    thus a 20-gon (=4*5) is exactly what you should expect with the default settings. If you want something closer to a circle, increase the value of quadsegs.

    I assume this is done for computational efficiency.