LASzip Python Bindings
The `laszip` library provides Python bindings for the LASzip C/C++ library, enabling efficient compression and decompression of LAZ (compressed LAS) point cloud files. It leverages `pybind11` for seamless integration. The current version is 0.3.0, and releases occur as needed for bug fixes and feature enhancements, typical for a pre-1.0 library.
Common errors
-
ModuleNotFoundError: No module named 'laszip'
cause The `laszip` library is not installed in your current Python environment.fixRun `pip install laszip` to install the library. -
OSError: [Errno 2] No such file or directory: 'your_non_existent_file.laz'
cause You are trying to open a LAZ file that does not exist at the specified path.fixVerify that the file path is correct and the file exists. Use an absolute path or ensure your working directory is correct. -
RuntimeError: error creating writer: ... (followed by details like 'invalid header')
cause The `LaszipHeader` object passed to `LaszipWriter` is malformed or contains invalid values (e.g., `point_data_format` not set, incorrect `point_data_record_length`).fixEnsure all required `LaszipHeader` fields are correctly populated and consistent with the LAS format specification before creating the writer. Refer to the quickstart example for a minimal valid header. -
AttributeError: 'LaszipReader' object has no attribute 'read_header'
cause You're attempting to access a method that either doesn't exist or has changed its name. The header is typically accessed as an attribute (`reader.header`) after opening the reader, not via a `read_header()` method.fixAccess the header directly via the `reader.header` attribute after initializing `LaszipReader`. For example: `with laszip.LaszipReader('file.laz') as reader: my_header = reader.header`.
Warnings
- breaking As a pre-1.0.0 library, `laszip`'s API may undergo breaking changes without a major version increment. Always consult the GitHub repository for the latest API documentation and examples when upgrading.
- gotcha Building `laszip` from source (e.g., if pre-compiled wheels are not available for your platform/Python version) requires C++ build tools and `pybind11` development headers. This can lead to complex build errors.
- gotcha When processing extremely large point clouds, be mindful of memory consumption if you attempt to load all points into memory simultaneously. `LaszipReader` is designed for iterative reading.
Install
-
pip install laszip
Imports
- LaszipReader
from laszip import LaszipReader
- LaszipWriter
from laszip import LaszipWriter
- LaszipHeader
from laszip import LaszipHeader
- LaszipPoint
from laszip import LaszipPoint
Quickstart
import laszip
import os
import tempfile
# Create a temporary file path
temp_laz_path = os.path.join(tempfile.gettempdir(), "test_laszip_quickstart.laz")
try:
# 1. Prepare a minimal header for writing
header = laszip.LaszipHeader()
header.major_version = 1
header.minor_version = 4
header.point_data_format = 6 # Standard LAS 1.4, Point Data Record Format 6
header.point_data_record_length = 36 # bytes for format 6
header.number_of_point_records = 1
header.X_offset = 0.0; header.Y_offset = 0.0; header.Z_offset = 0.0
header.X_scale_factor = 0.01; header.Y_scale_factor = 0.01; header.Z_scale_factor = 0.01
header.min_x = 100.0; header.max_x = 100.0
header.min_y = 200.0; header.max_y = 200.0
header.min_z = 300.0; header.max_z = 300.0
# Prepare a dummy point
point = laszip.LaszipPoint()
point.X = 10000; point.Y = 20000; point.Z = 30000 # Scaled by 0.01
point.classification = 1
point.intensity = 100
# 2. Write a dummy LAZ file
print(f"Writing dummy LAZ to {temp_laz_path}")
with laszip.LaszipWriter(temp_laz_path, header_in=header) as writer:
writer.write_point(point)
# 3. Read the LAZ file back
print(f"Reading from {temp_laz_path}")
with laszip.LaszipReader(temp_laz_path) as reader:
read_header = reader.header
print(f" Read header - Point count: {read_header.number_of_point_records}")
read_point = reader.read_point()
print(f" Read point: X={read_point.X}, Y={read_point.Y}, Z={read_point.Z}")
except Exception as e:
print(f"An error occurred: {e}")
finally:
# 4. Clean up the temporary file
if os.path.exists(temp_laz_path):
os.remove(temp_laz_path)
print(f"Cleaned up {temp_laz_path}")