如何将复杂几何划分为 n 个等于子几何?

问题描述

我想将一个复杂的几何图形划分为同一区域的 n 个子几何图形。

例如,如果我有一个矩形,我可以做这样的事情

from shapely.geometry import Linestring,Multipolygon,polygon
from shapely.ops import split
def splitpolygon(polygon,nx,ny):
    minx,miny,maxx,maxy = polygon.bounds
    dx = (maxx - minx) / nx
    dy = (maxy - miny) / ny

    minx,maxy = polygon.bounds
    dx = (maxx - minx) / nx  # width of a small part
    dy = (maxy - miny) / ny  # height of a small part
    horizontal_splitters = [Linestring([(minx,miny + i*dy),(maxx,miny + i*dy)]) for i in range(ny)]
    vertical_splitters = [Linestring([(minx + i*dx,miny),(minx + i*dx,maxy)]) for i in range(nx)]
    splitters = horizontal_splitters + vertical_splitters
    result = polygon
    for splitter in splitters:
        result = Multipolygon(split(result,splitter))
    return result

mypolygons = splitpolygon(polygon,5,5)
import geopandas as gpd
gdfR   = gpd.GeoDataFrame(columns=['geometry'],data=mypolygons.geoms)
f,ax=plt.subplots()
gdfR.boundary.plot(ax=ax,color='red')
polygon.boundary.plot(ax=ax)

enter image description here

我想将像下面这样的复杂几何图形拆分为同一区域的 n 最小几何图形。可以将几何下载为 shapefile here

enter image description here

解决方法

最简单、最有效的解决方案之一是采用几何的 minimum_rotated_rectangle

  1. 首先,您将 Polygon 或 MultiPolygon 转换为 LineString 并获得包含此线的 import certifi certifi.where()

  2. 然后将之前的代码应用到这个矩形。

  3. 最后,您必须在多边形的 LineString 和子几何图形之间进行第二次分割。