27 changes: 13 additions & 14 deletions python/plugins/sextante/algs/FieldsCalculator.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,9 @@ class FieldsCalculator(GeoAlgorithm):
FORMULA = "FORMULA"
OUTPUT_LAYER = "OUTPUT_LAYER"

TYPE_NAMES = ["Integer", "Float", "String"]
TYPES = [QVariant.Int, QVariant.Double, QVariant.String]
TYPE_NAMES = ["Float", "Integer", "String", "Boolean"]
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):
self.name = "Field calculator"
Expand All @@ -62,7 +58,7 @@ def defineCharacteristics(self):
self.addParameter(ParameterString(self.FIELD_NAME, "Result field name"))
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_PRECISION, "Field precision", 0, 10, 0))
self.addParameter(ParameterNumber(self.FIELD_PRECISION, "Field precision", 0, 10, 5))
self.addParameter(ParameterString(self.FORMULA, "Formula"))
self.addOutput(OutputVector(self.OUTPUT_LAYER, "Output layer"))

Expand All @@ -85,17 +81,20 @@ def processAlgorithm(self, progress):
nFeat = provider.featureCount()
nElement = 0
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:
progress.setPercentage(int((100 * nElement) / nFeat))
attrs = inFeat.attributes()
expression = formula
k = 0
for attr in attrs:
expression = expression.replace(unicode(fields[k].name()), unicode(attr))
k += 1
try:
expression = formula
for idx in fieldidx:
expression = expression.replace(unicode(fields[idx].name()), unicode(attrs[idx]))
try:
result = eval(expression)
except Exception:
except Exception:
result = None
#raise GeoAlgorithmExecutionException("Problem evaluation formula: Wrong field values or formula")
nElement += 1
Expand Down
5 changes: 4 additions & 1 deletion python/plugins/sextante/gui/help/algclasssification.txt
Original file line number Diff line number Diff line change
Expand Up @@ -393,4 +393,7 @@ saga:verticaldistancetochannelnetwork,USE_ORIGINAL_NAME,Domain specific/Hydrolog
saga:waterretentioncapacity,USE_ORIGINAL_NAME,Domain specific/Hydrology
saga:watershedbasins,USE_ORIGINAL_NAME,Domain specific/Hydrology
saga:windeffectwindwardleewardindex,USE_ORIGINAL_NAME,Domain specific/Terrain analysis and geomorphometry
saga:zonalgridstatistics,USE_ORIGINAL_NAME,Raster/Statistics
saga:zonalgridstatistics,USE_ORIGINAL_NAME,Raster/Statistics
modelertools:calculator,USE_ORIGINAL_NAME,Modeler/Modeler tools
modelertools:rasterlayerbounds,USE_ORIGINAL_NAME,Modeler/Modeler tools
modelertools:vectorlayerbounds,USE_ORIGINAL_NAME,Modeler/Modeler tools
2 changes: 1 addition & 1 deletion python/plugins/sextante/modeler/models/notinorder.model
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ None
None
None
None
ALGORITHM:saga:fillsinksplanchondarboux2001
ALGORITHM:saga:fillsinks
340.0,260.0
None
-1|RASTERLAYER_RASTER
Expand Down
2 changes: 1 addition & 1 deletion python/plugins/sextante/modeler/models/watersheds.model
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ VALUE:HARDCODEDPARAMVALUE_LINEARTHRS_1===500.0
VALUE:HARDCODEDPARAMVALUE_DIV_CELLS_2===10
VALUE:HARDCODEDPARAMVALUE_CONVERGENCE_1===1.0
VALUE:HARDCODEDPARAMVALUE_MINLEN_2===10
ALGORITHM:saga:fillsinksplanchondarboux2001
ALGORITHM:saga:fillsinks
120.0,160.0
None
-1|RASTERLAYER_DEM
Expand Down
2 changes: 2 additions & 0 deletions python/plugins/sextante/saga/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
FILE(GLOB PY_FILES *.py)
FILE(GLOB DESCR_FILES description/*.txt)

ADD_SUBDIRECTORY(ext)

PLUGIN_INSTALL(sextante saga ${PY_FILES})
PLUGIN_INSTALL(sextante saga/description ${DESCR_FILES})
28 changes: 27 additions & 1 deletion python/plugins/sextante/saga/SagaAlgorithm.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
__revision__ = '$Format:%H$'

import os
import importlib
from qgis.core import *
from PyQt4.QtCore import *
from PyQt4.QtGui import *
Expand Down Expand Up @@ -180,6 +181,8 @@ def processAlgorithm(self, progress):
raise GeoAlgorithmExecutionException("SAGA folder is not configured.\nPlease configure it before running SAGA algorithms.")
commands = list()
self.exportedLayers = {}

self.preProcessInputs()

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


#4 Run SAGA
commands = self.editCommands(commands)
SagaUtils.createSagaBatchJobFileFromSagaCommands(commands)
loglines = []
loglines.append("SAGA execution commands")
Expand All @@ -319,6 +323,28 @@ def processAlgorithm(self, 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.saga.ext.' + name)
except ImportError:
return commands
if hasattr(module, 'editCommands'):
func = getattr(module,'editCommands')
return func(commands)
else:
return commands

def getOutputCellsize(self):
'''tries to guess the cellsize of the output, searching for a parameter with an appropriate name for it'''
cellsize = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ ParameterSelection|RELATIVE_PROB|Probability Reference|[0] absolute;[1] relative
ParameterNumber|THRESHOLD_ANGLE|Spectral Angle Threshold (Degree)|None|None|0.0
OutputTable|CLASS_INFO|Class Information
OutputRaster|CLASSES|Classification
OutputRaster|QUALITY|Quality
OutputRaster|QUALITY|Quality
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ ParameterRaster|DEM|Elevation|False
ParameterNumber|RADIUS_A_MIN|Min Radius A|None|None|0
ParameterNumber|RADIUS_A_MAX|Max Radius A|None|None|100
ParameterNumber|RADIUS_B_MIN|Min Radius B|None|None|0
ParameterNumber|RADIUS_B_MAX|Min Radius B|None|None|1000
ParameterNumber|RADIUS_B_MAX|Max Radius B|None|None|1000
ParameterSelection|DISTANCE_WEIGHTING_WEIGHTING|Distance Weighting|[0] no distance weighting;[1] inverse distance to a power;[2] exponential;[3] gaussian weighting
ParameterNumber|DISTANCE_WEIGHTING_IDW_POWER|Inverse Distance Weighting Power|0.0|None|1
ParameterBoolean|DISTANCE_WEIGHTING_IDW_OFFSET |Inverse Distance Offset|True
Expand Down
3 changes: 3 additions & 0 deletions python/plugins/sextante/saga/ext/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
FILE(GLOB PY_FILES *.py)

PLUGIN_INSTALL(sextante saga/ext ${PY_FILES})
Empty file.
32 changes: 32 additions & 0 deletions python/plugins/sextante/saga/ext/supervisedclassification.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# -*- 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. *
* *
***************************************************************************
"""

__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$'

from sextante.tests.TestData import table

def editCommands(commands):
commands[-3] = commands[-3] + " -STATS " + table()
return commands