Search code examples
pythonnumpylidar

Attribute error float object has no attribute 'append'


I try to make this code to open LAS files make a change and then save the result in a new LAS file. The las file contains points which have coordinates (X and Y) and values (like Z for elevation). Unfortunately the code works if I don't put the saving part into it, but when I do like below, I got the following error:

Traceback (most recent call last):
  File "C:\Users\Geri\Desktop\Sync\pythonlas\envisecond.py", line 33, in <module>
    listx1=p.append(listx1, float[newx])
AttributeError: 'float' object has no attribute 'append'
enter code here

So far I know I need an array to save the values to the las file at the end of the code:

import laspy
import laspy.file
import numpy as p
header = laspy.header.Header()
inFile2 = laspy.file.File("C:\\Users\\Geri\\Desktop\\Sync\\pythonlas\\mapperclip\\2clip.las", mode = "r")
inFile3 = laspy.file.File("C:\\Users\\Geri\\Desktop\\Sync\\pythonlas\\mapperclip\\3clip.las", mode = "r")
point_records = inFile2.points
point_records = inFile3.points

t=0
listx1=p.array([], dtype=float)
listy1=p.array([], dtype=float)
listz1=p.array([], dtype=float)
while t < 415:
    z=0
    q=0
    p=0.1
    while z==0:

        xmin=inFile3.x[t]-p
        ymin=inFile3.y[t]-p
        xmax=inFile3.x[t]+p
        ymax=inFile3.y[t]+p
        n=0
        for points in inFile2.points:
            ax=inFile2.x[n]
            ay=inFile2.y[n]
            if ax > xmin and ax < xmax and ay < ymax and ay > ymin: 

                newx = [inFile3.x[t]-((inFile3.x[t]-inFile2.x[n])/2)]
                newy = [inFile3.y[t]-((inFile3.y[t]-inFile2.y[n])/2)]
                newz = [inFile3.z[t]-((inFile3.z[t]-inFile2.z[n])/2)]
                listx1=p.append(listx1, float[newx])
                listy1=p.append(listy1, float[newy])
                listz1=p.append(listz1, float[newz])
                print listx1
                print n
                n+=1
                q+=1
                t+=1

            else:
                n+=1
        if q>0:            
            z+=1
        else:
            p+=0.1
outfile = laspy.file.File("C:\\Users\\Geri\\Desktop\\Sync\\pythonlas\\mapperclip\\output2.las", mode="w", header=header)
outfile.X = [listx1]
outfile.Y = [listy1]
outfile.Z = [listz1]
outfile.close() 

Solution

  • You set p to a float at the start of your outer while loop:

    p=0.1
    

    That masks the numpy import at the top:

    import numpy as p
    

    so within the while loop p is no longer the module, it is a float object and the call p.append() calls will fail.

    Use a different name for the module or the float value.