Python生成Delaunay三角网

一直以为生成德洛内(Delaunay)三角网和泰森(Voronoi)多边形只有在Arcgis中才能实现,Python中没有现成的包。今天正打算造轮子实现三角网构建的代码,一搜发现Shapely其实是有对应函数的。这下好了,省了不少功夫。

函数完整调用名为“shapely.ops.triangulate”,参数有三个:geoms, tolerance, edges。具体可以看下方代码。

				
					def triangulate(geom, tolerance=0.0, edges=False):
    """Creates the Delaunay triangulation and returns a list of geometries

    The source may be any geometry type. All vertices of the geometry will be
    used as the points of the triangulation.

    From the GEOS documentation:
    tolerance is the snapping tolerance used to improve the robustness of
    the triangulation computation. A tolerance of 0.0 specifies that no
    snapping will take place.

    If edges is False, a list of Polygons (triangles) will be returned.
    Otherwise the list of LineString edges is returned.

    """
        collection = shapely.delaunay_triangles(geom, tolerance=tolerance, only_edges=edges)
        return [g for g in collection.geoms]
				
			

因此,需要输入一个geoms合集,一般是点要素。例如:

				
					from shapely.ops import triangulate
from shapely.geometry import MultiPoint

points = MultiPoint([(0, 0), (1, 1), (0, 2), (2, 2), (3, 1), (1, 0)])
triangles = triangulate(points)
print([triangle.wkt for triangle in triangles])
['POLYGON ((0 2, 0 0, 1 1, 0 2))',
 'POLYGON ((0 2, 1 1, 2 2, 0 2))',
 'POLYGON ((2 2, 1 1, 3 1, 2 2))',
 'POLYGON ((3 1, 1 1, 1 0, 3 1))',
 'POLYGON ((1 0, 1 1, 0 0, 1 0))']
 
triangles2 = triangulate(points, edges=True)
print([triangle.wkt for triangle in triangles2])
['LINESTRING (2 2, 3 1)',
 'LINESTRING (0 2, 2 2)',
 'LINESTRING (0 0, 0 2)',
 'LINESTRING (0 0, 1 0)',
 'LINESTRING (1 0, 3 1)',
 'LINESTRING (1 0, 1 1)',
 'LINESTRING (1 1, 3 1)',
 'LINESTRING (1 1, 2 2)',
 'LINESTRING (0 2, 1 1)',
 'LINESTRING (0 0, 1 1)']
				
			

参考:

https://zhuanlan.zhihu.com/p/284565335

https://blog.csdn.net/linzi1994/article/details/106484538

渝ICP备2024029132
Copyright © 2024 xuzzhan
滚动至顶部