-1

I have this little code for manipulating lidar points in a Las file. The goal is to take 1 point from file 1 and look for the closest point in the file 2. Then create a new point which using the coordinates of the points and then save it. I encountered 2 problem so far, which are explained below:

import laspy
import laspy.file
import liblas
from liblas import header
h = header.Header()

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

t=0
for points in inFile3.points:
    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:          
                                f = liblas.file.File("C:\\Users\\Geri\\Desktop\\Sync\\Sync\\pythonlas\\mapperclip\\proba.las",mode='w', header= h)
                                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)
                                pt = liblas.point.Point(newx, newy, newz)

The problem is here after this line. I want to save the newx,y,z as the X Y coordinates and z value for the new points in a new las file.

Getting the following error:

ctypes.Argumenterror: argument 1: : wrong type

                                f.write(pt)
                                f.close()
                                print t
                                print newx
                                print newy
                                print newz
                                print inFile2.y[n]
                                print inFile2.z[n]                                
                                print inFile3.z[t]
                                n+=1
                                q+=1
                                t+=1
            else:
                n+=1
        if q>0:            
            z+=1
        else:
            p+=0.1

When I take out the attempt to write the file from the code then I got the following: (the script runs 415 times succesfully)

Traceback (most recent call last):
   File "........
       newx = inFile3.x[t]-((inFile3.x[t]-inFile2.x[n])/2)

IndexError: index 416 is out of bounds for axis 0 with size 416

Gary
  • 209
  • 1
  • 3
  • 13
  • Fun thing about Python: `if ax > xmin and ax < xmax and ay < ymax and ay > ymin` is probably clearer written as `if xmin < ax < xmax and ymin < ay < ymax`. –  Sep 24 '15 at 06:45
  • Can you show the complete traceback, instead of just a one-liner? In particular, which function call is causing the ArgumentError? Because then you can look up what type of argument the function expects, and use the correct argument instead. –  Sep 24 '15 at 06:46
  • When you iterative over the points in a file (`for point in inFile3.points`), that point is then available in the `point` variable. Instead of then using indices (`n`, `t`), you could just compare the coordinate values of those `point` variables themselves. This would eliminate the possibility of `IndexErrors` in that area. – Pete Gadomski Sep 24 '15 at 14:02
  • You'll have to look up 1/ what the types are of newx, newy and newz, and 2/ what input types liblas.point.Point takes as argument. If you solve that, you can get rid of the ArgumentError. –  Sep 24 '15 at 22:39
  • For the argument, error it turned out that in the inFile3.points I only have 415 points. So there is an error in the code where it tries to run the loop one more time when it should not – Gary Sep 29 '15 at 03:23

1 Answers1

1

So here is what I got so far:

For the first problem I simply left out the liblas module and used the laspy for saving the created new file.

For the second problem I replaced the

for points in inFile3.points:

with

 while t < 415:
Gary
  • 209
  • 1
  • 3
  • 13