This document explains how objects are written on disk when processing a LAScatalog. As mentioned
in LAScatalog-class, users can set a templated filename to store the outputs on disk instead
of in R memory. By defaut LAS
objects are stored in .las files with writeLAS,
Raster*
objects are stored in .tif files with writeRaster,
Spatial*
objects are stored in .shp files with writeOGR,
data.frame
objects are stored in .csv files with fwrite, and other
objects are not supported. However, users can modify all these default settings and even add new drivers.
This manual page explain how. One may also refer to some unofficial documentation
here or
here.
A driver is stored in the @output_options
slot of a LAScatalog. It is a list that contains:
A function that receives an object and a path, and writes the object into a file using the path. The function can also have extra options.
A string that gives the file extension.
A string that gives the name of the argument used to pass the object to write in the function used to write the object.
A string that gives the name of the argument used to pass the path of the file to write in the function used to write the object.
A labelled list of extra parameters for the function used to write the object
For example, the driver to write a Raster*
is
list( write = raster::writeRaster, extension = ".tif", object = "x", path = "filename", param = list(format = "GTiff"))
And the driver to write a LAS
is
list( write = lidR::writeLAS, extension = ".las", object = "las", path = "file", param = list())
Users can modify the drivers to write different file types than the default. For example, to write in
GeoPackage instead of shapefile, one must change the Spatial
driver:
ctg@output_options$drivers$Spatial$extension <- ".gpkg"
To write in .grd files instead of .tif files one must change the Raster
driver:
ctg@output_options$drivers$Raster$extension <- ".grd" ctg@output_options$drivers$Raster$param$format <- "raster"
To write in .laz files instead of .las files one must change the LAS
driver:
ctg@output_options$drivers$LAS$extension <- ".laz"
The drivers allow LAS
, Spatial*
, Raster*
and data.frame
objects to be written. When
using the engine (catalog_apply) to build new tools, users may need to be able to write other
objects such as a list
. To do that users need to add a list
element into the output_options
:
ctg@output_options$drivers$list = list( write = base::saveRDS, object = "object", path = "file", extension = ".rds", param = list(compress = TRUE))
The LAScatalog
now has a new driver capable of writing a list
.
It is also possible to completely overwrite an existing driver. By default SpatialPointsDataFrame
objects are written into ESRI shapefiles with writeOGR. writeOGR
can write into other
file types, such as GeoPackage or GeoJSON and even as SQLlite database objects. But it cannot add data into
an existing SQLlite database. Let's create our own driver for a SpatialPointsDataFrame
. First
we need a function able to write and append a SpatialPointsDataFrame
into a SQLlite database
from the object and the path.
dbWrite_SpatialPointsDataFrame = function(x, path, name) { x <- as.data.frame(x) con <- RSQLite::dbConnect(RSQLite::SQLite(), path) RSQLite::dbWriteTable(con, name, x, append = TRUE) RSQLite::dbDisconnect(con) }
Then we create the driver. User-defined drivers supersede default drivers:
ctg@output_options$drivers$SpatialPointsDataFrame = list( write = dbWrite_SpatialPointsDataFrame, extension = ".sqlite", object = "x", path = "path", param = list(name = "layername"))
Then to be sure that we do not write several .sqlite files, we don't use templated filename.
opt_output_files(ctg) <- paste0(tempdir(), "/mysqlitefile")
And all the SpatialPointsDataFrame
will be appended in a single database.