I have a standalone script that call general.runalg(qgis:linestopolygons...) all ok, but only convert the first feature, and I do previous "selectAll", why ?

Full code:

#!/usr/bin/env python

import qgis
from qgis.core import *
import sys, os

app = QgsApplication([],True, None)
app.setPrefixPath("/usr", True)
from processing.core.Processing import Processing
from processing.tools import *

vlayer = QgsVectorLayer(inFile, "temp", "ogr")

print "Elements loaded:"+str(vlayer.featureCount()) # shows number elements loaded

# note
# you can also call (but same result), only 1 feature into output:
# general.runalg("qgis:linestopolygons",inFile,poly_output_file)

# Exit applications
#app.exitQgis() # give segmentation fault

The temp_poly.shp file is created, but only have one polygon (the first feature).

If I try to convert the same file in, but using QGIS desktop GUI all features are convert to polygons, so is not a problem from source file.

  • After has been studing the source /QGIS-master/python/plugins/processing/algs/qgis/LinesToPolygons.py I discover that you can call runalg() with and URI and not with a QgsVectorLayer, so changuing the line that call to runalg() to: general.runalg("qgis:linestopolygons",inFile,poly_output_file) also create the output file, but same result :( only one feature. – Juanma Font Dec 2 '16 at 8:29

It's a BUG, or it may be an BUG, by modifying the source code and removing the line that shows the progress bar, the writing of the file is done correctly and all the polygons are written to the output file.

Using Linux -> QGIS 2.18.1

Into the package python-qgis-common, the file /usr/share/qgis/python/plugins/processing/algs/qgis/LinesToPolygons.py

If I comment out the line(82) that says :

        progress.setPercentage(int(current * total))


        #progress.setPercentage(int(current * total))

then all the polygons are write to the output file when calling :

general.runalg("qgis:linestopolygons",inputFile, outputFile) from and standalone script.

That's it....

I'm gonna to communicate to developers...

  • Strange, don't have linux to test this on but works fine under Windows. Also, you shouldn't need to use vlayer.selectAll(), the tool should process all features in the layer by default =) – Joseph Dec 2 '16 at 10:35
  • 1
    Thanks, Joseph. it's true, looking the source code I saw that works over all features, my original code it was copied from an example from another user. – Juanma Font Dec 2 '16 at 11:04

Maybe is not a bug (Although I have reported it to developers). I have read this: [How to run a simple python script for QGIS from outside (e.g. Sublime Text)?

The important part is this:

(About the scripts) There are three different situations:

  • A QGIS plugin
  • A script that runs inside QGIS (not a plugin) for automation
  • Standalone app using QGIS APIs

    1. and 2. have access to iface, the last one doesn't.

For 3 if you want to create a script that opens a layer in a map canvas you would do this after QgsApplication.initQgis()

So not all plugins can be called from a standalone scripts, if the original plugin using a direct call to a GUI part, you can not use it. In our case, the plugin /usr/share/qgis/python/plugins/processing/algs/qgis/LinesToPolygons.py use the progressbar , so if is it called from and standalone script this will be produce an error (not showed into console), and the called plugin (script) will finished with an error (remember, not showed to us into console).

  • Algorithms should not directly work with gui widgets. progress in this case is a simple interface which can show the information on a QProgressBar, but there is e.g. also processing.core.SilentProgress which prints errors to stdout and does nothing else. You can even provide your own progress object. – Matthias Kuhn Dec 3 '16 at 9:31
  • Yes, Matthias, and using python is not easy avoid execution (compilation time) of a line of code. I think the same that you, a plugin must not work directly with gui widgets. In this case the use of a progress bar makes the plugin unusable from the outside (standalone script) and this limits much the development of third applications on the environment QGIS. – Juanma Font Dec 3 '16 at 11:17
  • 1
    To stress the important bit: progress is not a ProgressBar. It's a progress object, it's totally up to you how you deal with that progress in a custom script. – Matthias Kuhn Dec 3 '16 at 11:23
  • 1
    Here's an example how an algorithm can be called with a custom progress object github.com/opengisch/QFieldSync/blob/master/qfieldsync/utils/… – Matthias Kuhn Dec 3 '16 at 11:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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