Skip to content

55 lidar

image image image

LiDAR data analysis and visualization with whitebox and leafmap

Create a new conda env to install required packages:

1
2
3
4
5
conda create -n geo python
conda activate geo
conda install -c conda-forge mamba
mamba install -c conda-forge pygis
pip install laspy[lazrs]

Uncomment the following line to install packages in Google Colab.

1
# !pip install leafmap
1
# !pip install laspy[lazrs]

Import libraries

1
2
3
import os
import leafmap
import whitebox

Set up whitebox

1
2
3
wbt = whitebox.WhiteboxTools()
wbt.set_working_dir(os.getcwd())
wbt.set_verbose_mode(False)

Download sample data

1
2
url = "https://opengeos.org/data/lidar/madison.zip"
filename = "madison.las"
1
leafmap.download_file(url, "madison.zip", unzip=True)

Read LAS/LAZ data

1
laz = leafmap.read_lidar(filename)
1
laz
1
str(laz.header.version)

Upgrade file version

1
las = leafmap.convert_lidar(laz, file_version="1.4")
1
str(las.header.version)

Write LAS data

1
leafmap.write_lidar(las, "madison.las")

Histogram analysis

1
wbt.lidar_histogram("madison.las", "histogram.html")

Visualize LiDAR data

1
leafmap.view_lidar("madison.las")

Remove outliers

1
wbt.lidar_elevation_slice("madison.las", "madison_rm.las", minz=0, maxz=450)

Visualize LiDAR data after removing outliers

1
leafmap.view_lidar("madison_rm.las", cmap="terrain")

Create DSM

1
2
3
wbt.lidar_digital_surface_model(
    "madison_rm.las", "dsm.tif", resolution=1.0, minz=0, maxz=450
)
1
leafmap.add_crs("dsm.tif", epsg=2255)

Visualize DSM

1
2
3
m = leafmap.Map()
m.add_raster("dsm.tif", palette="terrain", layer_name="DSM")
m

Create DEM

1
wbt.remove_off_terrain_objects("dsm.tif", "dem.tif", filter=25, slope=15.0)

Visualize DEM

1
2
m.add_raster("dem.tif", palette="terrain", layer_name="DEM")
m

Create CHM

1
chm = wbt.subtract("dsm.tif", "dem.tif", "chm.tif")
1
2
m.add_raster("chm.tif", palette="gist_earth", layer_name="CHM")
m