I was wondering why sf::st_intersection() takes too much time, in comparison with sp::over(); I use sf for pretty much all of my geospatial workflows (I was used to sp as well), and more and more packages are building support for sf objects; is there a way for speeding up st_intersection? According to st_intersection man page: "A spatial index is built on argument x", so I can't build a spatial index to speed it up.
In the following example eke is a point (sp or sf) object and ver_poly is a polygon object, even coerced to "Spatial", sp::over() goes faster (just using 10000 points, whole object is 150000 features):
> system.time(en_ver <- over(eke[1:10000,], as(ver_poly, "Spatial")))
user system elapsed
0.573 0.004 0.576
> system.time(eke[1:10000,] %>% st_as_sf(coords = c("longitud", "latitud")) %>% st_set_crs(4326) %>%
+ st_intersection(ver_poly))
although coordinates are longitude/latitude, st_intersection assumes that they are planar
user system elapsed
83.552 0.004 83.585
Best Answer
You are converting from sf to sp in the first, and from from sp to sf in the second - you should avoid timing those conversions.
sf
is sometimes slow with points because of the way they are stored, but what you gain is far greater consistency than with sp, and usually faster ops.Here I think it is comparable, but will depend on your actual data.
Here's a like-to-like comparison. You need
st_intersects
for the analog to whatover
is doing here.Created on 2019-06-06 by the reprex package (v0.3.0)