Skip to content

Commit 933eb9b

Browse files
author
volayaf
committed
iterative execution now works without problems
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
1 parent a991808 commit 933eb9b

14 files changed

+119
-36
lines changed

src/sextante/algs/FieldsCalculator.py

+1
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,5 @@ def processAlgorithm(self, progress):
5858

5959
def checkParameterValuesBeforeExecuting(self):
6060
##TODO check that formula is correct and fields exist
61+
pass
6162

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
from sextante.core.GeoAlgorithm import GeoAlgorithm
2+
from sextante.outputs.OutputVector import OutputVector
3+
from sextante.parameters.ParameterVector import ParameterVector
4+
from qgis.core import *
5+
from PyQt4.QtCore import *
6+
from PyQt4.QtGui import *
7+
from sextante.core.QGisLayers import QGisLayers
8+
9+
10+
class SaveSelectedFeatures(GeoAlgorithm):
11+
'''This is an example algorithm that takes a vector layer and creates
12+
a new one just with just those features of the input layer that are
13+
selected.
14+
It is meant to be used as an example of how to create your own SEXTANTE
15+
algorithms and explain methods and variables used to do it.
16+
An algorithm like this will be available in all SEXTANTE elements, and
17+
there is not need for additional work.
18+
19+
All SEXTANTE algorithms should extend the GeoAlgorithm class'''
20+
21+
#constants used to refer to parameters and outputs.
22+
#They will be used when calling the algorithm from another algorithm,
23+
#or when calling SEXTANTE from the QGIS console.
24+
OUTPUT_LAYER = "OUTPUT_LAYER"
25+
INPUT_LAYER = "INPUT_LAYER"
26+
27+
def defineCharacteristics(self):
28+
'''Here we define the inputs and output of the algorithm, along
29+
with some other properties'''
30+
31+
#the name that the user will see in the toolbox
32+
self.name = "Create new layer with selected features"
33+
34+
#the branch of the toolbox under which the algorithm will appear
35+
self.group = "Algorithms for vector layers"
36+
37+
#we add the input vector layer. It can have any kind of geometry
38+
#It is a mandatory (not optional) one, hence the False argument
39+
self.addParameter(ParameterVector(self.INPUT_LAYER, "Input layer", ParameterVector.VECTOR_TYPE_ANY, False))
40+
# we add a vector layer as output
41+
self.addOutput(OutputVector(self.OUTPUT_LAYER, "Output layer with selected features"))
42+
43+
44+
def processAlgorithm(self, progress):
45+
'''Here is where the processing itself takes place'''
46+
47+
#the first thing to do is retrieve the values of the parameters
48+
#entered by the user
49+
inputFilename = self.getParameterValue(self.INPUT_LAYER)
50+
output = self.getOutputValue(self.OUTPUT_LAYER)
51+
52+
#input layers values are always a string with its location.
53+
#That string can be converted into a QGIS object (a QgsVectorLayer in this case))
54+
#using the Sextante.getObject() method
55+
vectorLayer = QGisLayers.getObjectFromUri(inputFilename)
56+
57+
#And now we can process
58+
59+
#First we create the output layer.
60+
#The output value entered by the user is a string containing a filename,
61+
#so we can use it directly
62+
settings = QSettings()
63+
systemEncoding = settings.value( "/UI/encoding", "System" ).toString()
64+
provider = vectorLayer.dataProvider()
65+
writer = QgsVectorFileWriter( output, systemEncoding, provider.fields(), provider.geometryType(), provider.crs() )
66+
67+
#Now we take the selected features and add them to the output layer
68+
selection = vectorLayer.selectedFeatures()
69+
for feat in selection:
70+
writer.addFeature(feat)
71+
del writer
72+
73+
#There is nothing more to do here. We do not have to open the layer that we have created.
74+
#SEXTANTE will take care of that, or will handle it if this algorithm is executed within
75+
#a complex model

src/sextante/algs/SextanteAlgorithmProvider.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@
33
from PyQt4 import QtGui
44
import os
55
from sextante.algs.FieldsCalculator import FieldsCalculator
6+
from sextante.algs.SaveSelectedFeatures import SaveSelectedFeatures
67

78
class SextanteAlgorithmProvider(AlgorithmProvider):
89

910
def __init__(self):
1011
AlgorithmProvider.__init__(self)
11-
self.alglist = [AddTableField(), FieldsCalculator()]
12+
self.alglist = [AddTableField(), FieldsCalculator(), SaveSelectedFeatures()]
1213

1314
def initializeSettings(self):
1415
AlgorithmProvider.initializeSettings(self)

