I have a folder in which I have several thousand .LAZ files and I would like to select those that are contained in the .SHP polygon. Can it be programmed in R, e.g. by adding a given name to .LAZ files. Loading so many files into a GIS program is not possible.
I tried this script but it doesn't work. do i need to generate .LAS it increases the data significantly.
# Install and load necessary packages
install.packages("lidR")
install.packages("rgdal")
library(lidR)
library(rgdal)
# Read the .shp file with the extent
extent <- readOGR(dsn = "/path/to/folder", layer = "filename")
# Read the .laz files in the folder
folder <- "/path/to/folder"
laz_files <- list.files(folder, pattern = "\\.laz$", full.names = TRUE)
# Iterate through the .laz files and select those that overlap with the extent
for (laz_file in laz_files) {
points <- readLAS(laz_file)
if (extent(points) %over% extent) {
cat("File", laz_file, "overlaps with the extent.\n")
# Create a new file name with the selected file name appended
new_name <- sub(".laz", paste0("_selected_", basename(laz_file)), laz_file)
# Save the file under the new name
writeLAS(points, new_name)
} else {
cat("File", laz_file, "does not overlap with the extent.\n")
}
}