laspy

2.7.0 · active · verified Tue Apr 14

Laspy is a native Python library for reading, modifying, and creating ASPRS LAS and LAZ (compressed) LIDAR files, supporting specifications 1.0 through 1.5. It provides a Pythonic API via NumPy arrays for efficient point cloud data manipulation. The library is actively maintained with frequent releases, typically several times a year.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to create a simple LAS file from scratch, read an existing LAS file using `laspy.open()` and `LasData.read()`, access point data and header attributes, and filter points. It also shows how to write new or modified `LasData` objects to a new file. Note the distinction between scaled (lowercase) and raw (uppercase) dimension access.

import laspy
import numpy as np
import os

# Create a dummy LAS file for demonstration
header = laspy.LasHeader(point_format=3, version="1.4")
header.offsets = np.array([0, 0, 0])
header.scales = np.array([0.01, 0.01, 0.01])

las_data = laspy.LasData(header)

n_points = 100
las_data.x = np.random.rand(n_points) * 100
las_data.y = np.random.rand(n_points) * 100
las_data.z = np.random.rand(n_points) * 50
las_data.classification = np.random.randint(0, 5, n_points)

output_filename = "output.las"
las_data.write(output_filename)
print(f"Created {output_filename} with {len(las_data.points)} points.")

# Read a LAS/LAZ file
try:
    with laspy.open(output_filename) as fh:
        print(f"Reading {fh.header.point_count} points from {output_filename}")
        las = fh.read()

        # Access point data (scaled and raw)
        print(f"X (scaled): {las.x[:5]}") # Scaled coordinates
        print(f"X (raw): {las.X[:5]}")   # Raw integer coordinates
        print(f"Classification: {las.classification[:5]}")

        # Access header information
        print(f"LAS Version: {las.header.version}")
        print(f"Point Format ID: {las.header.point_format.id}")
        print(f"Offset: {las.header.offsets}")
        print(f"Scale: {las.header.scales}")

        # Filter points (e.g., classification == 2)
        ground_points = las.points[las.classification == 2]
        print(f"Number of ground points (classification=2): {len(ground_points)}")

        # Create a new LAS file with filtered points
        new_las = laspy.create(point_format=las.header.point_format, file_version=las.header.version)
        new_las.points = ground_points
        filtered_output_filename = "filtered_output.las"
        new_las.write(filtered_output_filename)
        print(f"Created {filtered_output_filename} with {len(new_las.points)} filtered points.")

finally:
    # Clean up dummy files
    if os.path.exists(output_filename):
        os.remove(output_filename)
    if os.path.exists(filtered_output_filename):
        os.remove(filtered_output_filename)

view raw JSON →