src/sextante/core/AlgorithmProvider.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def initializeSettings(self):
3737
By default it just adds a setting to activate or deactivate algorithms from the provider'''
3838
SextanteConfig.settingIcons[self.getName()] = self.getIcon()
3939
name = "ACTIVATE_" + self.getName().upper().replace(" ", "_")
40-
SextanteConfig.addSetting(Setting(self.getName(), name, "Activate", True))
40+
SextanteConfig.addSetting(Setting(self.getDescription(), name, "Activate", True))
4141

4242
def unload(self):
4343
'''Do here anything that you want to be done when the provider is removed from the list of available ones.

src/sextante/grass/GrassAlgorithmProvider.py

+10-11
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,16 @@ def __init__(self):
2020
def initializeSettings(self):
2121
AlgorithmProvider.initializeSettings(self)
2222
if SextanteUtils.isWindows():
23-
SextanteConfig.addSetting(Setting("GRASS", GrassUtils.GRASS_FOLDER, "GRASS folder", GrassUtils.grassPath()))
24-
SextanteConfig.addSetting(Setting("GRASS", GrassUtils.GRASS_WIN_SHELL, "Msys folder", GrassUtils.grassWinShell()))
25-
SextanteConfig.addSetting(Setting("GRASS", GrassUtils.GRASS_AUTO_REGION, "Use min covering region", True))
26-
SextanteConfig.addSetting(Setting("GRASS", GrassUtils.GRASS_LATLON, "Coordinates are lat/lon", False))
27-
SextanteConfig.addSetting(Setting("GRASS", GrassUtils.GRASS_REGION_XMIN, "GRASS Region min x", 0))
28-
SextanteConfig.addSetting(Setting("GRASS", GrassUtils.GRASS_REGION_YMIN, "GRASS Region min y", 0))
29-
SextanteConfig.addSetting(Setting("GRASS", GrassUtils.GRASS_REGION_XMAX, "GRASS Region max x", 1000))
30-
SextanteConfig.addSetting(Setting("GRASS", GrassUtils.GRASS_REGION_YMAX, "GRASS Region max y", 1000))
31-
SextanteConfig.addSetting(Setting("GRASS", GrassUtils.GRASS_REGION_CELLSIZE, "GRASS Region cellsize", 1))
32-
33-
SextanteConfig.addSetting(Setting("GRASS", GrassUtils.GRASS_HELP_FOLDER, "GRASS help folder", GrassUtils.grassHelpPath()))
23+
SextanteConfig.addSetting(Setting(self.getDescription(), GrassUtils.GRASS_FOLDER, "GRASS folder", GrassUtils.grassPath()))
24+
SextanteConfig.addSetting(Setting(self.getDescription(), GrassUtils.GRASS_WIN_SHELL, "Msys folder", GrassUtils.grassWinShell()))
25+
SextanteConfig.addSetting(Setting(self.getDescription(), GrassUtils.GRASS_AUTO_REGION, "Use min covering region", True))
26+
SextanteConfig.addSetting(Setting(self.getDescription(), GrassUtils.GRASS_LATLON, "Coordinates are lat/lon", False))
27+
SextanteConfig.addSetting(Setting(self.getDescription(), GrassUtils.GRASS_REGION_XMIN, "GRASS Region min x", 0))
28+
SextanteConfig.addSetting(Setting(self.getDescription(), GrassUtils.GRASS_REGION_YMIN, "GRASS Region min y", 0))
29+
SextanteConfig.addSetting(Setting(self.getDescription(), GrassUtils.GRASS_REGION_XMAX, "GRASS Region max x", 1000))
30+
SextanteConfig.addSetting(Setting(self.getDescription(), GrassUtils.GRASS_REGION_YMAX, "GRASS Region max y", 1000))
31+
SextanteConfig.addSetting(Setting(self.getDescription(), GrassUtils.GRASS_REGION_CELLSIZE, "GRASS Region cellsize", 1))
32+
SextanteConfig.addSetting(Setting(self.getDescription(), GrassUtils.GRASS_HELP_FOLDER, "GRASS help folder", GrassUtils.grassHelpPath()))
3433

3534
def unload(self):
3635
AlgorithmProvider.unload(self)

src/sextante/gui/AlgorithmExecutor.py

+13-6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
from PyQt4.QtGui import *
2+
from PyQt4.QtCore import *
3+
from qgis.core import *
24
from sextante.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException
35
from sextante.core.QGisLayers import QGisLayers
46
from sextante.core.SextanteUtils import SextanteUtils
7+
from sextante.gui.SextantePostprocessing import SextantePostprocessing
58

69
class AlgorithmExecutor:
710

