rproj <- function() sprintf("+proj=laea +lon_0=%f +lat_0=%f", runif(1, -180, 180), runif(1, -90, 90))
pts <- geosphere::randomCoordinates(1e5)
ll <- function() "EPSG:4326"
library(terra)
library(sf)
terra_ <- function(x) {
terra::project(x, to = rproj(), from = ll())
}
sf_ <- function(x) {
sf::sf_project(pts, to = rproj(), from = ll())
}
reproj_ <- function(x) {
reproj::reproj_xy(x, rproj(), source = ll())
}
wk_ <- function(x, trans) {
## everything else returns a matrix, so we're a bit unfair on wk here
as.matrix(wk::wk_transform(wk::xy(x[,1, drop = TRUE], x[,2, drop = TRUE]), trans))
}
gdalraster_ <- function(x) {
gdalraster::transform_xy(x, srs_to = rproj(), srs_from = ll())
}
PROJ_ <- function(x) {
PROJ::proj_trans(x, rproj(), source_crs = ll())
}
proj4_ <- function(x) {
proj4::ptransform(x * pi/180, dst.proj = rproj(), src.proj = ll())
}
system.time(x <- terra_(pts))
system.time(x <- sf_(pts))
system.time(x <- reproj_(pts))
trans <- PROJ::proj_trans_create(target_crs = rproj(), source_crs = ll())
system.time(x <- wk_(pts, trans))
system.time(x <- gdalraster_(pts))
colnames(pts) <- c("x", "y")
system.time(x <- PROJ_(pts))
system.time(x <- proj4_(pts))
rbenchmark::benchmark(terra = terra_(pts),
sf = sf_(pts),
reproj = reproj_(pts),
wk = wk_(pts, trans),
gdalraster = gdalraster_(pts),
PROJ = PROJ_(pts),
proj4 = proj4_(pts),
replications = 100) |> dplyr::arrange(relative)
Last active
June 20, 2025 11:28
-
-
Save mdsumner/049c365db354bb6aea0b1a00447adb00 to your computer and use it in GitHub Desktop.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
with more points we start to differentiate a bit, gdalraster looking good (really there't not much point caring with these numbers, all are good)