LAZrs - LAZ Compression for Python
LAZrs provides Python bindings for `laz-rs`, a high-performance Rust implementation of LAZ (Laszip) compression and decompression. Its primary role is to serve as an optional, multi-threaded backend for other Python libraries, such as `laspy`, enabling them to efficiently read and write LAZ data. The library is currently at version 0.8.1, with releases tied to updates of its underlying Rust core.
Warnings
- gotcha LAZrs does not support points with waveforms. If your LAZ data includes waveform information, you will need to use an alternative backend like `laszip-python` (if available and compatible with your version of `laspy`) to ensure all data is correctly handled.
- gotcha LAZrs is primarily a low-level Rust-based compression engine and offers no direct high-level Python API for file reading or writing. It is consumed as a backend by libraries like `laspy`, meaning users interact with `lazrs` indirectly through the `laspy` API. Attempting to find high-level `lazrs.read()` or `lazrs.write()` functions will be unsuccessful.
- gotcha Explicit documentation on breaking changes between major versions (e.g., from 0.5.x to 0.8.x) specifically for the Python bindings is not readily available. While the underlying Rust library may have its own changelog, direct impact on the Python API for non-backend users is unclear.
Install
-
pip install lazrs -
pip install laspy[lazrs]
Quickstart
import laspy
import os
# Create a dummy LAS file for demonstration
header = laspy.header.Header()
header.point_format.scale = [0.01, 0.01, 0.01]
header.point_format.offset = [0, 0, 0]
points = laspy.create_points(point_count=100)
points.x = range(100)
points.y = range(100)
points.z = range(100)
input_filename = 'dummy.las'
output_filename = 'compressed.laz'
with laspy.open(input_filename, mode='w', header=header) as writer:
writer.write_points(points)
print(f"Created {input_filename} with {points.x.shape[0]} points.")
# --- Using lazrs as a backend via laspy to compress and decompress ---
# laspy will automatically detect and use lazrs if installed.
# Explicitly set laz_backend=laspy.compression.LazBackend.Lazrs if needed.
# Compress to LAZ using lazrs (implicitly by laspy)
with laspy.open(input_filename, mode='r') as infile:
with laspy.open(output_filename, mode='w', header=infile.header) as outfile:
outfile.write_points(infile.read_points())
print(f"Compressed to {output_filename} using lazrs backend.")
# Decompress from LAZ using lazrs (implicitly by laspy)
with laspy.open(output_filename, mode='r') as reader:
decompressed_points = reader.read_points()
print(f"Decompressed {decompressed_points.x.shape[0]} points from {output_filename}.")
assert decompressed_points.x.shape[0] == points.x.shape[0]
# Clean up dummy files
os.remove(input_filename)
os.remove(output_filename)
print("Cleaned up dummy files.")