From Voronoi tessellation to Shapely polygons

后端 未结 4 1355
陌清茗
陌清茗 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条回答
  •  猫巷女王i
    2020-12-24 08:52

    The library is able to generate ordered list of coordinates, you just need to make use of the index lists provided:

    import numpy as np
    from scipy.spatial import Voronoi
    
    ...
    
    ids = np.array(my_points_list)
    vor = Voronoi(points)
    polygons = {}
    for id, region_index in enumerate(vor.point_region):
        points = []
        for vertex_index in vor.regions[region_index]:
            if vertex_index != -1:  # the library uses this for infinity
                points.append(list(vor.vertices[vertex_index]))
        points.append(points[0])
        polygons[id]=points
    

    each polygon in the polygons dictionary can be exported to geojson or brought into shapely and I was able to render them properly in QGIS

提交回复
热议问题