From eef6e871101543da91c58349478bbe473ec460cb Mon Sep 17 00:00:00 2001 From: Bingqing Liu Date: Sun, 17 Nov 2024 00:24:53 -0600 Subject: [PATCH] update --- book/labs/lab7.ipynb | 598 +++++++++++++++++++++++++++++++++++++++ book/labs/supervised.tif | Bin 10483702 -> 0 bytes 2 files changed, 598 insertions(+) create mode 100644 book/labs/lab7.ipynb delete mode 100644 book/labs/supervised.tif diff --git a/book/labs/lab7.ipynb b/book/labs/lab7.ipynb new file mode 100644 index 0000000..ad1376f --- /dev/null +++ b/book/labs/lab7.ipynb @@ -0,0 +1,598 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "9a1ef642", + "metadata": {}, + "source": [ + "# Lab7 - LiDAR data analysis and visualization with whitebox and leafmap**\n", + "\n", + "LIDAR .LAS or .LAZ file can be downloaded from: \n", + "https://apps.nationalmap.gov/lidar-explorer/#/\n" + ] + }, + { + "cell_type": "markdown", + "id": "ca62d9a8", + "metadata": {}, + "source": [ + "\n", + "Create a new conda env to install required packages:\n", + "\n", + "```bash\n", + "conda create -n geo python\n", + "conda activate geo\n", + "conda install -c conda-forge mamba\n", + "mamba install -c conda-forge pygis\n", + "pip install laspy[lazrs]\n", + "```\n", + "\n", + "Uncomment the following line to install packages in Google Colab." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "1", + "metadata": {}, + "outputs": [], + "source": [ + "# !pip install leafmap" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "2", + "metadata": {}, + "outputs": [], + "source": [ + "# !pip install laspy[lazrs]" + ] + }, + { + "cell_type": "markdown", + "id": "3", + "metadata": {}, + "source": [ + "## Import libraries" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "id": "4", + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "c:\\Users\\C00553090\\AppData\\Local\\miniconda3\\envs\\hypercoast\\lib\\site-packages\\pandas\\core\\computation\\expressions.py:21: UserWarning: Pandas requires version '2.8.4' or newer of 'numexpr' (version '2.7.3' currently installed).\n", + " from pandas.core.computation.check import NUMEXPR_INSTALLED\n" + ] + } + ], + "source": [ + "import os\n", + "import leafmap\n", + "import whitebox" + ] + }, + { + "cell_type": "markdown", + "id": "5", + "metadata": {}, + "source": [ + "## Set up whitebox" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "6", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "0" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "wbt = whitebox.WhiteboxTools()\n", + "wbt.set_working_dir(os.getcwd())\n", + "wbt.set_verbose_mode(False)" + ] + }, + { + "cell_type": "markdown", + "id": "7", + "metadata": {}, + "source": [ + "## Download sample data" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "8", + "metadata": {}, + "outputs": [], + "source": [ + "filename = \"USGS_LPC_Barataria_and_Jean_Lafitte_LiDAR_15RYN5357.laz\"" + ] + }, + { + "cell_type": "markdown", + "id": "10", + "metadata": {}, + "source": [ + "## Read LAS/LAZ data" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "11", + "metadata": {}, + "outputs": [], + "source": [ + "# laz = leafmap.read_lidar(filename) is part of the leafmap library, \n", + "# and it is used to read LiDAR data (LAS or LAZ format) into a Python object for further processing and visualization.\n", + "laz = leafmap.read_lidar(filename)" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "12", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + ", 37891 points, 3 vlrs)>" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "laz" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "13", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'1.2'" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# LAS 1.0 is basic and lacks many modern features.\n", + "# LAS 1.4 supports additional attributes like full waveform and higher precision.\n", + "# LAS 1.2 is a versatile format and widely supported by LiDAR processing tools.\n", + "str(laz.header.version)" + ] + }, + { + "cell_type": "markdown", + "id": "14", + "metadata": {}, + "source": [ + "## Upgrade file version" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "15", + "metadata": {}, + "outputs": [], + "source": [ + "#Some modern tools (like GIS or analysis software) may require LAS 1.4 to work with advanced datasets. \n", + "\n", + "las = leafmap.convert_lidar(laz, file_version=\"1.4\")" + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "id": "16", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'1.4'" + ] + }, + "execution_count": 21, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "str(las.header.version)" + ] + }, + { + "cell_type": "markdown", + "id": "17", + "metadata": {}, + "source": [ + "## Write LAS data" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "18", + "metadata": {}, + "outputs": [], + "source": [ + "# The command leafmap.write_lidar(las, \"LA.las\") is used to save LiDAR data to a file in the specified LAS or LAZ format. Here's a detailed breakdown:\n", + "leafmap.write_lidar(las, \"LA.las\")" + ] + }, + { + "cell_type": "markdown", + "id": "19", + "metadata": {}, + "source": [ + "## Histogram analysis" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "20", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "0" + ] + }, + "execution_count": 10, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# The command wbt.lidar_histogram(\"LA.las\", \"histogram.html\") is part of the WhiteboxTools (WBT) library and is used to generate a histogram of the LiDAR data stored in the LAS file (LA.las). \n", + "# it generates an interactive histogram of the attribute distributions in the LiDAR file LA.las and saves it as an HTML file (histogram.html). \n", + "# This tool helps analyze and visualize the data's attributes, such as elevation or intensity, in a user-friendly format.\n", + "\n", + "wbt.lidar_histogram(\"LA.las\", \"histogram.html\")" + ] + }, + { + "cell_type": "markdown", + "id": "21", + "metadata": {}, + "source": [ + "## Visualize LiDAR data" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "id": "22", + "metadata": {}, + "outputs": [ + { + "data": { + "application/vnd.jupyter.widget-view+json": { + "model_id": "2b380492889141ad85ad0ebd71d57b56", + "version_major": 2, + "version_minor": 0 + }, + "text/plain": [ + "Widget(value='