python 点云las、laz文件的读取、写入、压缩

1. python las读取与写入

用laspy库: https://pypi.org/project/laspy/

获取las、laz的规格、点数据格式,点数据的具体字段名称,具体的点数据
解析las、laz头文件区、点数据区的所有字段

参考:https://readthedocs.org/projects/laspy/
参考:https://pylas.readthedocs.io/en/latest/installation.html

pip install laspy
from laspy.file import File

def main():
    f = File("E:/dataTes/1001140020191217.las", mode='r')

    # 查看点云的点格式及字段名称
    print('\nPoint Of Data Format: ', f.header.data_format_id)
    print("\tExamining Point Format: ", end=" ")
    for spec in f.point_format:
        print(spec.name, end=", ")

    print('\noffset: ', f.header.offset)  # 偏移量
    print('scale: ', f.header.scale)  # 比例因子
    print('min: ', f.header.min)  # x、y、z 的最小值
    print('max: ', f.header.max)  # x、y、z 的最大值
    print('file_signature: ', f.header.file_signature)  # 文件标识
    print('Point Of Data Format: ', f.header.data_format_id)  # 点格式
    print('data_record_length: ', f.header.data_record_length)  # 点个数
    print('FileCreateDay+Year: ', f.header.date)
    print()

    print('f.x: ', f.x)
    print('f.y: ', f.y)
    print('f.z: ', f.z)
    print('f.intensity: ', f.intensity)
    print('f.gps_time: ', f.gps_time)
    print('f.raw_classification: ', f.raw_classification)
    print()
    # print('f.user_data: ', f.user_data)
    # print('f.flag_byte: ', f.flag_byte)
    # print('f.Color: ', f.red, f.green, f.blue)

    # print('file_source_id: ', f.header.file_source_id)
    # print('Major_Minor version: ', f.header.version, str(f.header.version_major) + '.' + str(f.header.version_minor))
    # print('Generation Software: ', f.header.software_id)
    # print('system_id: ', f.header.system_id)
    # print('Header Size: ', f.header.header_size)
    # print('file_global_encoding: ', f.header.global_encoding)
    # print('gps_time_type: ', f.header.gps_time_type)
    # print('guid: ', f.header.guid)
    print()

    # print('f.edge_flight_line: ', f.edge_flight_line)
    # print('f.return_num: ', f.return_num)
    # print('f.classification: ', f.classification)
    # print('f.scan_angle_rank: ', f.scan_angle_rank)
    # print('f.scan_dir_flag: ', f.scan_dir_flag)
    # print('f.num_returns: ', f.num_returns)
    # print('pt_src_id: ', f.pt_src_id)

    f.close()

if __name__ == "__main__":
    main()

2. las的压缩,.las转.laz,.laz转.las

las点云的无损压缩:将.las转为.laz,压缩率可达15%,空间占用减少85%;

需要俩个模块

pip install pylas
pip install lazrs

参考:https://gis.stackexchange.com/questions/332366/reading-laz-file-in-python-directly
pylas官方文档: https://pylas.readthedocs.io/en/latest/installation.html

import pylas
import time

# laz 转 las
end0 = time.time()
las = pylas.read('E:/dataTes/1001117.laz')
las = pylas.convert(las)
las.write('E:/dataTes/1001117_laz2las.las')
end1 = time.time()
print("laz2las 耗时:%.2f秒" % (end1 - end0))
print('------------end1------------')

# las 转 laz
las1 = pylas.read('E:/dataTes/1001147.las')
las1 = pylas.convert(las1)
las1.write('E:/dataTes/1001147_las2laz.laz')
end2 = time.time()
print("las2laz 耗时:%.2f秒" % (end2 - end1))
print('---------end----------')

3. laspy读取.laz

参考:https://stackoverflow.com/questions/49500149/laspy-cannot-find-laszip-when-is-installed-from-source-laszip-is-in-path

有用到(lasTools的laszip.exe以及dll)
windows下需要往Path环境变量里添加自己下载的lasTools的bin目录;
lasTools下载: https://github.com/m-schuetz/LAStools

from laspy.file import File
import os

