将高程等值线导出到 DXF 时连接线错误

问题描述 投票:0回答:1

我正在寻求帮助以解决从数据框中导出轮廓线的问题。

我的目标是获得一个 DXF 文件,以如下所示的方式表示高程等高线: 所需输出

但是,我得到了这个: 实际输出(错误连接的线)

这就是输入格式:

X(东距) Y(北移) Z(海拔高度,单位:米)
312581.9 6457327.5 84.88
... ... ...

这是我的代码:

import numpy as np
import matplotlib.pyplot as plt
from scipy.interpolate import griddata
import ezdxf

# See input format in the table above. 

# Extract the x, y, z columns
x = cut_out_df['x'].values
y = cut_out_df['y'].values
z = cut_out_df['z'].values

# Create a grid to interpolate the data
grid_x, grid_y = np.mgrid[min(x):max(x):200j, min(y):max(y):200j]  # Grid with 200x200 resolution
grid_z = griddata((x, y), z, (grid_x, grid_y), method='linear')  # Interpolation

# Create the contour lines
contour_levels = np.linspace(np.min(grid_z), np.max(grid_z), 10)  # Define contour levels
contour_data = ax.contour(grid_x, grid_y, grid_z, levels=contour_levels)

# Export contour data to DXF
doc = ezdxf.new()

# Create a new model space in the DXF document
msp = doc.modelspace()

# Iterate through the contour collections
for level, collection in zip(contour_levels, contour_data.collections):
    # Get the paths of the contour lines
    for path in collection.get_paths():
        vertices = path.vertices
        # Create a polyline for each contour line
        polyline = msp.add_lwpolyline(vertices, close=False)
        # Add a layer or set a specific attribute to identify the contour level
        polyline.dxf.layer = f'Contour_{level:.2f}'
        
# Save the DXF document
doc.saveas('contours.dxf')

我认为

close=False
可以解决问题,但后来意识到可以有多个相同海拔高度的“岛屿”。它们实际上是独立的实体。

gis cad dxf ezdxf
1个回答
0
投票

再次感谢mozman给出最初的提示。

我可以通过以下方式解决这个问题:


# Code as above 

doc = ezdxf.new()
msp = doc.modelspace()

for level, collection in zip(contour_levels, contour_data.collections):
    for path in collection.get_paths():
        for segment in path.to_polygons():
            if len(segment) > 1:
                vertices_3d = [(x, y, level) for x, y in segment]
                if np.allclose(vertices_3d[0], vertices_3d[-1]):
                    vertices_3d.pop()

                polyline = msp.add_polyline3d(vertices_3d)
                polyline.dxf.layer = f'Contour_{level:.2f}'

使用

if np.allclose(vertices_3d[0], vertices_3d[-1]):
检查,然后使用
vertices_3d.pop()
确实可以删除导出中始终连接的点。

© www.soinside.com 2019 - 2024. All rights reserved.