0

How can I add data to a custom defined dimension when saving a laspy file (.las) in python?

Ok, more details: I read a standard .las file with attributes X,Y,Z,I. Then I do some calculations and want to save the result as a .las file with attributes X,Y,Z,I and the additional new dimension "my_dim". As long as I know the name of the new dimension, it's no problem to define and add data to it:

header = laspy.header.Header(point_format=2)
with laspy.file.File(FILEPATH, mode="w", header=header) as f:
    f.define_new_dimension(name="my_dim", data_type=5, description="My new dimension")
    f.my_dim = ...
    ...

However, I want to capsulate the saving process in an own function and provide additional dimensions as parameter extra_dims (as list of dicts, like [{"name": "my_dim", data_type: 5, ...}]. Now, I am still able to define the new dimension based on the parameter, but adding data is not possible. Here is the snippet:

def save(extra_dims):
    header = ...
    with laspy...:
        for dim in extra_dims:
            f.define_new_dimension(name = dim["name"], data_type = ...
        # Assign X,Y,Z,I
        ...

For adding data f[dim["name"]] = ... doesn't work (neither does f.dim["name"], obviously), I only get

TypeError: 'File' object does not support item assignment

Does anyone has an idea how to solve this, i.e. adding data to an attribute whose name is "hidden" in a parameter?

trippeljojo
  • 65
  • 1
  • 11

1 Answers1

0

Use pyhton's built in setattr() and getattr() attributes:

    ...
    # Assign X,Y,Z,I
    setattr(f, dim["name"], your_data)
oboy
  • 1