9

I'm kind of stuck trying to figure out the way to run sextante from a standalone python from OSGeo4W distribution. The reason I want to do this is that I got tired entering parameters in the dialog every time I want to test a model from Model Builder.

So here is the python script let's call it test.py

# as per http://qgis.org/pyqgis-cookbook/intro.html#using-pyqgis-in-custom-application
from qgis.core import *
# supply path to where is your qgis installed
QgsApplication.setPrefixPath("C:/OSGeo4W/apps/qgis", True)
# load providers
QgsApplication.initQgis()

from sextante.core.Sextante import Sextante
Sextante.alglist()
Sextante.alghelp("saga:slopeaspectcurvature")

That I'm calling from my batch file

@echo off

set OSGEO4W_ROOT=C:\OSGeo4W
set PYTHONPATH=%OSGEO4W_ROOT%\apps\qgis\python;%OSGEO4W_ROOT%\apps\qgis\python\plugins;%HOME%/.qgis/python/plugins
set PATH=%OSGEO4W_ROOT%\bin;%OSGEO4W_ROOT%\apps\qgis\bin;%OSGEO4W_ROOT%\apps\qgis\plugins

python test.py

The problem is that it says Algorithm not found whereas I get meaningful output from QGIS python console.

I feel like I'm missing to initialize something. But what?

Is there a better way to test a Model other than via entering tons of parameters using GUI?

UPDATE 7/2/2012

I'm looking for generic pythonic solution to test with "mine" algorithms. Aforementioned algorithm is just an example showing that something probably was not initialized.

UPDATE 7/27/2012

An alternative to Script Runner is to use IPython console to debug scripts. Other than that there does not seem to be a way to do simple unit testing with sextante with nothing else running:(

UPDATE 7/30/2012

As Victor Olaya suggests, I try to initialize Sextante like in the code below.

#!/usr/bin/env python

import sys
from PyQt4.QtGui import QApplication
from sextante.core.Sextante import Sextante

def main():
    """ main function or something """
    # as per http://qgis.org/pyqgis-cookbook/intro.html#using-pyqgis-in-custom-application
    from qgis.core import *
    import qgis.utils

    app = QApplication(sys.argv)
    # supply path to where is your qgis installed
    QgsApplication.setPrefixPath("C:/OSGeo4W/apps/qgis", True)
    # load providers
    QgsApplication.initQgis()
    # how???
    # qgis.utils.iface = QgisInterface.instance()
    Sextante.initialize()
    run_script(qgis.utils.iface)

def run_script(iface):
    """ this shall be called from Script Runner"""
    Sextante.alglist()
    Sextante.alghelp("saga:slopeaspectcurvature")

if __name__=="__main__":
    main()

However I get something like

Traceback (most recent call last):
  File "test.py", line 29, in
    main()
  File "test.py", line 20, in main
    Sextante.initialize()
  File "C:\Documents and Settings\user\.qgis\python\plugins\sextante\core\Sextante.py", line 94, in initialize
    Sextante.addProvider(GrassAlgorithmProvider())
  File "C:\Documents and Settings\user\.qgis\python\plugins\sextante\grass\GrassAlgorithmProvider.py", lin
e 17, in __init__
    self.actions.append(DefineGrassRegionAction())
  File "C:\Documents and Settings\user\.qgis\python\plugins\sextante\grass\DefineGrassRegionAction.py", li
ne 16, in __init__
    canvas = QGisLayers.iface.mapCanvas()
AttributeError: 'NoneType' object has no attribute 'mapCanvas'

Well... it all becomes a mailing list discussion alike. Perhaps it worth moving to qgis-user or qgis-developer instead of SE.

ahmadhanb
  • 40,826
  • 5
  • 51
  • 105
mlt
  • 357
  • 4
  • 14
  • You can't access iface in a standalone QGIS script. iface is only of use when running in side QGIS. – Nathan W May 16 '13 at 00:31

5 Answers5

5

You could craft your script to work with Gary Sherman's Script Runner plugin and run it from within QGIS. Re-running the script, after editing, should prompt Script Runner to reload the module and reflect your changes. See also: Script Runner's plugins.qgis.org listing.

The essentials are to make sure you have a run_script function, which gets called by Script Runner (example from his blog):

def run_script(iface):
    ldr = Loader(iface)
    ldr.load_shapefiles('/vmap0_shapefiles')
dakcarto
  • 7,754
  • 26
  • 34
  • While in theory it should indeed help to debug (though inside QGIS), it looks like it is broken on Windows. It keeps saying AttributeError: 'module' object has no attribute 'run_script' and keep insisting that I have no docstrings that I can see in source viewer. – mlt Jul 02 '12 at 21:55
  • Did you add the def run_script(iface) function? Your script will not run in Script Runner without that. – dakcarto Jul 02 '12 at 22:07
  • Apparently one should not name script as test :-) mytest works okay so far. It would be nice if __import__ scope can be constrained instead of looking all over sys.path. It shows docstring and list of other functions. – mlt Jul 02 '12 at 22:27
  • Script Runner is updated here – Dave X Jul 22 '14 at 17:16
3

For new users reading this post, there is a way of running QGIS processing algorithms in standalone PyQGIS scripts. Check answers to Import error for qgis.core when running OSGeo4w shell script and How can I access `processing` with Python?, which provide you with tested examples.

Germán Carrillo
  • 36,307
  • 5
  • 123
  • 178
3

Sextante has to be initialized, so it loads the algorithms and can later execute them.

Call Sextante.initialize() before doing anything.

Victor Olaya
  • 679
  • 4
  • 4
  • Hi Victor, is this still the case? I'm struggling with how to call Sextante in a standalone script (outside of QGIS) and finding all examples I google to not work. This was posted a year ago so I wonder if the architecture of sextante has changed? – Rich May 15 '13 at 06:49
2

Since the algorithm you want to use is part of saga, you could use saga directly.

eg from a batchfile:

@ECHO OFF

REM SET SAGA_MLB = C:\SAGA\Modules
REM SET PATH = %PATH%;C:\SAGA

saga_cmd ta_morphometry "Slope, Aspect, Curvature" -ELEVATION=elevation.sgrd -SLOPE=slope.sgrd -ASPECT=aspect.sgrd -CURV=NULL -HCURV=NULL -VCURV=NULL -METHOD=5

PAUSE
johanvdw
  • 6,207
  • 27
  • 42
  • I apologize, I should have made it clear. It was just an example. I am not going to use SAGA in particular. – mlt Jul 02 '12 at 19:05
2

According to How to run a simple python script for QGIS from outside (e.g. Sublime Text)? you can't get a reference to the iface object here because it doesn't exist in this context since this is being run outside of QGIS. Any progress?

Martin
  • 302
  • 2
  • 10