How to divide a complex geometry into n equals subgeometries?

Viewed 762

I would like to divide a complex geometry into n subgeometries of the same area.

For instance if I have a rectangle I can do something like this

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, miny, maxx, 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

I would like to split a complex geometry like following one into n smallest geometries of the same area. Is possible to download the geometry as shapefile here.

enter image description here

1 Answers

One of the simplest and most efficient solutions is to take the minimum_rotated_rectangle of the geometry.

  1. First, you transform the Polygon or MultiPolygon into a LineString and you get the minimum_rotated_rectangle containing this line.

  2. Then you apply your previous code to this rectangle.

  3. Finally you have to make a second split between the LineString of your polygon and the subgeometries.


A great turnkey solution: https://stackoverflow.com/a/68778560/14864907

Related