# How to split two interscting polygons by the average linestring of the shared area (QGIS / python shapely)

I have two polygons,

POLYGON((-5 -5,-5 5,5 5,5 -5,-5 -5)) POLYGON((1 1, 1 7, 7 7, 7 1, 1 1))

and wish to split them evenly. The result should be a split along the red marked line.

I'm using qgis/ python shapely and wish to do it using python.

-
How general is your problem? Evidently you intend a solution to apply to more than just these two polygons. What kinds of polygons do you contemplate? Precisely what do you mean by "evenly"? Must the split be along a line segment or can it be along some nonlinear arc? What should happen in case the polygon intersection consists of multiple components or is not simply-connected? What form should the output take--should it split the intersection, or each of the polygons, or perhaps just be the splitting line? – whuber Oct 25 '12 at 10:21
I'm working w/ irregular polygons, and wanna have two not-intersecting polygons. in this case, original polygons as mentions and the desired outcome is POLYGON((-5 -5,-5 5,1 5,5 1,5 -5,-5 -5)) POLYGON((1 5, 1 7, 7 7, 7 1,5 1,1 5)) – OLS Oct 25 '12 at 12:15
The problem is difficult when either or both of the polygons may have rings (that is, not be simply-connected). – whuber Oct 25 '12 at 13:21

I used an approach with voronoi polygons once. I did it by and hand I only have a vague idea of how you could do it with shapely, but here's how it goes.

First, you extract the vertices of each overlapping polygons and create Voronoi polygons from them (vector > geometry tools > extract nodes & vector > geometry tools > vononoi). With the resulting shape, you can draw the line that splits your overlapping area exactly in two. (The bright green line)

You clip the voronoi layer with the overlapping polygon and remove the overlap from the original shapes and paste the clipped voronoi polygon back with your original shapes. Select, merge, voilà!

Now, it looks a little crude, because mine are really simple shapes. But, it you densify it (Vector > Geometry tools > Density geometries) and add like 20 nodes between each existing vertex, you can have this:

Which is much better. Easy if you only have a small amount of shapes to modify.

-
This is a good idea. But where does that bright green line come from and how do you know it splits the region into two equal parts? – whuber Oct 25 '12 at 13:16
how different is this process than `SELECT astext(intersection(geomFromText('POLYGON((-5 -5,-5 5,5 5,5 -5,-5 -5))',4326), geomFromText('POLYGON((1 1, 1 7, 7 7, 7 1, 1 1))',4326))); astext -------------------------------- POLYGON((1 5,5 5,5 1,1 1,1 5)' ? – OLS Oct 25 '12 at 13:52
It comes from the propriety of voronoi polygon. It divide the space evenly between the points. That's why you have to densify your polygons because otherwise, the "middle" line doesn't follow the boundaries. As for the bright line, I draw it manually to illustrate where the middle is... As for the PostGIS command (I'm a newbie in PostGIS), I think it will only select the intersecting area, not divide it. – fgcartographix Oct 25 '12 at 14:26
Having said that, I didn't say that that it will be equal part. That method is used to find river centerline. If you have a bay in the river, the middle line will be pull toward it. but it will not enter it so making it "centered" nonetheless. – fgcartographix Oct 25 '12 at 14:38