Skip to content

Commit

Permalink
iterative execution now works without problems
Browse files Browse the repository at this point in the history
changed names of providers in settings

git-svn-id: http://sextante.googlecode.com/svn/trunk/soft/bindings/qgis-plugin@152 881b9c09-3ef8-f3c2-ec3d-21d735c97f4d
  • Loading branch information
volayaf committed Apr 25, 2012
1 parent a991808 commit 933eb9b
Show file tree
Hide file tree
Showing 14 changed files with 119 additions and 36 deletions.
1 change: 1 addition & 0 deletions src/sextante/algs/FieldsCalculator.py
Expand Up @@ -58,4 +58,5 @@ def processAlgorithm(self, progress):

def checkParameterValuesBeforeExecuting(self):
##TODO check that formula is correct and fields exist
pass

75 changes: 75 additions & 0 deletions src/sextante/algs/SaveSelectedFeatures.py
@@ -0,0 +1,75 @@
from sextante.core.GeoAlgorithm import GeoAlgorithm
from sextante.outputs.OutputVector import OutputVector
from sextante.parameters.ParameterVector import ParameterVector
from qgis.core import *
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from sextante.core.QGisLayers import QGisLayers


class SaveSelectedFeatures(GeoAlgorithm):
'''This is an example algorithm that takes a vector layer and creates
a new one just with just those features of the input layer that are
selected.
It is meant to be used as an example of how to create your own SEXTANTE
algorithms and explain methods and variables used to do it.
An algorithm like this will be available in all SEXTANTE elements, and
there is not need for additional work.
All SEXTANTE algorithms should extend the GeoAlgorithm class'''

#constants used to refer to parameters and outputs.
#They will be used when calling the algorithm from another algorithm,
#or when calling SEXTANTE from the QGIS console.
OUTPUT_LAYER = "OUTPUT_LAYER"
INPUT_LAYER = "INPUT_LAYER"

def defineCharacteristics(self):
'''Here we define the inputs and output of the algorithm, along
with some other properties'''

#the name that the user will see in the toolbox
self.name = "Create new layer with selected features"

#the branch of the toolbox under which the algorithm will appear
self.group = "Algorithms for vector layers"

#we add the input vector layer. It can have any kind of geometry
#It is a mandatory (not optional) one, hence the False argument
self.addParameter(ParameterVector(self.INPUT_LAYER, "Input layer", ParameterVector.VECTOR_TYPE_ANY, False))
# we add a vector layer as output
self.addOutput(OutputVector(self.OUTPUT_LAYER, "Output layer with selected features"))


def processAlgorithm(self, progress):
'''Here is where the processing itself takes place'''

#the first thing to do is retrieve the values of the parameters
#entered by the user
inputFilename = self.getParameterValue(self.INPUT_LAYER)
output = self.getOutputValue(self.OUTPUT_LAYER)

#input layers values are always a string with its location.
#That string can be converted into a QGIS object (a QgsVectorLayer in this case))
#using the Sextante.getObject() method
vectorLayer = QGisLayers.getObjectFromUri(inputFilename)

#And now we can process

#First we create the output layer.
#The output value entered by the user is a string containing a filename,
#so we can use it directly
settings = QSettings()
systemEncoding = settings.value( "/UI/encoding", "System" ).toString()
provider = vectorLayer.dataProvider()
writer = QgsVectorFileWriter( output, systemEncoding, provider.fields(), provider.geometryType(), provider.crs() )

#Now we take the selected features and add them to the output layer
selection = vectorLayer.selectedFeatures()
for feat in selection:
writer.addFeature(feat)
del writer

#There is nothing more to do here. We do not have to open the layer that we have created.
#SEXTANTE will take care of that, or will handle it if this algorithm is executed within
#a complex model
3 changes: 2 additions & 1 deletion src/sextante/algs/SextanteAlgorithmProvider.py
Expand Up @@ -3,12 +3,13 @@
from PyQt4 import QtGui
import os
from sextante.algs.FieldsCalculator import FieldsCalculator
from sextante.algs.SaveSelectedFeatures import SaveSelectedFeatures

class SextanteAlgorithmProvider(AlgorithmProvider):

def __init__(self):
AlgorithmProvider.__init__(self)
self.alglist = [AddTableField(), FieldsCalculator()]
self.alglist = [AddTableField(), FieldsCalculator(), SaveSelectedFeatures()]

