将索引附加到laspy文件(.las)

时间:2018-06-12 11:02:04

标签: python file shapefile laspy

我有两个文件,一个是esri shapefile(.shp),另一个是点云(.las)。

使用laspy和shapefile模块我已经设法找到.las文件的哪些点落在shapefile的特定多边形内。我现在想要做的是添加一个索引号,以便在两个数据集之间进行识别。所以例如落在多边形231内的所有点都应该得到数字231。

问题是,在编写.las文件时,我无法将任何内容附加到点列表中。我正在尝试的代码片段在这里:

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

我现在得到的错误是:AttributeError:'numpy.ndarray'对象没有属性'append'。我已经尝试了很多东西,包括np.append,但我真的不知道如何在las文件中添加任何内容。

非常感谢任何帮助!

1 个答案:

答案 0 :(得分:1)

有几种方法可以做到这一点。

Las文件具有分类字段,您可以将索引存储在该字段中

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

但是,如果Las文件的版本为<= 1.2,则分类字段只能存储[0,35]范围内的值,但是您可以使用'user_data'字段来保存[0,255]范围内的值

或者,如果您需要存储大于255的值/需要一个单独的字段,则可以定义新的维度(see laspy's doc on how to add extra dimensions)。 您的代码应该接近这样

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()