Skip to content

Commit c7eb99b

Browse files
committedJul 23, 2013
[sextante] added preprocessing of input params for saga algorithms
1 parent ab98569 commit c7eb99b

File tree

7 files changed

+77
-16
lines changed

7 files changed

+77
-16
lines changed
 

‎python/plugins/sextante/algs/FieldsCalculator.py

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,9 @@ class FieldsCalculator(GeoAlgorithm):
4747
FORMULA = "FORMULA"
4848
OUTPUT_LAYER = "OUTPUT_LAYER"
4949

50-
TYPE_NAMES = ["Integer", "Float", "String"]
51-
TYPES = [QVariant.Int, QVariant.Double, QVariant.String]
50+
TYPE_NAMES = ["Float", "Integer", "String", "Boolean"]
51+
TYPES = [QVariant.Double, QVariant.Int, QVariant.String. QVariant.Bool]
5252

53-
#===========================================================================
54-
# def getIcon(self):
55-
# return QtGui.QIcon(os.path.dirname(__file__) + "/../images/qgis.png")
56-
#===========================================================================
5753

5854
def defineCharacteristics(self):
5955
self.name = "Field calculator"
@@ -62,7 +58,7 @@ def defineCharacteristics(self):
6258
self.addParameter(ParameterString(self.FIELD_NAME, "Result field name"))
6359
self.addParameter(ParameterSelection(self.FIELD_TYPE, "Field type", self.TYPE_NAMES))
6460
self.addParameter(ParameterNumber(self.FIELD_LENGTH, "Field length", 1, 255, 10))
65-
self.addParameter(ParameterNumber(self.FIELD_PRECISION, "Field precision", 0, 10, 0))
61+
self.addParameter(ParameterNumber(self.FIELD_PRECISION, "Field precision", 0, 10, 5))
6662
self.addParameter(ParameterString(self.FORMULA, "Formula"))
6763
self.addOutput(OutputVector(self.OUTPUT_LAYER, "Output layer"))
6864

@@ -85,17 +81,20 @@ def processAlgorithm(self, progress):
8581
nFeat = provider.featureCount()
8682
nElement = 0
8783
features = QGisLayers.features(layer)
84+
85+
fieldnames = [field.name() for field in provider.fields()]
86+
fieldnames.sort(key=len, reverse=False)
87+
fieldidx = [fieldnames.index(field.name()) for field in provider.fields()]
88+
print fieldidx
8889
for inFeat in features:
8990
progress.setPercentage(int((100 * nElement) / nFeat))
9091
attrs = inFeat.attributes()
91-
expression = formula
92-
k = 0
93-
for attr in attrs:
94-
expression = expression.replace(unicode(fields[k].name()), unicode(attr))
95-
k += 1
96-
try:
92+
expression = formula
93+
for idx in fieldidx:
94+
expression = expression.replace(unicode(fields[idx].name()), unicode(attrs[idx]))
95+
try:
9796
result = eval(expression)
98-
except Exception:
97+
except Exception:
9998
result = None
10099
#raise GeoAlgorithmExecutionException("Problem evaluation formula: Wrong field values or formula")
101100
nElement += 1
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
FILE(GLOB PY_FILES *.py)
22
FILE(GLOB DESCR_FILES description/*.txt)
33

4+
ADD_SUBDIRECTORY(ext)
5+
46
PLUGIN_INSTALL(sextante saga ${PY_FILES})
57
PLUGIN_INSTALL(sextante saga/description ${DESCR_FILES})

‎python/plugins/sextante/saga/SagaAlgorithm.py

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
* *
1717
***************************************************************************
1818
"""
19+
import importlib
1920

2021
__author__ = 'Victor Olaya'
2122
__date__ = 'August 2012'
@@ -180,6 +181,8 @@ def processAlgorithm(self, progress):
180181
raise GeoAlgorithmExecutionException("SAGA folder is not configured.\nPlease configure it before running SAGA algorithms.")
181182
commands = list()
182183
self.exportedLayers = {}
184+
185+
self.preProcessInputs()
183186

184187
#1: Export rasters to sgrd and vectors to shp
185188
# Tables must be in dbf format. We check that.
@@ -306,8 +309,9 @@ def processAlgorithm(self, progress):
306309
else:
307310
commands.append("libio_gdal 1 -GRIDS \"" + filename2 + "\" -FORMAT 1 -TYPE 0 -FILE \"" + filename + "\"");
308311

309-
312+
310313
#4 Run SAGA
314+
commands = self.editCommands(commands)
311315
SagaUtils.createSagaBatchJobFileFromSagaCommands(commands)
312316
loglines = []
313317
loglines.append("SAGA execution commands")
@@ -319,6 +323,28 @@ def processAlgorithm(self, progress):
319323
SagaUtils.executeSaga(progress);
320324

321325

326+
def preProcessInputs(self):
327+
name = self.commandLineName().replace('.','_')[len('saga:'):]
328+
try:
329+
module = importlib.import_module('sextante.grass.ext.' + name)
330+
except ImportError:
331+
return
332+
if hasattr(module, 'preProcessInputs'):
333+
func = getattr(module,'preProcessInputs')
334+
func(self)
335+
336+
def editCommands(self, commands):
337+
name = self.commandLineName()[len('saga:'):]
338+
try:
339+
module = importlib.import_module('sextante.grass.ext.' + name)
340+
except ImportError:
341+
return commands
342+
if hasattr(module, 'editCommands'):
343+
func = getattr(module,'editCommands')
344+
return func(commands)
345+
else:
346+
return commands
347+
322348
def getOutputCellsize(self):
323349
'''tries to guess the cellsize of the output, searching for a parameter with an appropriate name for it'''
324350
cellsize = 0;

‎python/plugins/sextante/saga/description/SupervisedClassification.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@ ParameterSelection|RELATIVE_PROB|Probability Reference|[0] absolute;[1] relative
1111
ParameterNumber|THRESHOLD_ANGLE|Spectral Angle Threshold (Degree)|None|None|0.0
1212
OutputTable|CLASS_INFO|Class Information
1313
OutputRaster|CLASSES|Classification
14-
OutputRaster|QUALITY|Quality
14+
OutputRaster|QUALITY|Quality
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
FILE(GLOB PY_FILES *.py)
2+
3+
PLUGIN_INSTALL(sextante saga/ext ${PY_FILES})

‎python/plugins/sextante/saga/ext/__init__.py

Whitespace-only changes.
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# -*- coding: utf-8 -*-
2+
3+
"""
4+
***************************************************************************
5+
supervisedclassification.py
6+
---------------------
7+
Date : July 2013
8+
Copyright : (C) 2013 by Victor Olaya
9+
Email : volayaf at gmail dot com
10+
***************************************************************************
11+
* *
12+
* This program is free software; you can redistribute it and/or modify *
13+
* it under the terms of the GNU General Public License as published by *
14+
* the Free Software Foundation; either version 2 of the License, or *
15+
* (at your option) any later version. *
16+
* *
17+
***************************************************************************
18+
"""
19+
from sextante.tests.TestData import table
20+
__author__ = 'Victor Olaya'
21+
__date__ = 'July 2013'
22+
__copyright__ = '(C) 2013, Victor Olaya'
23+
# This will get replaced with a git SHA1 when you do a git archive
24+
__revision__ = '$Format:%H$'
25+
26+
27+
def editCommands(commands):
28+
commands[-1] = commands[-1] + " -STATS" + table()
29+
return commands
30+
31+

0 commit comments

Comments
 (0)
Please sign in to comment.