能否用PDAL获取各多边形最高点?无点多边形输出问题咨询
解决PDAL批量提取多边形最高点时空多边形无输出的问题
问题场景
现有PDAL处理管线用于批量提取多个多边形内的最高点,管线代码如下:
[ 'points.las', {'type': 'filters.crop', 'polygon': polygons}, { "type": "filters.outlier", "method": "statistical" }, { "type": "filters.range", "limits": "Classification![7:7]" }, {'type': 'filters.locate', "dimension": "Z", "minmax": 'max'}, {"type": "writers.text", "precision": 8, "filename": 'output.csv'}, ]
其中polygons为WKT格式的多边形长列表。当前问题:若某多边形内无符合条件的点,该多边形不会在output.csv中生成任何记录,导致无法将结果与原多边形一一对应。
解决方案
方法1:循环处理单个多边形,手动补全空结果
PDAL批量裁切空多边形时会直接跳过,因此可以遍历每个多边形单独构建管线处理,手动为空多边形添加默认记录:
import pdal import pandas as pd import json # 假设polygons是包含唯一ID和WKT的列表,格式为 [(poly_id_1, wkt_1), (poly_id_2, wkt_2), ...] results = [] for poly_id, wkt in polygons: pipeline_config = [ 'points.las', {'type': 'filters.crop', 'polygon': wkt}, {"type": "filters.outlier", "method": "statistical"}, {"type": "filters.range", "limits": "Classification![7:7]"}, {'type': 'filters.locate', "dimension": "Z", "minmax": 'max'}, ] pipeline = pdal.Pipeline(json.dumps(pipeline_config)) point_count = pipeline.execute() if point_count > 0: # 提取有效结果并关联多边形ID df = pd.DataFrame(pipeline.arrays[0]) df['poly_id'] = poly_id results.append(df) else: # 空多边形添加默认记录,可自定义默认值(如Z设为None或特定标记值) results.append(pd.DataFrame({'poly_id': [poly_id], 'Z': [None]})) # 合并所有结果并导出 final_result = pd.concat(results, ignore_index=True) final_result.to_csv('output.csv', index=False, precision=8)
此方法逻辑直观,可灵活自定义空多边形的默认值,确保每个多边形都有对应记录。
方法2:纯PDAL管线实现,通过分支处理补全空结果
利用PDAL的filters.crop拆分功能+filters.conditional分支判断,为空多边形生成预设点:
import pdal import json pipeline_config = [ 'points.las', # 按多边形拆分点云,每个多边形对应一个点云分块 {'type': 'filters.crop', 'polygon': polygons, 'split': True}, {"type": "filters.outlier", "method": "statistical"}, {"type": "filters.range", "limits": "Classification![7:7]"}, # 分块判断:空分块添加默认点,非空分块提取最高点 { "type": "filters.conditional", "condition": "$numpoints == 0", "true": [ # 自定义空多边形的默认值,@id对应分块的多边形索引 {"type": "filters.assign", "value": "X=0,Y=0,Z=-9999,poly_id=@id"} ], "false": [ {'type': 'filters.locate', "dimension": "Z", "minmax": 'max'}, {"type": "filters.assign", "value": "poly_id=@id"} ] }, {"type": "writers.text", "precision": 8, "filename": 'output.csv', "format": "csv"} ] pipeline = pdal.Pipeline(json.dumps(pipeline_config)) pipeline.execute()
此方法无需额外Python循环逻辑,依赖PDAL原生功能实现,但需确保使用PDAL 2.0及以上版本(支持filters.conditional分支语法)。
关键注意事项
- 无论哪种方法,都需要给每个多边形添加唯一标识(如ID),才能保证结果与原多边形正确关联;
- 空多边形的默认值可根据需求调整(如用
NaN、特定标记值-9999等)。
内容的提问来源于stack exchange,提问作者nickponline
相关产品推荐
相关产品推荐

