1

What is the correct way to add RGB values to points made from scratch?

Laspy is working great for me so far. I want to add RGB values to synthetic points that I've generated. I am using 2 numpy arrays. The first stores all of the XYZ (floats) values. The second array stores the point data record (intensity, classification; Red, Green, Blue). There are only 1200 points and they classify fine. When I comment out the "RGB" the file is generated.

Code:

def makeLASfile(finishPoints,LASheader, valuesP):
    #From laspy
    print("Your Making LAS file")

    outfile = laspy.file.File("C:\\Users\\Cary Hutchinson\\Documents\\Programming\\Python Scripts\\Laspy\\Circleoutput.las", mode="w", header=LASheader)
    outfile.header.offset = LASheader.offset
    outfile.header.scale = [1.0,1.0,1.0]

    outfile.x = finishPoints[ :,0]
    outfile.y = finishPoints[ :,1]
    outfile.z = finishPoints[ :,2]

    outfile.intensity=valuesP[ :,0] #intensity 
    outfile.flag_byte=valuesP[ :,1] #flag_byte
    outfile.Raw_Classification = valuesP[ :,2] #classification
    outfile.user_data = valuesP[ :,3] #user_data
    outfile.pt_src_id = valuesP[ :,4] #pt_src_id
    outfile.Red = valuesP[ :,5] #red
    #outfile.green=valuesP[ :,6] #green
    #outfile.blue = valuesP[ :,7] #blue 

    outfile.close()

The issue is that I have something not formatted correctly for the RGB and I get the following exception:

> Traceback (most recent call last):
  File "C:\Program Files\Python37\lib\site-packages\laspy\base.py", line 1221, in set_dimension
    spec = self.point_format.lookup[name]
KeyError: 'red'

    self.set_dimension("red", red)
  File "C:\Program Files\Python37\lib\site-packages\laspy\base.py", line 1225, in set_dimension
    "not found.")
laspy.util.LaspyException: Dimension: rednot found.*
  • Please edit your question to include an actual question. As it stands, it's just a statement of what you're working on. – csk Sep 27 '18 at 20:24
2

The header needs to be set with a point format that supports RGB colors, see: https://pythonhosted.org/laspy/tut_background.html. For LAS 1.2, the minimum point format for color is 2:

header = laspy.header.Header(point_format=2) # LAS point format 2 supports color

with laspy.file.File(output_path, mode="w", header=header) as lasfile:

    lasfile.header.offset = offset
    lasfile.header.scale = [1.0, 1.0, 1.0]

    lasfile.x = xs
    lasfile.y = ys
    lasfile.z = zs

    lasfile.red = reds
    lasfile.green = greens
    lasfile.blue = blues

By the way, the color calls are property attributes in laspy, so calls to "red" and "set_red" are equivalent.

0

The issue was that I was not using the set method from laspy writer. I modified the Outfile.red to outfile.set_red.

This makes a nice las 1.2 file from scratch.

Working code:

def makeLASfile(finishPoints,LASheader, valuesP):
#From laspy
print("Your Making LAS file")

outfile = laspy.file.File("C:\\Users\\Cary Hutchinson\\Documents\\Programming\\Python Scripts\\Laspy\\Circle.las", mode="w", header=LASheader)
#outfile.header.offset = LASheader.offset
outfile.header.scale = [1.0,1.0,1.0]
#outfile.header.header_size = LASheader.header_size  

outfile.x = finishPoints[ :,0]
outfile.y = finishPoints[ :,1]
outfile.z = finishPoints[ :,2]

outfile.intensity=valuesP[ :,0] #intensity 
outfile.flag_byte=valuesP[ :,1] #flag_byte
outfile.Raw_Classification = valuesP[ :,2] #classification
outfile.user_data = valuesP[ :,3] #user_data
outfile.pt_src_id = valuesP[ :,4] #pt_src_id
outfile.set_red = valuesP[ :,5] #red
outfile.set_green=valuesP[ :,6] #green
outfile.set_blue = valuesP[ :,7] #blue 

outfile.close()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.