[GIS] Convert multiple polylines to polygons in QGIS

polygonizepyqgispythonqgis

I have a vector layer with several polyline segments that represent borders of different areas. Each segment is connected to at least two other segments (at its start and its end). Some segments are connected to more then two other segments.

I need to convert those multiple polylines to multiple polygons. (See fig.)

enter image description here

When I use polygonize tool in vector->geometry menu it treats each segment as it's own polygon and makes garbage. (See fig 2)

enter image description here

Merging all segments into a single one doesn't help. Clipping large polygon with lines also doesn't help in this case since line segments doesn't cross the whole large polygon.

This problem seems obvious but I'm stuck. I can do it in GRASS with ease, but I have no idea how to do it in QGIS. I don't mind using Python scripting for this.

Best Answer

Using QGIS processing: v.clean [snap]

Polyline test enter image description here

The threshold is determined by attempts: enter image description here

final results enter image description here

To get the polygons: polygonize

enter image description here

polygons enter image description here

Related Question