# Draw polygons from self-intersecting lines: Looking for a workaround for inner boundaries / holes

I use OGR & Python to develop a geodesic polygon buffer. The buffered polygon is generated from a polyline ring. The polyline ring is generated from point coordinates. As the polyline ring is often self-intersecting, this leads to holes in the buffered polygon geometry. For the outer boundary of a self-intersecting line / polygon, applying a Buffer of 0 to the buffered polygon geometry is a widely used workaround to fill holes from self-intersections and to generate a correctly displayed polygon. However, I have difficulties buffering polygons with holes.

A (buffered) polygon with a hole consists of two lines: An outer line from which I want to determine the most outer boundary (using a Buffer of 0) and an inner line, from which i want to determine the most inner boundary. The workaround using a Buffer of 0 only determines the most outer boundary of a self-intersecting polygon. I am thereby looking for a workaround in order to determine the most inner boundary of a self-intersecting polygon (for applying the buffer to the hole).

The polygon I want to buffer:

I applied several buffer widths. The polyline rings and buffer vertices for the respective buffer polygons are displayed here. In order to create a buffered polygon with a hole inside, I need to determine the most outer boundary of the outer line and the most inner boundary of the inner line:

The polylines are self-intersecting and lead to Polygons with small holes (hole in the top right corner of the polygon):

As for the outer boundary, I use a Buffer of 0 in order to determine the most outer boundary of the outer Polyline. At this point, the hole in the top right corner is closed:

However, this approach can not be used for the inner ring of the polygon. I need to determine the most inner boundary of the self-intersecting inner polyline (Lines inside the hole in Image 2). From both, outer line with outer boundary (which I already have) and inner line with inner boundary I want to generate a buffered polygon with a hole inside.

Is there a workaround for this?

• can you just treat each ring as a separate polygon for the purposes of fixing them and then combine the holes into the final polygon when done? – Ian Turton Sep 8 '16 at 12:15
• Actually, this is what I'm currently trying. I extract and process the outer ring and the inner ring separately and combine them to a polygon (outer ring) with hole (inner ring) in the end. I fill the holes of the outer ring by applying a buffer of 0 (to get the 'outer extent'). What I don't know is how to process the inner ring in order to get the 'inner extent' of the inner ring. – C.Riedel Sep 8 '16 at 12:22
• buffer(-0.01) ? – Ian Turton Sep 8 '16 at 12:46
• Buffer(-0.01) has similar results as buffer(0) (they are slightly closer to the polygon boundaries). I get triangle-shaped features inside the hole. The triangles are the self-intersection areas of the inner polyline. – C.Riedel Sep 8 '16 at 13:01
• Buffering a polygon to remove self-intersection is a super hacky way to do it. You can't expect it to work all of the time because of the nuances involved in geometries. If you really want to remove all self intersects you need to first break the polygon into individual geometries (inner, outer etc.), intersect the geometry with itself to determine the vertex where the self intersect is happening, cut out all vertices within the self intersection, and stitch the polygon closed with the self intersect removed. – onakua Dec 17 '17 at 23:29

``````WITH p AS (SELECT (ST_Dump(ST_Polygonize(ST_Node(ST_ExteriorRing(ST_GeomFromText('POLYGON ((0 10,90 40,90 20,50 60,60 60,10 30,20 50,30 0,0 10))')))))).geom AS ring)