set.seed(1)
i <- j <- 200
m <- matrix(runif(i*j), i, j)
library(pixmap)
fmaker <- function() tempfile(fileext = ".pnm")
## pixmap
## I cannot get this to work
fpix <- function(x, write = TRUE) {
suppressWarnings(out <- pixmap::pixmap(x))
if (write) pixmap::write.pnm(out, fmaker())
out
}
## raster (uses rgdal for the write)
fras <- function(x, write = TRUE) {
out <- raster::raster(x)
if (write) raster::writeRaster(out, fmaker(), format = "PNM")
out
}
## stars/sf - faster than rgdal
## but can't get this to work with Float32 type
fstr <- function(x,write = TRUE) {
out <- stars::st_as_stars(x)
if (write) sf::gdal_write(out, file = fmaker(), driver = "PNM",
geotransform = c(0, 1, 0, nrow(x), 0, -1), type = "Byte")
out
}
rbenchmark::benchmark(raster = fras(m), stars = fstr(m))
## test replications elapsed relative user.self sys.self user.child
## 1 raster 100 3.279 10.12 3.058 0.218 0
## 2 stars 100 0.324 1.00 0.263 0.062 0
## sys.child
## 1 0
## 2 0