def main():
    # 最重要的后边要有 E:\Las\LAStools\bin; lasTools中laszip.exe的路径
    print(os.environ["PATH"])
    # E:\python\lib\site-packages\pywin32_system32;C:\Program Files (x86)\Common Files\Oracle\Java\javapath;C:\ProgramData\Oracle\Java\javapath;D:\jdk181\bin;D:\hadoop-common-2.2.0\bin;C:\Windows\vrv\common\vSSLFltSvc;D:\apache-maven-3.5.3\bin;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\WINDOWS\System32\OpenSSH\;D:\dnProject\scala-2.11.8\scala-2.11.8\bin;D:\svnSsrver\bin;D:\apache-maven-3.5.3\bin;E:\PCL 1.8.1\bin;E:\PCL 1.8.1\3rdParty\OpenNI2\Tools;E:\PCL 1.8.1\3rdParty\VTK\bin;E:\python\Scripts\;E:\python\;C:\Users\sunmeina\AppData\Local\Microsoft\WindowsApps;E:\PCL 1.8.1\bin;C:\Users\sunmeina\AppData\Local\BypassRuntm;E:\cmake\bin;E:\python\lib\site-packages\numpy\.libs;E:\Las\LAStools\bin;

    f = File("E:/dataTes/1001140020191217_las2laz.laz", mode='r')

    # 查看点云的点格式及字段名称
    print('\nPoint Of Data Format: ', f.header.data_format_id)
    print("\tExamining Point Format: ", end=" ")
    for spec in f.point_format:
        print(spec.name, end=", ")

    print('\noffset: ', f.header.offset)  # 偏移量
    print('scale: ', f.header.scale)  # 比例因子
    print('min: ', f.header.min)  # x、y、z 的最小值
    print('max: ', f.header.max)  # x、y、z 的最大值
    print('file_signature: ', f.header.file_signature)  # 文件标识
    print('Point Of Data Format: ', f.header.data_format_id)  # 点格式
    print('data_record_length: ', f.header.data_record_length)  # 点个数
    print('FileCreateDay+Year: ', f.header.date)
    print()

    print('f.x: ', f.x)
    print('f.y: ', f.y)
    print('f.z: ', f.z)
    print('f.intensity: ', f.intensity)
    print('f.gps_time: ', f.gps_time)
    print('f.raw_classification: ', f.raw_classification)
    print()
    f.close()

if __name__ == "__main__":
    main()

4. pylas读取.laz

import pylas

def lazread(path):
    print('\n\n------------\n\n')
    laz = pylas.read(path)
    print(laz)
    print(laz.header)
    print(laz.point_format)
    print(laz.header.version)
    print(laz.point_format.id)

    print(laz.header.file_signature)
    print('offset: ', laz.header.offset_to_point_data)
    print('version_major: ', laz.header.version_major)
    print('version_minor: ', laz.header.version_minor)
    print('point_count: ', laz.header.point_count)
    print('legacy_point_count: ', laz.header.legacy_point_count)
    print('file_source_id: ', laz.header.file_source_id)
    print('global_encoding: ', laz.header.global_encoding)
    print('date: ', laz.header.date)
    print('data_record_length: ', laz.points_data.point_size)
    print('point_size: ', laz.header.point_size)
    print('point_data_record_length: ', laz.header.point_data_record_length)
    print('uuid: ', laz.header.uuid)
    print('are_points_compressed: ', laz.header.are_points_compressed)
    print('offset: ', laz.header.offsets)
    print('scale: ', laz.header.scales)
    print('min: ', laz.header.mins)
    print('max: ', laz.header.maxs)

    print('-------\n')
    print(laz.point_format.dimension_names)
    print('laz.x: ', laz.x)
    print('laz.y: ', laz.y)
    print('laz.z: ', laz.z)
    print('laz.intensity: ', laz.intensity)
    print('laz.gps_time: ', laz.gps_time)

    print('laz.points: ', laz.points)
    print('laz.points_data: ', laz.points_data)

    print(laz.points_data.point_format.id)
    print(laz.points_data.array)
    print(laz.points_data.point_size)

def main():
    lazread('E:/Las/1001217_las2laz.laz')
    print('-------------endl-----------')

if __name__ == "__main__":
    main()

以上测试均在windows10中进行,都ok
centos服务器上 方式2中读取laz方式的可以支持;方式3中的不支持,报错如下:

raise ValueError(“Unable to read compressed file!”) ValueError: Unable to read compressed file!

你可能感兴趣的:(Python,OpenCV)