In the built-in {grDevices}
package there is
chull()
function, which contains an alternative
algorithm for calculating convex hull. It is faster compared to the
sf::st_convex_hull()
.
library("sf")
set.seed(1)
n = 2000000
df = data.frame(x = rnorm(n), y = rnorm(n))
pts = st_as_sf(df, coords = c("x", "y"))
pts = st_combine(pts)
system.time({
ch_sf = st_convex_hull(pts)
})
## user system elapsed
## 3.37 0.22 3.59
system.time({
crds = st_coordinates(pts)
ch_r = grDevices::chull(crds)
ch_r = st_as_sf(df[ch_r, ], coords = c("x", "y"))
ch_r = st_cast(st_combine(ch_r), "POLYGON")
})
## user system elapsed
## 0.16 0.05 0.20
st_equals(ch_r, ch_sf, sparse = FALSE)
## [,1]
## [1,] TRUE
plot(ch_sf, axes = TRUE, main = "Convex hull")