def initializeSettings(self):
AlgorithmProvider.initializeSettings(self)
Expand Down
2 changes: 1 addition & 1 deletion src/sextante/core/AlgorithmProvider.py
Expand Up @@ -37,7 +37,7 @@ def initializeSettings(self):
By default it just adds a setting to activate or deactivate algorithms from the provider'''
SextanteConfig.settingIcons[self.getName()] = self.getIcon()
name = "ACTIVATE_" + self.getName().upper().replace(" ", "_")
SextanteConfig.addSetting(Setting(self.getName(), name, "Activate", True))
SextanteConfig.addSetting(Setting(self.getDescription(), name, "Activate", True))

def unload(self):
'''Do here anything that you want to be done when the provider is removed from the list of available ones.
Expand Down
21 changes: 10 additions & 11 deletions src/sextante/grass/GrassAlgorithmProvider.py
Expand Up @@ -20,17 +20,16 @@ def __init__(self):
def initializeSettings(self):
AlgorithmProvider.initializeSettings(self)
if SextanteUtils.isWindows():
SextanteConfig.addSetting(Setting("GRASS", GrassUtils.GRASS_FOLDER, "GRASS folder", GrassUtils.grassPath()))
SextanteConfig.addSetting(Setting("GRASS", GrassUtils.GRASS_WIN_SHELL, "Msys folder", GrassUtils.grassWinShell()))
SextanteConfig.addSetting(Setting("GRASS", GrassUtils.GRASS_AUTO_REGION, "Use min covering region", True))
SextanteConfig.addSetting(Setting("GRASS", GrassUtils.GRASS_LATLON, "Coordinates are lat/lon", False))
SextanteConfig.addSetting(Setting("GRASS", GrassUtils.GRASS_REGION_XMIN, "GRASS Region min x", 0))
SextanteConfig.addSetting(Setting("GRASS", GrassUtils.GRASS_REGION_YMIN, "GRASS Region min y", 0))
SextanteConfig.addSetting(Setting("GRASS", GrassUtils.GRASS_REGION_XMAX, "GRASS Region max x", 1000))
SextanteConfig.addSetting(Setting("GRASS", GrassUtils.GRASS_REGION_YMAX, "GRASS Region max y", 1000))
SextanteConfig.addSetting(Setting("GRASS", GrassUtils.GRASS_REGION_CELLSIZE, "GRASS Region cellsize", 1))

SextanteConfig.addSetting(Setting("GRASS", GrassUtils.GRASS_HELP_FOLDER, "GRASS help folder", GrassUtils.grassHelpPath()))
SextanteConfig.addSetting(Setting(self.getDescription(), GrassUtils.GRASS_FOLDER, "GRASS folder", GrassUtils.grassPath()))
SextanteConfig.addSetting(Setting(self.getDescription(), GrassUtils.GRASS_WIN_SHELL, "Msys folder", GrassUtils.grassWinShell()))
SextanteConfig.addSetting(Setting(self.getDescription(), GrassUtils.GRASS_AUTO_REGION, "Use min covering region", True))
SextanteConfig.addSetting(Setting(self.getDescription(), GrassUtils.GRASS_LATLON, "Coordinates are lat/lon", False))
SextanteConfig.addSetting(Setting(self.getDescription(), GrassUtils.GRASS_REGION_XMIN, "GRASS Region min x", 0))
SextanteConfig.addSetting(Setting(self.getDescription(), GrassUtils.GRASS_REGION_YMIN, "GRASS Region min y", 0))
SextanteConfig.addSetting(Setting(self.getDescription(), GrassUtils.GRASS_REGION_XMAX, "GRASS Region max x", 1000))
SextanteConfig.addSetting(Setting(self.getDescription(), GrassUtils.GRASS_REGION_YMAX, "GRASS Region max y", 1000))
SextanteConfig.addSetting(Setting(self.getDescription(), GrassUtils.GRASS_REGION_CELLSIZE, "GRASS Region cellsize", 1))
SextanteConfig.addSetting(Setting(self.getDescription(), GrassUtils.GRASS_HELP_FOLDER, "GRASS help folder", GrassUtils.grassHelpPath()))

def unload(self):
AlgorithmProvider.unload(self)
Expand Down
19 changes: 13 additions & 6 deletions src/sextante/gui/AlgorithmExecutor.py
@@ -1,7 +1,10 @@
from PyQt4.QtGui import *
from PyQt4.QtCore import *
from qgis.core import *
from sextante.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException
from sextante.core.QGisLayers import QGisLayers
from sextante.core.SextanteUtils import SextanteUtils
from sextante.gui.SextantePostprocessing import SextantePostprocessing

class AlgorithmExecutor:

