1

I have a text file, each line in the text file has X,Y and Z coordinates eg:

551124.5841823021 470778.1953796738 318.1167521614109

I want to read the data and export it to LAS format, by using laspy. The code I use, inspired from :

header = laspy.header.Header()

with open(input_file, "r") as file:
     lines = list(line for line in (l.strip() for l in file) if line)
output_X = []
output_Y = []
output_Z = []
for line in lines:
    splited = line.split()
    output_X.append(float((splited[0])))
    output_Y.append(float((splited[1])))
    output_Z.append(float((splited[2])))
output_file = input_file.rsplit(".", 1)[0]+"_changed.las"
outfile = laspy.file.File(r"{}".format(output_file),
                          mode="w", header=header)

outfile.X = numpy.array(output_X)
outfile.Y = numpy.array(output_Y)
outfile.Z = numpy.array(output_Z)

outfile.close()

The values I expect to find should be of the same type as those from txt file, however, if I read the file I get:

[((551124, 470778, 318, 0, 0, 0, 0, 0, 0),) ((551124, 470779, 318, 0, 0, 0, 0, 0, 0),) ((551124, 470780, 318, 0, 0, 0, 0, 0, 0),) ... ((551438, 470707, 315, 0, 0, 0, 0, 0, 0),) ((551438, 470708, 315, 0, 0, 0, 0, 0, 0),) ((551438, 470709, 315, 0, 0, 0, 0, 0, 0),)]

I want the entries to be of type float, with decimal. I know it should be possible, since I have LAS files received from a third party, that have decimals. Should I be setting the scale for the LAS file on export? Also, if I open the output file in a software that reads LAS files, ArcGIS, points are not represented. What am I doing wrong?

0
1

You should be using lower-case letters for the dimensions when accessing them as floats, e.g. outfile.x instead of outfile.X.

The capitalized version is used to access the "un-scaled" dimension, which are the integers that are stored internally. Lower case should apply the scaling for you when accessing them. In your case, you're actually assigning floats to the integer fields, which is immediately truncating them.

From the tutorial in the docs:

Laspy can actually scale the x, y, and z dimensions for you. Upper case dimensions (las_file.X, las_file.Y, las_file.Z) give the raw integer dimensions, while lower case dimensions (las_file.x, las_file.y, las_file.z) give the scaled value. Both methods support assignment as well, although due to rounding error assignment using the scaled dimensions is not reccomended.

5
  • Now i get this error: RuntimeWarning: divide by zero encountered in true_divide self.set_dimension("X", np.round((X - self.header.offset[0])/self.header.scale[0])) – Paul Nicoara Dec 7 '20 at 17:34
  • 1
    Set the scale and offset first, as per an answer in the thread you linked: gis.stackexchange.com/a/208803/64255 – mikewatt Dec 7 '20 at 23:12
  • Print your splited = line.split() output to see if you still have decimals there. – Cary H Dec 11 '20 at 16:25
  • I tested, I still have decimals – Paul Nicoara Dec 13 '20 at 12:24
  • @mikewatt It worked, thank you! – Paul Nicoara Dec 13 '20 at 12:35

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.