我有点想从OSGeo4W发行版的独立python中找出运行sextante的方式。我要执行此操作的原因是,每当我要从“模型构建器”测试模型时,我都会在对话框中输入参数感到厌倦。
所以这是python脚本,我们称之为 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")
我正在从批处理文件中调用
@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
问题是它说,Algorithm not found
而我从QGIS python控制台获得有意义的输出。
我觉得我想初始化一些东西。但是呢
除了使用GUI输入大量参数外,还有没有更好的方法来测试模型?
更新7/2/2012
我正在寻找通用的pythonic解决方案来测试“我的”算法。前面提到的算法只是一个示例,表明可能未初始化某些内容。
更新7/27/2012
Script Runner的替代方法是使用IPython控制台调试脚本。除此之外,似乎没有一种方法可以使用sextante进行简单的单元测试,而没有其他任何运行:(
更新7/30/2012
正如Victor Olaya所建议的那样,我尝试像下面的代码中那样初始化Sextante。
#!/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()
但是我得到类似
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'
好吧...所有这些都变成了邮件列表讨论。也许值得将其转移到qgis-user或qgis-developer而不是SE。
iface
在独立的QGIS脚本中访问。iface
仅在侧面QGIS中运行时才使用。