Change extent of lidR LAS object for raster alignment

extentslidrr

Is it possible/allowed to change the extent of a lidR LAS point cloud? And if yes, does it affect the generation of rasters (e.g. with grid_metrics or grid_canopy), so that they cover the full extent?

So far, when creating a raster with lidR, it will (naturally) only be as big as the point cloud. Since I want pixels to align to a certain grid, I'd like to use a specified extent on which the rasters will be calculated (ignoring whether there are points or not, cells without points should result to NA).

In the doc of grid_metric I discovered a parameter start, but I don't know how to calculate the right values to ensure alignment to a given extent – also it seems circumstancial. My workaround so far is to extend the raster to the extent:

# calculate surface model
dsm = grid_canopy(las, res = 0.5, p2r(0.5))

# extend to original LAS extent
dsm = extend(dsm, extent(dtm), value=NA)

But that feels "dirty" to me and I also fear issues with pixel alignment if the point cloud seeds at uneven coordinates.

My naive approach extent(las) = extent(dtm) doesn't work (‘extent’ is not a slot in class “LAS”) and after reading the las formal class doc I am hesitant to manipulate the header itself, since I don't want to create invalid las objects.

Is there a proper way to do this (treat the point cloud as covering a given extent, in particular align raster generation to it)?

Background

I am working on a tiled grid that covers a large area. The point cloud is not always covering the full extent of a tile, as can be seen below for tile 2717_1088 (in green the tile, in grey the extent of the point cloud).
enter image description here

To avoid problems with pixel alignment, I'd like to process the point cloud on the full extent, so that pixels fit perfectly (the tile is 1000x1000m, e.g. 10m pixels will be perfectly aligned on the full extent. If the pointcloud however generates a raster starting at an uneven coordinate, the pixels may be shifted. I don't want to resample due to imprecision and computational cost).

Best Answer

Well, I think you are overthinking the problem.

Considering what I'm understanding of your problem I can't see any problem. In lidR rasters are always properly aligned according to a reference point (start parameter) and the extent of the point cloud does not matter. The grid is snapped and slightly extended to be aligned. No possible misalignment.

If you really want to get a specific raster extend is an option but you can also provide a RasterLayer as input. But it becomes harder to manage when processing an entire LAScatalog collection.

library(lidR)
library(raster)

LASfile <- system.file("extdata", "Megaplot.laz", package="lidR")
las = readLAS(LASfile)

b <- extent(las) + c(-40,70,0,100)
r <- raster(b, res = 8)
m <- grid_metrics(las, ~mean(Z), r)

plot(m)
plot(extent(m), add = T, col = "red", lwd = 2)