Home > OS >  Is there a way to have better buffer borders in R?
Is there a way to have better buffer borders in R?

Time:10-19

I'm hoping maybe someone can help me with the following situation. I'm working on a project were I need to plot a circle (radius) around a centroid in R. I'm also using this centroid to do an intersection with other geometry. The problem I'm facing is that the buffer border is not a very well defined circle which doesn't look good when I plot both the circle and the intersection.

My code looks somewhat like this:

using sf and ggplot2

Radius of 100km circle = st_buffer(centroid,100000) and for the intersection

intersection = st_intersection(geometry,circle)

for the plot

ggplot() geom_sf(data= circle, fill = "yellow")

The border of buffer is "low quality"

CodePudding user response:

Not so bad! That said, if you really want to smooth the circle/buffer border, you can use the package smoothr.

Here is one possibility :

library(smoothr)

# Smooth circle border
circle_smooth <- smooth(densify(circle, max_distance = 10), method = "ksmooth")

# Plot
ggplot2::ggplot() 
  ggplot2::geom_sf(data= circle_smooth, fill = "yellow")

CodePudding user response:

You can also use sf::st_buffer(..., nQuadSegs=)

From https://geocompr.robinlovelace.net/geometric-operations.html

The third and final argument of st_buffer() is nQuadSegs, which means ‘number of segments per quadrant’ and is set by default to 30 (meaning circles created by buffers are composed of 4×30=120 lines). This argument rarely needs to be set. Unusual cases where it may be useful include when the memory consumed by the output of a buffer operation is a major concern (in which case it should be reduced) or when very high precision is needed (in which case it should be increased).

  • Related