0

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). I have this almost working code. The only thing is remaining to save the result points into a new las file.

import laspy
import laspy.file
import numpy as np
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=np.array([], dtype=float)
listy1=np.array([], dtype=float)
listz1=np.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=np.append(listx1, (newx))
                                listy1=np.append(listy1, (newy))
                                listz1=np.append(listz1, (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() 

I got this problem when I try to store the X values:

Traceback (most recent call last):
  File "C:\Users\Geri\Desktop\Sync\pythonlas\envisecond.py", line 48, in <module>
    outfile.X = [listx1]
  File "C:\Python27\lib\site-packages\laspy\file.py", line 277, in set_x
    self._writer.set_x(x)
  File "C:\Python27\lib\site-packages\laspy\base.py", line 1257, in set_x
    self.set_dimension("X", X)
  File "C:\Python27\lib\site-packages\laspy\base.py", line 1107, in set_dimension
    return(self._set_dimension(spec, new_dim))
  File "C:\Python27\lib\site-packages\laspy\base.py", line 1113, in _set_dimension
    self.data_provider._pmap["point"][spec.name] = value
ValueError: setting an array element with a sequence.
Gary
  • 209
  • 1
  • 3
  • 13

1 Answers1

1

In this line

outfile.X = [listx1]

you trying to assign outfile.X list consisting of one element - np.array listx1. And expected just np.array. Try to use following code

outfile.X = listx1
outfile.Y = listy1
outfile.Z = listz1
kvorobiev
  • 5,012
  • 4
  • 29
  • 35
  • It's working now thank you. The only thing if you could help me, it seems it saves the values as integer (e.g. 690314 ), but since these are coordinates I need those to save as float or similar (e.g. 690314.28245) – Gary Sep 29 '15 at 20:24
  • @Gary `print listx1` - could you check, here it prints a list with float values? – kvorobiev Sep 29 '15 at 20:32
  • It lists it as a float value but when I open the new file it only shows them as an integer – Gary Sep 29 '15 at 20:47
  • @Gary I am not completely sure, but looks like problem in `header`. Try to use `outfile = laspy.file.File("your/filepath", mode="w", header=inFile2 .header)` – kvorobiev Sep 29 '15 at 21:10