@@ -22,7 +25,7 @@ def runalgIterating(alg,paramToIter,progress):
2225
#generate all single-feature layers
2326
settings = QSettings()
2427
systemEncoding = settings.value( "/UI/encoding", "System" ).toString()
25-
layerfile = alg.getParameterFromName(paramToIter)
28+
layerfile = alg.getParameterValue(paramToIter)
2629
layer = QGisLayers.getObjectFromUri(layerfile, False)
2730
provider = layer.dataProvider()
2831
allAttrs = provider.attributeIndexes()
@@ -36,20 +39,24 @@ def runalgIterating(alg,paramToIter,progress):
3639
writer = QgsVectorFileWriter(output, systemEncoding,provider.fields(), provider.geometryType(), provider.crs() )
3740
writer.addFeature(feat)
3841
del writer
39-
#now run all the algorithms
42+
43+
#store output values to use them later as basenames for all outputs
4044
for out in alg.outputs:
41-
output[out.name] = out.value
45+
outputs[out.name] = out.value
4246

47+
#now run all the algorithms
4348
i = 1
4449
for f in filelist:
45-
alg.setOutputValue(paramToIter, f)
50+
alg.setParameterValue(paramToIter, f)
4651
for out in alg.outputs:
4752
filename = outputs[out.name]
4853
if filename:
49-
filename = filename[:filename.rfind(".")] + "_" + str(i) + filename[filename.rfind("."):]
54+
filename = filename[:filename.rfind(".")] + "_" + str(i) + filename[filename.rfind("."):]
5055
out.value = filename
56+
progress.setText("Executing iteration " + str(i) + "/" + str(len(filelist)) + "...")
57+
progress.setPercentage((i * 100) / len(filelist))
5158
if AlgorithmExecutor.runalg(alg, SilentProgress()):
52-
progress.setValue(i/len(f))
59+
SextantePostprocessing.handleAlgorithmResults(alg, False)
5360
i+=1
5461
else:
5562
return False;

src/sextante/gui/ParametersDialog.py

-1
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,6 @@ def reject(self):
191191
def setPercentage(self, i):
192192
self.progress.setValue(i)
193193

194-
195194
def setText(self, text):
196195
self.progressLabel.setText(text)
197196

src/sextante/lastools/LasToolsAlgorithmProvider.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def __init__(self):
2525

2626
def initializeSettings(self):
2727
AlgorithmProvider.initializeSettings(self)
28-
SextanteConfig.addSetting(Setting("LASTools", LasToolsUtils.LASTOOLS_FOLDER, "LASTools folder", LasToolsUtils.LasToolsPath()))
28+
SextanteConfig.addSetting(Setting(self.getDescription(), LasToolsUtils.LASTOOLS_FOLDER, "LASTools folder", LasToolsUtils.LasToolsPath()))
2929

3030
def getName(self):
3131
return "lastools"

src/sextante/mmqgis/MMQGISAlgorithmProvider.py

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ def scriptsFolder(self):
1717

1818
def getDescription(self):
1919
return "MMQGIS (Vector and table tools)"
20+
2021
def getName(self):
2122
return "mmqgis"
2223

src/sextante/modeler/ModelerAlgorithmProvider.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ def __init__(self):
2121

2222
def initializeSettings(self):
2323
AlgorithmProvider.initializeSettings(self)
24-
SextanteConfig.addSetting(Setting("Modeler", ModelerUtils.MODELS_FOLDER, "Models folder", ModelerUtils.modelsFolder()))
24+
SextanteConfig.addSetting(Setting(self.getDescription(), ModelerUtils.MODELS_FOLDER, "Models folder", ModelerUtils.modelsFolder()))
2525

2626
def setAlgsList(self, algs):
2727
ModelerUtils.allAlgs = algs

src/sextante/otb/OTBAlgorithmProvider.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,10 @@ def createAlgsList(self):
4141

4242
def initializeSettings(self):
4343
AlgorithmProvider.initializeSettings(self)
44-
SextanteConfig.addSetting(Setting("OTB", OTBUtils.OTB_FOLDER, "OTB command line tools folder", OTBUtils.otbPath()))
45-
SextanteConfig.addSetting(Setting("OTB", OTBUtils.OTB_LIB_FOLDER, "OTB applications folder", OTBUtils.otbLibPath()))
46-
SextanteConfig.addSetting(Setting("OTB", OTBUtils.OTB_SRTM_FOLDER, "SRTM tiles folder", OTBUtils.otbSRTMPath()))
47-
SextanteConfig.addSetting(Setting("OTB", OTBUtils.OTB_GEOID_FILE, "Geoid file", OTBUtils.otbGeoidPath()))
44+
SextanteConfig.addSetting(Setting(self.getDescription(), OTBUtils.OTB_FOLDER, "OTB command line tools folder", OTBUtils.otbPath()))
45+
SextanteConfig.addSetting(Setting(self.getDescription(), OTBUtils.OTB_LIB_FOLDER, "OTB applications folder", OTBUtils.otbLibPath()))
46+
SextanteConfig.addSetting(Setting(self.getDescription(), OTBUtils.OTB_SRTM_FOLDER, "SRTM tiles folder", OTBUtils.otbSRTMPath()))
47+
SextanteConfig.addSetting(Setting(self.getDescription(), OTBUtils.OTB_GEOID_FILE, "Geoid file", OTBUtils.otbGeoidPath()))
4848

