TransWikia.com

How to remove extra field from LAS file with laspy

Geographic Information Systems Asked by Kas on August 27, 2021

I have LAS files with a useless extra scalar field (named "tree") that I would like to remove.

I tried the following code to create a new LAS file, copy each field of the orginal LAS file into the new LAS file, except the unwanted field.

import laspy

LASInput = laspy.file.File('foo.las', mode = 'r')
LASOutput = laspy.file.File('bar.las', mode = "w", header = LASInput.header)

#Lets copy all the existing data from input file:
for dimension in LASInput.point_format:
    print(dimension.name)
    if not(dimension.name == 'tree'):
        LASOutput.writer.set_dimension(dimension.name, LASInput.reader.get_dimension(dimension.name))
    
LASOutput.close()
LASInput.close()

But the resulting LAS file still contains the field "tree". I guess the header also has to be modified so I tried this approach, creating a new header:

import laspy

LASInput = laspy.file.File('D:foo.las', mode = 'r')
LASOutput = laspy.file.File('D:bar.las', mode = "w", header = laspy.header.Header(1.3))

#Lets create the extra fields
NamesOutput = [i.name for i in LASOutput.point_format]
for dimension in LASInput.point_format:
    print(dimension.name)
    if not(dimension.name == 'tree'):
        if not(dimension.name in NamesOutput):
            LASOutput.define_new_dimension(name = dimension.name, data_type = 5, description ='blabla')

#Lets copy all the existing data from the imput file
for dimension in LASInput.point_format:
    print(dimension.name)
    if not(dimension.name == 'tree'):
        LASOutput.writer.set_dimension(dimension.name, LASInput.reader.get_dimension(dimension.name))

LASOutput.close()
LASInput.close()

print('ok')

This works, but I find it rather inelegant, and also I did not find a way to get the fromat of the fields in the input header, in my code I used data_type=5, which is likely to be incorrect. Is there not a way to duplicate the input header and then to discard the unwanted field from the header?

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP