sf
sf copied to clipboard
s2 much slower than sf with st_union in epsg 4326
Hello,
Writing the following operation, I get much slower runtime using s2 using the 4326 projection
library(sf)
#> Linking to GEOS 3.9.0, GDAL 3.2.1, PROJ 7.2.1
sf_use_s2(T)
shp <- read_sf(system.file("shape/nc.shp", package = "sf"))
shp2 <- st_transform(shp,3857)
system.time(st_union(shp))
#> user system elapsed
#> 0.16 0.00 0.15
system.time(st_union(shp2))
#> user system elapsed
#> 0.04 0.00 0.05
sf_use_s2(F)
#> Spherical geometry (s2) switched off
system.time(st_union(shp))
#> although coordinates are longitude/latitude, st_union assumes that they are planar
#> user system elapsed
#> 0.03 0.00 0.03
system.time(st_union(shp2))
#> user system elapsed
#> 0.05 0.00 0.05
Created on 2021-07-27 by the reprex package (v2.0.0)
I don't have a reprex for it, but the difference in performance increases severely at scale, and sometimes breaks altogether. Also seems to hold true for st_intersects. Perhaps the difference here deserves a warning, such as is given in the non-sf workflow?
Showing the difference at scale:
library(sf)
nc = st_read(system.file("shape/nc.shp", package="sf"))
#> Reading layer `nc' from data source
#> `C:\Users\Arthur.Gailes\Documents\R\R-4.1.0\library\sf\shape\nc.shp'
#> using driver `ESRI Shapefile'
#> Simple feature collection with 100 features and 14 fields
#> Geometry type: MULTIPOLYGON
#> Dimension: XY
#> Bounding box: xmin: -84.32385 ymin: 33.88199 xmax: -75.45698 ymax: 36.58965
#> Geodetic CRS: NAD27
grid <- st_make_grid(nc, cellsize = 1e-1)
system.time(st_union(st_transform(grid, 4326)))
#> user system elapsed
#> 2.41 0.00 2.41
system.time(st_union(st_transform(grid, 3857)))
#> user system elapsed
#> 0.33 0.02 0.35
grid <- st_make_grid(nc, cellsize = 1e-2)
system.time(st_union(st_transform(grid, 4326)))
#> user system elapsed
#> 1826.68 0.28 1827.17
system.time(st_union(st_transform(grid, 3857)))
#> user system elapsed
#> 18.72 0.17 18.91