3

In QGIS 2.12.3 I have a small shapefile with lines (as often occurs in parcel boundary datasets) of 2 vertices each, snapped to each other (no topology errors, no dangles, no intersections). But the "Lines to Polygons" (screenshot) tool doesn't produce any polygons (in contrast to the two expected polygons).

What am I doing wrong or what am i having to do to make this tool work properly? (I'm checking out 2.14. tomorrow...) I know that in PostGIS the ST_Polygonize works pretty well, but importing my data to PostGIS first or any other workaround seems not a staightforward solution to me.

QGIS 2.12. Lines To Polygons does not work properly

1 Answer 1

3

Hmm. Something seems to be wrong with the algorithm... but the 'polygonize' seems to work.

Use Qgis Console and then (Layer with your lines is the first in the legend):

from PyQt4.QtCore import *
from PyQt4.QtGui import *
from qgis.utils import iface
import processing
output = r"C:/path to where you want to save it/"
input = qgis.utils.iface.activeLayer() #make sure line-layer is active
# if first layer in legend you can also use: input = qgis.utils.iface.legendInterface().layers()[0]

#if None doesn't work; try False instead
processing.runalg("qgis:polygonize", input, None, QGis.WKBLineString, output+"Polygonzetestpoly.shp")
poly = QgsVectorLayer(output+"Polygonzetestpoly.shp","Polygonzetestpoly",'ogr')
QgsMapLayerRegistry.instance().addMapLayer(poly)

Worked for me (windows)

I hope this helps?Screenshot

6
  • 3
    Great answer! Couple of things I noticed: you could use input = qgis.utils.iface.activeLayer() which means the user can just highlight the layer instead of moving it up the ToC; I had to replace None with False in the processing algorithm but I think it's to do with my setup though =)
    – Joseph
    Commented Feb 25, 2016 at 12:21
  • 1
    I'll edit, activeLayer() seems more intuitive.
    – Jason
    Commented Feb 25, 2016 at 12:36
  • That works, great! But I wonder if QGIS doesn't internally make use of this algorithm? Commented Feb 25, 2016 at 15:31
  • 1
    I check this out with a parcel boundary dataset with about 60k polylines, additionally I noticed that have to convert (save as) a dxf file to shp. Commented Feb 25, 2016 at 15:33
  • I just found out, that you can also use that algorithm this way: Open the processing-toolbox (--> processing--> toolbox) and then change the interface (bottom corner) to advanced. Then you have 2 options with different outputs: 1) polygonize (you can search in the toolbox but it's under Qgis-Algorithms--> Vector geometry tools) or 2) use the SAGA--> Shapes - Polygons -->**Convert Lines to Polygons**
    – Jason
    Commented Feb 25, 2016 at 15:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.