如何确定正方形与多边形相交的内边界顶点?Python实现求助
获取正方形瓦片与多边形相交内边缘的顶点坐标问题
我正尝试编写Python脚本,获取正方形瓦片与n顶点闭合多边形之间相交内边缘的所有顶点坐标(x,y)。目前已完成如下代码,但得到的内边界顶点存在错误,请问是否有专门算法可用于确定内边缘,以明确搜索方向?
from shapely.geometry import Polygon, LineString, MultiLineString import matplotlib.pyplot as plt from matplotlib.patches import Polygon as MplPolygon square_coords = [ (53906250, 35456250), # lt (54296875, 35456250), # rt (54296875, 35546875), # rb (53906250, 35546785) # lb ] polygon_coords = [ (54340742, 35518900), (53997222, 35518800), (53997298, 35183478), (54340666, 35183478), (54340742, 35518890) ] square = Polygon(square_coords) polygon = Polygon(polygon_coords) intersection = square.intersection(polygon) fig, ax = plt.subplots() ax.set_aspect('equal') square_patch = MplPolygon(list(square.exterior.coords), color='blue', alpha=0.4, label='Square') ax.add_patch(square_patch) polygon_patch = MplPolygon(list(polygon.exterior.coords), color='green', alpha=0.4, label='Polygon') ax.add_patch(polygon_patch) if not intersection.is_empty: if intersection.geom_type == 'Polygon': inter_coords = list(intersection.exterior.coords) inter_patch = MplPolygon(inter_coords, color='red', alpha=0.6, label='Intersection') ax.add_patch(inter_patch) elif intersection.geom_type == 'MultiPolygon': for part in intersection.geoms: inter_coords = list(part.exterior.coords) inter_patch = MplPolygon(inter_coords, color='red', alpha=0.6, label='Intersection') ax.add_patch(inter_patch) polygon_boundary = polygon.boundary intersection_boundary = intersection.boundary inner_border = polygon_boundary.intersection(intersection_boundary) if not inner_border.is_empty: if isinstance(inner_border, (LineString, MultiLineString)): if isinstance(inner_border, LineString): lines = [inner_border] else: lines = list(inner_border.geoms) for line in lines: x, y = line.xy ax.plot(x, y, color='black', linewidth=2, label='Inner Border') ax.scatter([x[0], x[-1]], [y[0], y[-1]], color='purple', s=40, zorder=10) ax.text(x[0], y[0], f"({x[0]:.5f}, {y[0]:.5f})", fontsize=7, color='purple') ax.text(x[-1], y[-1], f"({x[-1]:.5f}, {y[-1]:.5f})", fontsize=7, color='purple') ax.set_title("Polygon-Square Intersection & Inner Border") ax.set_xlabel("X") ax.set_ylabel("Y") ax.grid(True) ax.legend() plt.tight_layout() plt.show()
我的目标是获取示例图中黄色边界上的所有相交顶点坐标(x,y):


内容的提问来源于stack exchange,提问作者tomazj
相关产品推荐
相关产品推荐

