[sextante] added preprocessing of input params for saga algorithms

This commit is contained in:
Victor Olaya 2013-07-23 13:05:24 +02:00
parent ab98569f44
commit c7eb99bae1
7 changed files with 77 additions and 16 deletions

View File

@ -47,13 +47,9 @@ class FieldsCalculator(GeoAlgorithm):
FORMULA = "FORMULA" FORMULA = "FORMULA"
OUTPUT_LAYER = "OUTPUT_LAYER" OUTPUT_LAYER = "OUTPUT_LAYER"
TYPE_NAMES = ["Integer", "Float", "String"] TYPE_NAMES = ["Float", "Integer", "String", "Boolean"]
TYPES = [QVariant.Int, QVariant.Double, QVariant.String] TYPES = [QVariant.Double, QVariant.Int, QVariant.String. QVariant.Bool]
#===========================================================================
# def getIcon(self):
# return QtGui.QIcon(os.path.dirname(__file__) + "/../images/qgis.png")
#===========================================================================
def defineCharacteristics(self): def defineCharacteristics(self):
self.name = "Field calculator" self.name = "Field calculator"
@ -62,7 +58,7 @@ class FieldsCalculator(GeoAlgorithm):
self.addParameter(ParameterString(self.FIELD_NAME, "Result field name")) self.addParameter(ParameterString(self.FIELD_NAME, "Result field name"))
self.addParameter(ParameterSelection(self.FIELD_TYPE, "Field type", self.TYPE_NAMES)) self.addParameter(ParameterSelection(self.FIELD_TYPE, "Field type", self.TYPE_NAMES))
self.addParameter(ParameterNumber(self.FIELD_LENGTH, "Field length", 1, 255, 10)) self.addParameter(ParameterNumber(self.FIELD_LENGTH, "Field length", 1, 255, 10))
self.addParameter(ParameterNumber(self.FIELD_PRECISION, "Field precision", 0, 10, 0)) self.addParameter(ParameterNumber(self.FIELD_PRECISION, "Field precision", 0, 10, 5))
self.addParameter(ParameterString(self.FORMULA, "Formula")) self.addParameter(ParameterString(self.FORMULA, "Formula"))
self.addOutput(OutputVector(self.OUTPUT_LAYER, "Output layer")) self.addOutput(OutputVector(self.OUTPUT_LAYER, "Output layer"))
@ -85,14 +81,17 @@ class FieldsCalculator(GeoAlgorithm):
nFeat = provider.featureCount() nFeat = provider.featureCount()
nElement = 0 nElement = 0
features = QGisLayers.features(layer) features = QGisLayers.features(layer)
fieldnames = [field.name() for field in provider.fields()]
fieldnames.sort(key=len, reverse=False)
fieldidx = [fieldnames.index(field.name()) for field in provider.fields()]
print fieldidx
for inFeat in features: for inFeat in features:
progress.setPercentage(int((100 * nElement) / nFeat)) progress.setPercentage(int((100 * nElement) / nFeat))
attrs = inFeat.attributes() attrs = inFeat.attributes()
expression = formula expression = formula
k = 0 for idx in fieldidx:
for attr in attrs: expression = expression.replace(unicode(fields[idx].name()), unicode(attrs[idx]))
expression = expression.replace(unicode(fields[k].name()), unicode(attr))
k += 1
try: try:
result = eval(expression) result = eval(expression)
except Exception: except Exception:

View File

@ -1,5 +1,7 @@
FILE(GLOB PY_FILES *.py) FILE(GLOB PY_FILES *.py)
FILE(GLOB DESCR_FILES description/*.txt) FILE(GLOB DESCR_FILES description/*.txt)
ADD_SUBDIRECTORY(ext)
PLUGIN_INSTALL(sextante saga ${PY_FILES}) PLUGIN_INSTALL(sextante saga ${PY_FILES})
PLUGIN_INSTALL(sextante saga/description ${DESCR_FILES}) PLUGIN_INSTALL(sextante saga/description ${DESCR_FILES})

View File

@ -16,6 +16,7 @@
* * * *
*************************************************************************** ***************************************************************************
""" """
import importlib
__author__ = 'Victor Olaya' __author__ = 'Victor Olaya'
__date__ = 'August 2012' __date__ = 'August 2012'
@ -181,6 +182,8 @@ class SagaAlgorithm(GeoAlgorithm):
commands = list() commands = list()
self.exportedLayers = {} self.exportedLayers = {}
self.preProcessInputs()
#1: Export rasters to sgrd and vectors to shp #1: Export rasters to sgrd and vectors to shp
# Tables must be in dbf format. We check that. # Tables must be in dbf format. We check that.
if self.resample: if self.resample:
@ -308,6 +311,7 @@ class SagaAlgorithm(GeoAlgorithm):
#4 Run SAGA #4 Run SAGA
commands = self.editCommands(commands)
SagaUtils.createSagaBatchJobFileFromSagaCommands(commands) SagaUtils.createSagaBatchJobFileFromSagaCommands(commands)
loglines = [] loglines = []
loglines.append("SAGA execution commands") loglines.append("SAGA execution commands")
@ -319,6 +323,28 @@ class SagaAlgorithm(GeoAlgorithm):
SagaUtils.executeSaga(progress); SagaUtils.executeSaga(progress);
def preProcessInputs(self):
name = self.commandLineName().replace('.','_')[len('saga:'):]
try:
module = importlib.import_module('sextante.grass.ext.' + name)
except ImportError:
return
if hasattr(module, 'preProcessInputs'):
func = getattr(module,'preProcessInputs')
func(self)
def editCommands(self, commands):
name = self.commandLineName()[len('saga:'):]
try:
module = importlib.import_module('sextante.grass.ext.' + name)
except ImportError:
return commands
if hasattr(module, 'editCommands'):
func = getattr(module,'editCommands')
return func(commands)
else:
return commands
def getOutputCellsize(self): def getOutputCellsize(self):
'''tries to guess the cellsize of the output, searching for a parameter with an appropriate name for it''' '''tries to guess the cellsize of the output, searching for a parameter with an appropriate name for it'''
cellsize = 0; cellsize = 0;

View File

@ -0,0 +1,3 @@
FILE(GLOB PY_FILES *.py)
PLUGIN_INSTALL(sextante saga/ext ${PY_FILES})

View File

@ -0,0 +1,31 @@
# -*- coding: utf-8 -*-
"""
***************************************************************************
supervisedclassification.py
---------------------
Date : July 2013
Copyright : (C) 2013 by Victor Olaya
Email : volayaf at gmail dot com
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************
"""
from sextante.tests.TestData import table
__author__ = 'Victor Olaya'
__date__ = 'July 2013'
__copyright__ = '(C) 2013, Victor Olaya'
# This will get replaced with a git SHA1 when you do a git archive
__revision__ = '$Format:%H$'
def editCommands(commands):
commands[-1] = commands[-1] + " -STATS" + table()
return commands