The lwgeom
package and the use of the global
Sys.setenv("LWGEOM_WKT" = "true")
option allows faster
conversion of simple features objects to well-known text (WKT)
representation, due to the use of low-level code. It also enables faster
printing of complex geometries. Note that using this method, the output
may differ from the default, which can cause reproducibility problems.
Alternatively, you can also use the lwgeom::st_astext() and wk::as_wkt() functions directly.
library("sf")
# generate some data
n = 100000
pts = data.frame(x = rnorm(n), y = rnorm(n))
pts = st_as_sf(pts, coords = c("x", "y"))
pts = st_as_sfc(pts)
system.time(x <- st_as_text(pts))
## user system elapsed
## 7.36 0.16 7.51
x[1]
## [1] "POINT (0.007360117 -1.83104)"
# `lwgeom` must be installed
Sys.setenv("LWGEOM_WKT" = "true")
system.time(y <- st_as_text(pts))
## user system elapsed
## 0.20 0.01 0.22
y[1]
## [1] "POINT(0.0073601 -1.8310401)"