Home > Software design >  Speeding up st_buffer on many line segments
Speeding up st_buffer on many line segments

Time:10-05

I am buffering around a road network with many small line segments and it takes a long time. When I tried using the profiler to see what was making it take so long most of the time is occupied by ".Call". So I am wondering what is happening in the .Call section and is there anything I could do to speed up this process?

Here is a reproducible example:

library(sf)
my_linestring_sfc <- st_sfc(
  st_linestring(matrix(c(-10, -10, -1, -1, 0, 0), ncol = 2, byrow = TRUE)), 
  st_linestring(matrix(c(10, -10, 1, -1, 0, 0), ncol = 2, byrow = TRUE)), 
  st_linestring(matrix(c(0, 0, 0, 1, 0, 10), ncol = 2, byrow = TRUE))
)

line_lst <- map(1:10000 *5, ~my_linestring_sfc   .x)

lins <- do.call(c, line_lst)

profvis::profvis({st_buffer(lins, 100)})

And what the profile flame graph looks like for me: flamegraph

CodePudding user response:

Note that the .Call refers to an internal call within st_buffer. It probably calls c libraries or other optimized compiled code. It is unlikely that you will be able to easily speed this up. Simplifying your lines might be one way to speed up without hardly degrading the quality of you outcome, this can be done with st_simplify.

A bit more detail if you look at the code of sf:::st_buffer.sfc you see it calls: sf:::CPL_geos_op which in it self is basically a wrapper for a call to a internal compiled function.

  •  Tags:  
  • r sf
  • Related