From Voronoi tessellation to Shapely polygons

后端 未结 4 1351
陌清茗
陌清茗 2020-12-24 08:11

from a set of points I built the Voronoi tessellation using scipy:

from scipy.spatial import Voronoi
vor = Voronoi(points)

Now I would like

4条回答
  •  不思量自难忘°
    2020-12-24 09:09

    If you're just after a collection of polygons you don't need to pre-order the point to build them.

    The scipy.spatial.Voronoi object has a ridge_vertices attribute containing indices of vertices forming the lines of the Voronoi ridge. If the index is -1 then the ridge goes to infinity.

    First start with some random points to build the Voronoi object.

    import numpy as np
    from scipy.spatial import Voronoi, voronoi_plot_2d
    import shapely.geometry
    import shapely.ops
    
    points = np.random.random((10, 2))
    vor = Voronoi(points)
    voronoi_plot_2d(vor)
    

    Voronoi plot from scipy.spatial

    You can use this to build a collection of Shapely LineString objects.

    lines = [
        shapely.geometry.LineString(vor.vertices[line])
        for line in vor.ridge_vertices
        if -1 not in line
    ]
    

    The shapely.ops module has a polygonize that returns a generator for Shapely Polygon objects.

    for poly in shapely.ops.polygonize(lines):
        #do something with each polygon
    

    Polygons from Voronoi with some sample points

    Or if you wanted a single polygon formed from the region enclosed by the Voronoi tesselation you can use the Shapely unary_union method:

    shapely.ops.unary_union(list(shapely.ops.polygonize(lines)))
    

    Merged Voronoi tesselation polygon

提交回复
热议问题