8

I am developing the RQGIS package. To run QGIS from within R, I call QGIS via the command line. This worked perfectly with QGIS 2.14. Running the same code with QGIS 2.16, however, produces an error. Here is what I do:

First, I set all necessary paths in the command prompt:

@echo off
SET OSGEO4W_ROOT=D:\osgeo4w_qgis16
call "%OSGEO4W_ROOT%"\bin\o4w_env.bat
@echo off
path %PATH%;%OSGEO4W_ROOT%\apps\qgis\bin
set PYTHONPATH=%PYTHONPATH%;%OSGEO4W_ROOT%\apps\qgis\python;
set QGIS_PREFIX_PATH=%OSGEO4W_ROOT%\apps\qgis
rem open python     
python.exe

Subsequently, I run following lines in Python:

import os
import sys
from qgis.core import *
import qgis.utils
from osgeo import ogr
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from qgis.gui import *
QgsApplication.setPrefixPath('D:\osgeo4w_qgis16\apps\qgis', True)
app = QgsApplication([], True)
QgsApplication.initQgis()
sys.path.append(r'D:\osgeo4w_qgis16\apps\qgis\python\plugins')
from processing.core.Processing import Processing
Processing.initialize()
import processing
processing.alglist()
processing.alghelp("grass7:v.voronoi")

This works. However, calling processing.runalg...

from processing.tests.TestData import points
result = processing.runalg('grass7:v.voronoi', points(), False, False, '270778.60198,270855.745301,4458921.97814,4458983.8488', -1, 0.0001, 0, None,)

...produces this error message:

Error in sys.excepthook:
Traceback (most recent call last):
  File "D:\osgeo4w_qgis16\apps\qgis\python\qgis\utils.py", line 196, in qgis_excepthook
    showException(type, value, tb, None, messagebar=True)
  File "D:\osgeo4w_qgis16\apps\qgis\python\qgis\utils.py", line 107, in showException
    open_stack_dialog(type, value, tb, msg)
  File "D:\osgeo4w_qgis16\apps\qgis\python\qgis\utils.py", line 142, in open_stack_dialog
    iface.messageBar().popWidget()
AttributeError: 'NoneType' object has no attribute 'messageBar'

Original exception was:
Traceback (most recent call last):
  File "<stdin>", line 10, in <module>
  File "D:\osgeo4w_qgis16\apps\qgis\python\plugins\processing\tools\general.py", line 75, in runalg
    alg = Processing.runAlgorithm(algOrName, None, *args, **kwargs)
  File "D:\osgeo4w_qgis16\apps\qgis\python\plugins\processing\core\Processing.py", line 304, in runAlgorithm
    ret = runalg(alg, progress)
  File "D:\osgeo4w_qgis16\apps\qgis\python\plugins\processing\gui\AlgorithmExecutor.py", line 52, in runalg
    progress.error(e.msg)
AttributeError: 'NoneType' object has no attribute 'error'
5

The Processing error was fixed recently, see https://github.com/qgis/QGIS/commit/df2ca2e60798315d816966f25aa024b93835f776

1
  • 1
    Victor, though you said the issue was fixed, it still remains alive. Recently, QGIS 2.18 was released. But still one has to manually replace alg.execute(progress) by alg.execute(progress or SilentProgress()) in AlgorithmExecutor.py to use QGIS in a custom application. The issue has become even worse since now there is no error message occuring. Instead there are even success messages (e.g., when running saga:catchmentarearecursive), only the specified output folders remain empty. I would greatly appreciate if you could fix that issue. Or am I missing something? – Jannes Oct 27 '16 at 14:39
5

Barry, thank you for your advice. Apparently, the QGIS core team changed the code. In QGIS 2.14 runalg was defined as follows:

def runalg(alg, progress=None):
  """Executes a given algorithm, showing its progress in the
  progress object passed along.

  Return true if everything went OK, false if the algorithm
  could not be completed.
  """

  if progress is None:
      progress = SilentProgress()
  try:
      alg.execute(progress)
      return True
  except GeoAlgorithmExecutionException as e:
      ProcessingLog.addToLog(sys.exc_info()[0], ProcessingLog.LOG_ERROR)
      progress.error(e.msg)
      return False

So if progress is equal to None (which is the case in my example code), SilentProgress took care of it. By contrast, in QGIS 2.16 the corresponding if-statement was removed (see Barry's answer above), which leads to the failure of runalg. One solution would be to manually edit the AlgorithmExecutor.py script by adding again the missing lines. In this case, one would have to add another import line to the beginning of the script (from processing.core.SilentProgress import SilentProgress).

I will also ask the QGIS core team if there is a special reason for the removal of these lines. With a bit of luck, they will add them again...

2
  • adding the 2 lines above and the import statement to the AlgorithmExecutor.py script worked for me! I just upgraded to 2.16 last week and was hung up on this as well. Thanks! – rickD Aug 22 '16 at 20:21
  • I've just upgraded from 2.8.2 to 2.16.3 and encountered the same issues with some external python scripts I run. Making the above changes fixed the issue for me. Thanks Jannes and Spacedman. – BStone Sep 27 '16 at 14:23
4

This last bit of the original error:

  File "D:\osgeo4w_qgis16\apps\qgis\python\plugins\processing\gui\AlgorithmExecutor.py", line 52, in runalg
    progress.error(e.msg)
AttributeError: 'NoneType' object has no attribute 'error'

is saying that progress is None, so the progress.error call fails. This then triggers the first part of the displayed error because QGIS error handler tries to write a message to an iface object that also doesn't exist.

The relevant bit of Processing's python code that generates the original error is:

def runalg(alg, progress=None):
    """Executes a given algorithm, showing its progress in the
    progress object passed along.

    Return true if everything went OK, false if the algorithm
    could not be completed.
    """
    try:
        alg.execute(progress)
        return True
    except GeoAlgorithmExecutionException as e:
        ProcessingLog.addToLog(sys.exc_info()[0], ProcessingLog.LOG_ERROR)
        progress.error(e.msg)  ## this line ##
        return False

So for some reason the algorithm has raised a GeoAlgorithmExecutionException and progress is None. I don't know where progress is meant to come from - perhaps your python script should create something to pass to it. I don't know its raising the GeoAlg error either, or which of the two problems is the one you really need to fix...

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.