3

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?

djas
  • 973
  • 8
  • 24

1 Answers1

5

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.

Ben Bolker
  • 211,554
  • 25
  • 370
  • 453
  • 2
    An important point is that the red shape isn't a circle either - its a 120-agon, because `?st_buffer` has `nQuadSegs = 30`. If you want a perfect Platonic circle in R you'll need to represent it as an object with a centre and a radius. – Spacedman Oct 23 '19 at 11:29
  • @djas `c1 = list(radius=4.3, centre=c(2.2, 5.12))` or similar. I don't know if there's an R package to work with explicit exact geometric objects. Go check CRAN for one. – Spacedman Oct 28 '19 at 13:03