6

I would like to split complex geometries into n smallest geometries of the same area in Python. While I can divide simple geometry in the following way:

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

I am not able to find a way to do the same for complex geometries.

Is there any similar plugin or function in Python like Splitting polygon into equal area polygons in QGIS 3?

A geometry could be the following one that can be dowloaded here

enter image description here

PolyGeo
  • 65,136
  • 29
  • 109
  • 338
emax
  • 269
  • 4
  • 17
  • 1
    Equal-area partitioning of irregular features can be extremely computationally intensive, and if they are highly irregular, then they can be NP Hard or NP Complete. – Vince Jul 28 '21 at 14:59
  • I developed an algorithm by using PyQGIS. It considers cut angle and proportion of involved areas. For instance, I used your irregular geometry for dividing it with a cut angle of 45 ΒΊ in two parts and 30/70 proportion. Result was as follows: https://i.stack.imgur.com/ctbUx.png – xunilk Jul 28 '21 at 19:51
  • Very simple algorithm if shapely has split function that works on multipart polygon https://gis.stackexchange.com/questions/364262/split-a-polygon-56-west-44-east/364480#364480 – FelixIP Jul 29 '21 at 09:29

0 Answers0