st_intersection
is very slow compared to st_intersects
. So why not use the latter instead of the former? Here's an example with a small toy dataset, but the difference in execution time is huge for my actual set of just 62,020 points intersected with an actual geographic region boundary. I have 24Gb of RAM and the st_intersects
code takes a few seconds whereas the st_intersection
code takes more than 15 minutes (possibly much more, I haven't had the patience to wait...). Does st_intersection
do anything that I am not getting with st_intersects
?
The below code handles sfc
objects but I believe would work equally for sf
objects.
library(sf)
library(dplyr)
# create square
s <- rbind(c(1, 1), c(10, 1), c(10, 10), c(1, 10), c(1, 1)) %>% list %>% st_polygon %>% st_sfc
# create random points
p <- runif(50, 0, 11) %>% cbind(runif(50, 0, 11)) %>% st_multipoint %>% st_sfc %>% st_cast("POINT")
# intersect points and square with st_intersection
st_intersection(p, s)
# intersect points and square with st_intersects (courtesy of https://mcmap.net/q/472475/-r-convert-output-from-sf-st_within-to-vector)
p[st_intersects(p, s) %>% lengths > 0,]