Expand All @@ -22,7 +25,7 @@ def runalgIterating(alg,paramToIter,progress):
#generate all single-feature layers
settings = QSettings()
systemEncoding = settings.value( "/UI/encoding", "System" ).toString()
layerfile = alg.getParameterFromName(paramToIter)
layerfile = alg.getParameterValue(paramToIter)
layer = QGisLayers.getObjectFromUri(layerfile, False)
provider = layer.dataProvider()
allAttrs = provider.attributeIndexes()
Expand All @@ -36,20 +39,24 @@ def runalgIterating(alg,paramToIter,progress):
writer = QgsVectorFileWriter(output, systemEncoding,provider.fields(), provider.geometryType(), provider.crs() )
writer.addFeature(feat)
del writer
#now run all the algorithms

#store output values to use them later as basenames for all outputs
for out in alg.outputs:
output[out.name] = out.value
outputs[out.name] = out.value

#now run all the algorithms
i = 1
for f in filelist:
alg.setOutputValue(paramToIter, f)
alg.setParameterValue(paramToIter, f)
for out in alg.outputs:
filename = outputs[out.name]
if filename:
filename = filename[:filename.rfind(".")] + "_" + str(i) + filename[filename.rfind("."):]
filename = filename[:filename.rfind(".")] + "_" + str(i) + filename[filename.rfind("."):]
out.value = filename
progress.setText("Executing iteration " + str(i) + "/" + str(len(filelist)) + "...")
progress.setPercentage((i * 100) / len(filelist))
if AlgorithmExecutor.runalg(alg, SilentProgress()):
progress.setValue(i/len(f))
SextantePostprocessing.handleAlgorithmResults(alg, False)
i+=1
else:
return False;
Expand Down
1 change: 0 additions & 1 deletion src/sextante/gui/ParametersDialog.py
Expand Up @@ -191,7 +191,6 @@ def reject(self):
def setPercentage(self, i):
self.progress.setValue(i)


def setText(self, text):
self.progressLabel.setText(text)

Expand Down
2 changes: 1 addition & 1 deletion src/sextante/lastools/LasToolsAlgorithmProvider.py
Expand Up @@ -25,7 +25,7 @@ def __init__(self):

def initializeSettings(self):
AlgorithmProvider.initializeSettings(self)
SextanteConfig.addSetting(Setting("LASTools", LasToolsUtils.LASTOOLS_FOLDER, "LASTools folder", LasToolsUtils.LasToolsPath()))
SextanteConfig.addSetting(Setting(self.getDescription(), LasToolsUtils.LASTOOLS_FOLDER, "LASTools folder", LasToolsUtils.LasToolsPath()))

def getName(self):
return "lastools"
Expand Down
1 change: 1 addition & 0 deletions src/sextante/mmqgis/MMQGISAlgorithmProvider.py
Expand Up @@ -17,6 +17,7 @@ def scriptsFolder(self):

def getDescription(self):
return "MMQGIS (Vector and table tools)"

def getName(self):
return "mmqgis"

Expand Down
2 changes: 1 addition & 1 deletion src/sextante/modeler/ModelerAlgorithmProvider.py
Expand Up @@ -21,7 +21,7 @@ def __init__(self):

def initializeSettings(self):
AlgorithmProvider.initializeSettings(self)
SextanteConfig.addSetting(Setting("Modeler", ModelerUtils.MODELS_FOLDER, "Models folder", ModelerUtils.modelsFolder()))
SextanteConfig.addSetting(Setting(self.getDescription(), ModelerUtils.MODELS_FOLDER, "Models folder", ModelerUtils.modelsFolder()))

def setAlgsList(self, algs):
ModelerUtils.allAlgs = algs
Expand Down
8 changes: 4 additions & 4 deletions src/sextante/otb/OTBAlgorithmProvider.py
Expand Up @@ -41,10 +41,10 @@ def createAlgsList(self):

def initializeSettings(self):
AlgorithmProvider.initializeSettings(self)
SextanteConfig.addSetting(Setting("OTB", OTBUtils.OTB_FOLDER, "OTB command line tools folder", OTBUtils.otbPath()))
SextanteConfig.addSetting(Setting("OTB", OTBUtils.OTB_LIB_FOLDER, "OTB applications folder", OTBUtils.otbLibPath()))
SextanteConfig.addSetting(Setting("OTB", OTBUtils.OTB_SRTM_FOLDER, "SRTM tiles folder", OTBUtils.otbSRTMPath()))
SextanteConfig.addSetting(Setting("OTB", OTBUtils.OTB_GEOID_FILE, "Geoid file", OTBUtils.otbGeoidPath()))
SextanteConfig.addSetting(Setting(self.getDescription(), OTBUtils.OTB_FOLDER, "OTB command line tools folder", OTBUtils.otbPath()))
SextanteConfig.addSetting(Setting(self.getDescription(), OTBUtils.OTB_LIB_FOLDER, "OTB applications folder", OTBUtils.otbLibPath()))
SextanteConfig.addSetting(Setting(self.getDescription(), OTBUtils.OTB_SRTM_FOLDER, "SRTM tiles folder", OTBUtils.otbSRTMPath()))
SextanteConfig.addSetting(Setting(self.getDescription(), OTBUtils.OTB_GEOID_FILE, "Geoid file", OTBUtils.otbGeoidPath()))

