Friday 22 March 2019

pyqgis - How to run sextante algorithms outside of QGIS python console?


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.




No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...