I use lidR package. I have a catalog and I want to save as a shapefile the centroid of each chunk make with opt_chunk_size of a catalog ?
1 Answer
There may be a better way to do this, but a combination of lidR
functions and the sf package should work. Try the following:
ctg <- readLAScatalog("~/Documents/data/lidar/example_catalog/")
opt_chunk_size(ctg) <- 1000
plot(ctg, chunk=TRUE)
chunks <- catalog_makechunks(ctg)
library(sf)
centroids <- lapply(chunks, function(chunk) {
return(st_point(x=c(chunk@center$x, chunk@center$y)))
})
centroids_shape <- st_as_sfc(centroids)
plot(centroids_shape)
st_write(centroids_shape, "/tmp/test.shp")
Plot of catalog:
Plot of un-chunked centroids:
Plot of chunked catalog:
Plot of chunked centroids:
Overlay of chunks, and their centroids:
** UPDATE ** - As mentioned by @JRR, you will need version 3.0.0 of the lidR
package to use catalog_makechunks()
which I believe is currently in development. In older versions you must use the internal function lidR:::catalog_makeclusters()
. To get version 3.0.0 of lidR
you can run:
library(devtools)
install_github("Jean-Romain/lidR", ref = "devel")
-
1This is a good answer. However
catalog_makechunks
exists only from v3.0.0 and is not "internal". It is exposed to users. Before v3.0.0 one must uselidR:::catalog_makeclusters()
that is actually internal.– JRRCommented Apr 1, 2020 at 15:14