def unload(self):
AlgorithmProvider.unload(self)
Expand Down
4 changes: 2 additions & 2 deletions src/sextante/r/RAlgorithmProvider.py
Expand Up @@ -21,9 +21,9 @@ def __init__(self):

def initializeSettings(self):
AlgorithmProvider.initializeSettings(self)
SextanteConfig.addSetting(Setting("R", RUtils.RSCRIPTS_FOLDER, "R Scripts folder", RUtils.RScriptsFolder()))
SextanteConfig.addSetting(Setting(self.getDescription(), RUtils.RSCRIPTS_FOLDER, "R Scripts folder", RUtils.RScriptsFolder()))
if SextanteUtils.isWindows():
SextanteConfig.addSetting(Setting("R", RUtils.R_FOLDER, "R folder", RUtils.RFolder()))
SextanteConfig.addSetting(Setting(self.getDescription(), RUtils.R_FOLDER, "R folder", RUtils.RFolder()))

def unload(self):
AlgorithmProvider.unload(self)
Expand Down
14 changes: 7 additions & 7 deletions src/sextante/saga/SagaAlgorithmProvider.py
Expand Up @@ -18,13 +18,13 @@ def __init__(self):
def initializeSettings(self):
AlgorithmProvider.initializeSettings(self)
if SextanteUtils.isWindows():
SextanteConfig.addSetting(Setting("SAGA", SagaUtils.SAGA_FOLDER, "SAGA folder", SagaUtils.sagaPath()))
SextanteConfig.addSetting(Setting("SAGA", SagaUtils.SAGA_AUTO_RESAMPLING, "Use min covering grid system for resampling", True))
SextanteConfig.addSetting(Setting("SAGA", SagaUtils.SAGA_RESAMPLING_REGION_XMIN, "Resampling region min x", 0))
SextanteConfig.addSetting(Setting("SAGA", SagaUtils.SAGA_RESAMPLING_REGION_YMIN, "Resampling region min y", 0))
SextanteConfig.addSetting(Setting("SAGA", SagaUtils.SAGA_RESAMPLING_REGION_XMAX, "Resampling region max x", 1000))
SextanteConfig.addSetting(Setting("SAGA", SagaUtils.SAGA_RESAMPLING_REGION_YMAX, "Resampling region max y", 1000))
SextanteConfig.addSetting(Setting("SAGA", SagaUtils.SAGA_RESAMPLING_REGION_CELLSIZE, "Resampling region cellsize", 1))
SextanteConfig.addSetting(Setting(self.getDescription(), SagaUtils.SAGA_FOLDER, "SAGA folder", SagaUtils.sagaPath()))
SextanteConfig.addSetting(Setting(self.getDescription(), SagaUtils.SAGA_AUTO_RESAMPLING, "Use min covering grid system for resampling", True))
SextanteConfig.addSetting(Setting(self.getDescription(), SagaUtils.SAGA_RESAMPLING_REGION_XMIN, "Resampling region min x", 0))
SextanteConfig.addSetting(Setting(self.getDescription(), SagaUtils.SAGA_RESAMPLING_REGION_YMIN, "Resampling region min y", 0))
SextanteConfig.addSetting(Setting(self.getDescription(), SagaUtils.SAGA_RESAMPLING_REGION_XMAX, "Resampling region max x", 1000))
SextanteConfig.addSetting(Setting(self.getDescription(), SagaUtils.SAGA_RESAMPLING_REGION_YMAX, "Resampling region max y", 1000))
SextanteConfig.addSetting(Setting(self.getDescription(), SagaUtils.SAGA_RESAMPLING_REGION_CELLSIZE, "Resampling region cellsize", 1))

def unload(self):
AlgorithmProvider.unload(self)
Expand Down
2 changes: 1 addition & 1 deletion src/sextante/script/ScriptAlgorithmProvider.py
Expand Up @@ -21,7 +21,7 @@ def __init__(self):

def initializeSettings(self):
AlgorithmProvider.initializeSettings(self)
SextanteConfig.addSetting(Setting("Scripts", ScriptUtils.SCRIPTS_FOLDER, "Scripts folder", ScriptUtils.scriptsFolder()))
SextanteConfig.addSetting(Setting(self.getDescription(), ScriptUtils.SCRIPTS_FOLDER, "Scripts folder", ScriptUtils.scriptsFolder()))

def unload(self):
AlgorithmProvider.unload(self)
Expand Down

0 comments on commit 933eb9b

Please sign in to comment.