-
Notifications
You must be signed in to change notification settings - Fork 8
Spanner 3D Display Example
Andrew Sánchez Meador edited this page Nov 14, 2024
·
2 revisions
This example script demonstrates the process of reading a LAS file and visualizing it in 3D using the lidR
, spanner
, and plotly
libraries in R.
library(lidR)
library(spanner)
library(plotly)
The following code reads a LAS file, applies a filter to drop points below a certain height, and extracts a region of interest (ROI).
# Replace 'path_to_las_file' with the actual path to your LAS file
LASfile <- system.file("extdata", "MixedConifer.laz", package="lidR")
poi <- "-drop_z_below 0 -inside 481280 3812940 481320 3812980"
las <- readLAS(LASfile, select = "xyz", filter = poi)
# Check if LAS data is read correctly
if (is.null(las)) {
stop("Failed to read LAS file")
}
This section segments trees in the LAS file using the Li et al. (2012) algorithm.
las <- segment_trees(las, li2012(dt1 = 1.4))
![](https://private-user-images.githubusercontent.com/3956719/386216501-9cb8680e-cba0-4dc1-a7e8-e4fc4eb41b1c.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3Mzk2ODM1MTcsIm5iZiI6MTczOTY4MzIxNywicGF0aCI6Ii8zOTU2NzE5LzM4NjIxNjUwMS05Y2I4NjgwZS1jYmEwLTRkYzEtYTdlOC1lNGZjNGViNDFiMWMucG5nP1gtQW16LUFsZ29yaXRobT1BV1M0LUhNQUMtU0hBMjU2JlgtQW16LUNyZWRlbnRpYWw9QUtJQVZDT0RZTFNBNTNQUUs0WkElMkYyMDI1MDIxNiUyRnVzLWVhc3QtMSUyRnMzJTJGYXdzNF9yZXF1ZXN0JlgtQW16LURhdGU9MjAyNTAyMTZUMDUyMDE3WiZYLUFtei1FeHBpcmVzPTMwMCZYLUFtei1TaWduYXR1cmU9MTM5MDI3Y2RkZGJmNTQzYzU0YTAxN2ExYWNmNTg5OTYzNTNmYmI5Yzk4ZjkzMzQ1NTBlMDczNjMwOTRjY2ZiNiZYLUFtei1TaWduZWRIZWFkZXJzPWhvc3QifQ.5t5DxChd4OTYVe07aQidMOTGfMfAzfv1LoFpzZTmEJk)
The custom function spanner_pal
creates a continuous color palette for visualization purposes.
spanner_pal <- function(n) {
colors <- c(
"#F52220", "#E8801A", "#E1DA8A", "#5EA530", "#114232",
"#2BB4A2", "#115A5D", "#B20F66", "#560F11", "#C0181A"
)
colorRampPalette(colors)(n)
}
This function displays the LAS data in 3D using the plotly
library.
display_las_3d <- function(las, point_size = 3) {
# Check if the LAS object is valid
if (is.null(las) || !inherits(las, "LAS")) {
stop("Invalid LAS object")
}
# Extract the coordinates and treeID
coords <- as.data.frame(las@data[, c("X", "Y", "Z", "treeID")])
# Create a 3D scatter plot
plot_ly(coords, x = ~X, y = ~Y, z = ~Z, color = ~treeID,
colors = spanner_pal(length(unique(coords$treeID))),
type = "scatter3d", mode = "markers", marker = list(size = point_size)) %>%
layout(scene = list(xaxis = list(title = 'X'),
yaxis = list(title = 'Y'),
zaxis = list(title = 'Z')))
}
# Display the LAS data in 3D
display_las_3d(las)
![](https://private-user-images.githubusercontent.com/3956719/386213649-3797d4ad-3285-421f-9962-eba2121e15b7.gif?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3Mzk2ODM1MTcsIm5iZiI6MTczOTY4MzIxNywicGF0aCI6Ii8zOTU2NzE5LzM4NjIxMzY0OS0zNzk3ZDRhZC0zMjg1LTQyMWYtOTk2Mi1lYmEyMTIxZTE1YjcuZ2lmP1gtQW16LUFsZ29yaXRobT1BV1M0LUhNQUMtU0hBMjU2JlgtQW16LUNyZWRlbnRpYWw9QUtJQVZDT0RZTFNBNTNQUUs0WkElMkYyMDI1MDIxNiUyRnVzLWVhc3QtMSUyRnMzJTJGYXdzNF9yZXF1ZXN0JlgtQW16LURhdGU9MjAyNTAyMTZUMDUyMDE3WiZYLUFtei1FeHBpcmVzPTMwMCZYLUFtei1TaWduYXR1cmU9ZDc2MjljNGE4Mjg4NTQyMTAxOTFmOTExY2RiNGM3MTI3ZmQ2ZmRjZWY1ZjExZTFiNDIzMWEzYjJkYTlmMTliOSZYLUFtei1TaWduZWRIZWFkZXJzPWhvc3QifQ.aTTI7-rhsqt-QK1IGHlS0oE0u2mTbAxpbgmYVnkrwiE)
The above code will create an interactive 3D scatter plot of the LAS data, colored by treeID
, allowing you to explore the point cloud in a 3D space.