4949
def unload(self):
5050
AlgorithmProvider.unload(self)

src/sextante/r/RAlgorithmProvider.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ def __init__(self):
2121

2222
def initializeSettings(self):
2323
AlgorithmProvider.initializeSettings(self)
24-
SextanteConfig.addSetting(Setting("R", RUtils.RSCRIPTS_FOLDER, "R Scripts folder", RUtils.RScriptsFolder()))
24+
SextanteConfig.addSetting(Setting(self.getDescription(), RUtils.RSCRIPTS_FOLDER, "R Scripts folder", RUtils.RScriptsFolder()))
2525
if SextanteUtils.isWindows():
26-
SextanteConfig.addSetting(Setting("R", RUtils.R_FOLDER, "R folder", RUtils.RFolder()))
26+
SextanteConfig.addSetting(Setting(self.getDescription(), RUtils.R_FOLDER, "R folder", RUtils.RFolder()))
2727

2828
def unload(self):
2929
AlgorithmProvider.unload(self)

src/sextante/saga/SagaAlgorithmProvider.py

+7-7
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,13 @@ def __init__(self):
1818
def initializeSettings(self):
1919
AlgorithmProvider.initializeSettings(self)
2020
if SextanteUtils.isWindows():
21-
SextanteConfig.addSetting(Setting("SAGA", SagaUtils.SAGA_FOLDER, "SAGA folder", SagaUtils.sagaPath()))
22-
SextanteConfig.addSetting(Setting("SAGA", SagaUtils.SAGA_AUTO_RESAMPLING, "Use min covering grid system for resampling", True))
23-
SextanteConfig.addSetting(Setting("SAGA", SagaUtils.SAGA_RESAMPLING_REGION_XMIN, "Resampling region min x", 0))
24-
SextanteConfig.addSetting(Setting("SAGA", SagaUtils.SAGA_RESAMPLING_REGION_YMIN, "Resampling region min y", 0))
25-
SextanteConfig.addSetting(Setting("SAGA", SagaUtils.SAGA_RESAMPLING_REGION_XMAX, "Resampling region max x", 1000))
26-
SextanteConfig.addSetting(Setting("SAGA", SagaUtils.SAGA_RESAMPLING_REGION_YMAX, "Resampling region max y", 1000))
27-
SextanteConfig.addSetting(Setting("SAGA", SagaUtils.SAGA_RESAMPLING_REGION_CELLSIZE, "Resampling region cellsize", 1))
21+
SextanteConfig.addSetting(Setting(self.getDescription(), SagaUtils.SAGA_FOLDER, "SAGA folder", SagaUtils.sagaPath()))
22+
SextanteConfig.addSetting(Setting(self.getDescription(), SagaUtils.SAGA_AUTO_RESAMPLING, "Use min covering grid system for resampling", True))
23+
SextanteConfig.addSetting(Setting(self.getDescription(), SagaUtils.SAGA_RESAMPLING_REGION_XMIN, "Resampling region min x", 0))
24+
SextanteConfig.addSetting(Setting(self.getDescription(), SagaUtils.SAGA_RESAMPLING_REGION_YMIN, "Resampling region min y", 0))
25+
SextanteConfig.addSetting(Setting(self.getDescription(), SagaUtils.SAGA_RESAMPLING_REGION_XMAX, "Resampling region max x", 1000))
26+
SextanteConfig.addSetting(Setting(self.getDescription(), SagaUtils.SAGA_RESAMPLING_REGION_YMAX, "Resampling region max y", 1000))
27+
SextanteConfig.addSetting(Setting(self.getDescription(), SagaUtils.SAGA_RESAMPLING_REGION_CELLSIZE, "Resampling region cellsize", 1))
2828

2929
def unload(self):
3030
AlgorithmProvider.unload(self)

src/sextante/script/ScriptAlgorithmProvider.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ def __init__(self):
2121

2222
def initializeSettings(self):
2323
AlgorithmProvider.initializeSettings(self)
24-
SextanteConfig.addSetting(Setting("Scripts", ScriptUtils.SCRIPTS_FOLDER, "Scripts folder", ScriptUtils.scriptsFolder()))
24+
SextanteConfig.addSetting(Setting(self.getDescription(), ScriptUtils.SCRIPTS_FOLDER, "Scripts folder", ScriptUtils.scriptsFolder()))
2525

2626
def unload(self):
2727
AlgorithmProvider.unload(self)

0 commit comments

Comments
 (0)