-
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))

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)

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.