2020-03-10 01:52:12 +00:00
# TODO: Move to pylas when project migrates to python3
2022-11-29 21:15:07 +00:00
import laspy
2020-03-10 01:52:12 +00:00
import numpy as np
from . . point_cloud import PointCloud
def read_cloud ( point_cloud_path ) :
# Open point cloud and read its properties
2022-11-29 21:15:07 +00:00
las_file = laspy . read ( point_cloud_path )
header = las_file . header
2020-03-10 01:52:12 +00:00
2022-11-29 21:15:07 +00:00
x = las_file . x . scaled_array ( )
y = las_file . y . scaled_array ( )
z = las_file . z . scaled_array ( )
2020-03-10 01:52:12 +00:00
2022-11-29 21:15:07 +00:00
cloud = PointCloud . with_dimensions ( x , y , z , las_file . classification . array , las_file . red , las_file . green , las_file . blue )
2020-03-10 01:52:12 +00:00
# Return the result
return header , cloud
def write_cloud ( header , point_cloud , output_point_cloud_path , write_extra_dimensions = False ) :
# Open output file
2022-11-29 21:15:07 +00:00
output_las_file = laspy . LasData ( header )
2020-03-10 01:52:12 +00:00
if write_extra_dimensions :
2022-11-29 21:15:07 +00:00
extra_dims = [ laspy . ExtraBytesParams ( name = name , type = dimension . get_las_type ( ) , description = " Dimension added by Ground Extend " ) for name , dimension in point_cloud . extra_dimensions_metadata . items ( ) ]
output_las_file . add_extra_dims ( extra_dims )
2020-03-10 01:52:12 +00:00
# Assign dimension values
for dimension_name , values in point_cloud . extra_dimensions . items ( ) :
setattr ( output_las_file , dimension_name , values )
# Adapt points to scale and offset
[ x , y ] = np . hsplit ( point_cloud . xy , 2 )
2022-11-29 21:15:07 +00:00
output_las_file . x = x . ravel ( )
output_las_file . y = y . ravel ( )
output_las_file . z = point_cloud . z
2020-03-10 01:52:12 +00:00
# Set color
[ red , green , blue ] = np . hsplit ( point_cloud . rgb , 3 )
output_las_file . red = red . ravel ( )
output_las_file . green = green . ravel ( )
output_las_file . blue = blue . ravel ( )
# Set classification
2022-11-29 21:15:07 +00:00
output_las_file . classification = point_cloud . classification . astype ( np . uint8 )
2020-03-10 01:52:12 +00:00
2022-11-29 21:15:07 +00:00
output_las_file . write ( output_point_cloud_path )