Search code examples
pythonfileshapefilelaspy

appending an index to laspy file (.las)


I have two files, one an esri shapefile (.shp), the other a point cloud (.las).

Using laspy and shapefile modules I've managed to find which points of the .las file fall within specific polygons of the shapefile. What I now wish to do is to add an index number that enables identification between the two datasets. So e.g. all points that fall within polygon 231 should get number 231.

The problem is that as of yet I'm unable to append anything to the list of points when writing the .las file. The piece of code that I'm trying to do it in is here:

outFile1 = laspy.file.File("laswrite2.las", mode = "w",header = inFile.header)
outFile1.points = truepoints
outFile1.points.append(indexfromshp)
outFile1.close()

The error I'm getting now is: AttributeError: 'numpy.ndarray' object has no attribute 'append'. I've tried multiple things already including np.append but I'm really at a loss here as to how to add anything to the las file.

Any help is much appreciated!


Solution

  • There are several ways to do this.

    Las files have classification field, you could store the indexes in this field

    las_file = laspy.file.File("las.las", mode="rw")
    las_file.classification = indexfromshp
    

    However if the Las file has version <= 1.2 the classification field can only store values in the range [0, 35], but you can use the 'user_data' field which can hold values in the range [0, 255].

    Or if you need to store values higher than 255 / you need a separate field you can define a new dimension (see laspy's doc on how to add extra dimensions). Your code should be close to something like this

    outFile1 = laspy.file.File("laswrite2.las", mode = "w",header = inFile.header)
    # copy fields
    for dimension in inFile.point_format:
        dat = inFile.reader.get_dimension(dimension.name)
        outFile1.writer.set_dimension(dimension.name, dat)
    
    outFile1.define_new_dimension(
        name="index_from_shape",
        data_type=7, # uint64_t
        description = "Index of corresponding polygon from shape file"
     )
    outFile1.index_from_shape = indexfromshp
    outFile1.close()