55 lidar
LiDAR data analysis and visualization with whitebox and leafmap
Create a new conda env to install required packages:
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.
In [1]:
Copied!
# !pip install leafmap
# !pip install leafmap
In [2]:
Copied!
# !pip install laspy[lazrs]
# !pip install laspy[lazrs]
Import libraries¶
In [3]:
Copied!
import os
import leafmap
import whitebox
import os
import leafmap
import whitebox
Set up whitebox¶
In [4]:
Copied!
wbt = whitebox.WhiteboxTools()
wbt.set_working_dir(os.getcwd())
wbt.set_verbose_mode(False)
wbt = whitebox.WhiteboxTools()
wbt.set_working_dir(os.getcwd())
wbt.set_verbose_mode(False)
Downloading WhiteboxTools pre-compiled binary for first time use ...
Downloading WhiteboxTools binary from https://www.whiteboxgeo.com/WBT_Linux/WhiteboxTools_linux_amd64.zip
Decompressing WhiteboxTools_linux_amd64.zip ...
WhiteboxTools package directory: /home/runner/work/leafmap/leafmap/.venv/lib/python3.12/site-packages/whitebox Downloading testdata ...
Out[4]:
0
Download sample data¶
In [5]:
Copied!
url = "https://open.gishub.org/data/lidar/madison.zip"
filename = "madison.las"
url = "https://open.gishub.org/data/lidar/madison.zip"
filename = "madison.las"
In [6]:
Copied!
leafmap.download_file(url, "madison.zip", unzip=True)
leafmap.download_file(url, "madison.zip", unzip=True)
madison.zip already exists. Skip downloading. Set overwrite=True to overwrite.
Out[6]:
'/home/runner/work/leafmap/leafmap/docs/notebooks/madison.zip'
Read LAS/LAZ data¶
In [7]:
Copied!
laz = leafmap.read_lidar(filename)
laz = leafmap.read_lidar(filename)
In [8]:
Copied!
laz
laz
Out[8]:
<LasData(1.3, point fmt: <PointFormat(1, 0 bytes of extra dims)>, 4068294 points, 2 vlrs)>
In [9]:
Copied!
str(laz.header.version)
str(laz.header.version)
Out[9]:
'1.3'
Upgrade file version¶
In [10]:
Copied!
las = leafmap.convert_lidar(laz, file_version="1.4")
las = leafmap.convert_lidar(laz, file_version="1.4")
In [11]:
Copied!
str(las.header.version)
str(las.header.version)
Out[11]:
'1.4'
Write LAS data¶
In [12]:
Copied!
leafmap.write_lidar(las, "madison.las")
leafmap.write_lidar(las, "madison.las")
Histogram analysis¶
In [13]:
Copied!
wbt.lidar_histogram("madison.las", "histogram.html")
wbt.lidar_histogram("madison.las", "histogram.html")
Out[13]:
0
Visualize LiDAR data¶
In [14]:
Copied!
leafmap.view_lidar("madison.las")
leafmap.view_lidar("madison.las")
Remove outliers¶
In [15]:
Copied!
wbt.lidar_elevation_slice("madison.las", "madison_rm.las", minz=0, maxz=450)
wbt.lidar_elevation_slice("madison.las", "madison_rm.las", minz=0, maxz=450)
Out[15]:
0
Visualize LiDAR data after removing outliers¶
In [16]:
Copied!
leafmap.view_lidar("madison_rm.las", cmap="terrain")
leafmap.view_lidar("madison_rm.las", cmap="terrain")
Create DSM¶
In [17]:
Copied!
wbt.lidar_digital_surface_model(
"madison_rm.las", "dsm.tif", resolution=1.0, minz=0, maxz=450
)
wbt.lidar_digital_surface_model(
"madison_rm.las", "dsm.tif", resolution=1.0, minz=0, maxz=450
)
Out[17]:
0
In [18]:
Copied!
leafmap.add_crs("dsm.tif", epsg=2255)
leafmap.add_crs("dsm.tif", epsg=2255)
Visualize DSM¶
In [19]:
Copied!
m = leafmap.Map()
m.add_raster("dsm.tif", palette="terrain", layer_name="DSM")
m
m = leafmap.Map()
m.add_raster("dsm.tif", palette="terrain", layer_name="DSM")
m
Out[19]:
Create DEM¶
In [20]:
Copied!
wbt.remove_off_terrain_objects("dsm.tif", "dem.tif", filter=25, slope=15.0)
wbt.remove_off_terrain_objects("dsm.tif", "dem.tif", filter=25, slope=15.0)
Out[20]:
0
Visualize DEM¶
In [21]:
Copied!
m.add_raster("dem.tif", palette="terrain", layer_name="DEM")
m
m.add_raster("dem.tif", palette="terrain", layer_name="DEM")
m
Out[21]:
Create CHM¶
In [22]:
Copied!
chm = wbt.subtract("dsm.tif", "dem.tif", "chm.tif")
chm = wbt.subtract("dsm.tif", "dem.tif", "chm.tif")
In [23]:
Copied!
m.add_raster("chm.tif", palette="gist_earth", layer_name="CHM")
m
m.add_raster("chm.tif", palette="gist_earth", layer_name="CHM")
m
Out[23]: