Skip to content

Commit c9b2d62

Browse files
author
volayaf
committed
Fixed bugs #5262, #5263, #5265 , #5266, #5267
Added numeric value selector improved grass provider git-svn-id: http://sextante.googlecode.com/svn/trunk/soft/bindings/qgis-plugin@57 881b9c09-3ef8-f3c2-ec3d-21d735c97f4d
1 parent 0bd4ab3 commit c9b2d62

31 files changed

+408
-179
lines changed

src/sextante/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ def name():
55
def description():
66
return "SEXTANTE Geoprocessing platform for QGIS"
77
def version():
8-
return "Version 0.1"
8+
return "Version 1.0"
99
def icon():
1010
return "icon.png"
1111
def qgisMinimumVersion():

src/sextante/core/AlgorithmProvider.py

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,25 @@ def __init__(self):
1212
self.actions = []
1313
self.contextMenuActions = []
1414

15+
def loadAlgorithms(self):
16+
self.algs = []
17+
name = "ACTIVATE_" + self.getName().upper().replace(" ", "_")
18+
if not SextanteConfig.getSetting(name):
19+
return
20+
else:
21+
self._loadAlgorithms()
22+
23+
#methods to be overridden.
24+
#==============================
25+
26+
27+
def _loadAlgorithms(self):
28+
'''Algorithm loading should take place here, filling self.algs, which is a list of
29+
elements of class GeoAlgorithm. Use that class to create your own algorithms
30+
Since algorithms should have a reference to the provider they come
31+
from, this is also the place to set the 'provider' variable of each algorithm'''
32+
pass
33+
1534
def initializeSettings(self):
1635
'''this is the place where you should add config parameters to sextante using the SextanteConfig class.
1736
this method is called when a provider is added to Sextante.
@@ -25,26 +44,12 @@ def unload(self):
2544
Removal of config setting should be done here'''
2645
name = "ACTIVATE_" + self.getName().upper().replace(" ", "_")
2746
SextanteConfig.removeSetting(name)
47+
2848
def getName(self):
2949
return "Generic algorithm provider"
3050

31-
def loadAlgorithms(self):
32-
self.algs = []
33-
name = "ACTIVATE_" + self.getName().upper().replace(" ", "_")
34-
if not SextanteConfig.getSetting(name):
35-
return
36-
else:
37-
self._loadAlgorithms()
3851

39-
#methods to be overridden.
40-
#==============================
4152

42-
#Algorithm loading should take place here, filling self.algs, which is a list of
43-
#elements of class GeoAlgorithm. Use that class to create your own algorithms
44-
#Since algorithms should have a reference to the provider they come
45-
#from, this is also the place to set the 'provider' variable of each algorithm
46-
def _loadAlgorithms(self):
47-
pass
4853

4954
def getIcon(self):
5055
return QtGui.QIcon(os.path.dirname(__file__) + "/../images/alg.png")

src/sextante/core/GeoAlgorithm.py

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
import os.path
88
from sextante.core.SextanteUtils import SextanteUtils
99
from sextante.parameters.ParameterMultipleInput import ParameterMultipleInput
10-
10+
from sextante.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException
11+
import traceback
12+
from sextante.core.SextanteLog import SextanteLog
1113

1214
class GeoAlgorithm:
1315

@@ -23,6 +25,10 @@ def __init__(self):
2325
#change any of the following if your algorithm should not appear in the toolbox or modeler
2426
self.showInToolbox = True
2527
self.showInModeler = True
28+
#true if the algorithm has been canceled while it was being executed
29+
#default value is false, so it should be changed in processAlgorithm only if the algorithm
30+
#gets canceled
31+
self.canceled = False
2632

2733
self.defineCharacteristics()
2834

@@ -32,6 +38,8 @@ def getIcon(self):
3238
return QtGui.QIcon(os.path.dirname(__file__) + "/../images/alg.png")
3339

3440
def helpFile(self):
41+
'''Returns the path to the help file with the description of this algorithm.
42+
It should be an HTML file'''
3543
return None
3644

3745
def processAlgorithm(self):
@@ -43,7 +51,8 @@ def defineCharacteristics(self):
4351
pass
4452

4553
def getCustomParametersDialog(self):
46-
'''if the algorithm has a custom parameters dialog, it should be returned here, ready to be executed'''
54+
'''if the algorithm has a custom parameters dialog, it should be returned
55+
here, ready to be executed'''
4756
return None
4857

4958
def getCustomModelerParametersDialog(self, modelAlg):
@@ -54,9 +63,29 @@ def getCustomModelerParametersDialog(self, modelAlg):
5463
#=========================================================
5564

5665
def execute(self, progress):
66+
'''The method to use to call a SEXTANTE algorithm.
67+
Although the body of the algorithm is in processAlgorithm(),
68+
it should be called using this method, since it performs
69+
some additional operations.
70+
The return value indicates whether the algorithm was canceled (false)
71+
or successfully run (true).
72+
Raises a GeoAlgorithmExecutionException in case anything goes wrong.'''
5773
self.setOutputCRSFromInputLayers()
5874
self.resolveTemporaryOutputs()
59-
self.processAlgorithm(progress)
75+
try:
76+
self.processAlgorithm(progress)
77+
return not self.canceled
78+
except GeoAlgorithmExecutionException, gaee:
79+
SextanteLog.addToLog(SextanteLog.LOG_ERROR, gaee.msg)
80+
raise gaee
81+
except Exception, e:
82+
#if something goes wrong and is not caught in the algorithm,
83+
#we catch it here and wrap it
84+
lines = []
85+
lines.append(str(e))
86+
lines.append(traceback.format_exc().replace("\n", "|"))
87+
SextanteLog.addToLog(SextanteLog.LOG_ERROR, lines)
88+
raise GeoAlgorithmExecutionException(str(e))
6089

6190
def resolveTemporaryOutputs(self):
6291
'''sets temporary outputs (output.value = None) with a temporary file instead'''
@@ -152,7 +181,6 @@ def getOutputValue(self, name):
152181
return out.value
153182
return None
154183

155-
156184
def getAsCommand(self):
157185
'''Returns the command that would run this same algorithm from the console'''
158186
s="Sextante.runalg(\"" + self.commandLineName() + "\","

src/sextante/core/QGisLayers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from qgis.core import *
22
from PyQt4.QtCore import *
33
from PyQt4.QtGui import *
4-
from PyQt4 import QtCore, QtGui
4+
from PyQt4 import QtGui
55
from os import path
66
from sextante.core.SextanteConfig import SextanteConfig
77
import os.path

src/sextante/core/Sextante.py

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ def removeProvider(provider):
5454
Sextante.updateAlgsList()
5555
except:
5656
pass #This try catch block is here to avoid problems if the plugin with a provider is unloaded
57-
#after SEXTANTEe itself has been unloaded. It is a quick fix before I found out how to
57+
#after SEXTANTE itself has been unloaded. It is a quick fix before I found out how to
5858
#properly avoid that
5959

6060
@staticmethod
@@ -82,6 +82,7 @@ def initialize():
8282
Sextante.addProvider(ScriptAlgorithmProvider())
8383
Sextante.addProvider(RAlgorithmProvider())
8484
Sextante.addProvider(SagaAlgorithmProvider())
85+
Sextante.addProvider(GrassAlgorithmProvider())
8586
Sextante.modeler.initializeSettings();
8687
#and initialize
8788
SextanteLog.startLogging()
@@ -260,14 +261,11 @@ def runalg(name, *args):
260261

261262
SextanteLog.addToLog(SextanteLog.LOG_ALGORITHM, alg.getAsCommand())
262263

263-
try:
264-
QApplication.setOverrideCursor(QCursor(Qt.WaitCursor))
265-
AlgorithmExecutor.runalg(alg, SilentProgress())
266-
QApplication.restoreOverrideCursor()
267-
return alg.getOutputValuesAsDictionary()
268-
except GeoAlgorithmExecutionException, e:
269-
print "*****Error executing algorithm*****"
270-
print e.msg
264+
QApplication.setOverrideCursor(QCursor(Qt.WaitCursor))
265+
AlgorithmExecutor.runalg(alg, SilentProgress())
266+
QApplication.restoreOverrideCursor()
267+
return alg.getOutputValuesAsDictionary()
268+
271269

272270
@staticmethod
273271
def load(layer):
@@ -283,7 +281,7 @@ def loadFromAlg(layersdict):
283281
@staticmethod
284282
def getObject(uri):
285283
'''Returns the QGIS object identified the given URI'''
286-
QGisLayers.getObjectFromUri(uri)
284+
return QGisLayers.getObjectFromUri(uri)
287285

288286
@staticmethod
289287
def runandload(name, *args):
@@ -313,13 +311,12 @@ def runandload(name, *args):
313311
return
314312
i = i +1
315313

316-
try:
317-
QApplication.setOverrideCursor(QCursor(Qt.WaitCursor))
318-
AlgorithmExecutor.runalg(alg, SilentProgress())
319-
QApplication.restoreOverrideCursor()
314+
QApplication.setOverrideCursor(QCursor(Qt.WaitCursor))
315+
ret = AlgorithmExecutor.runalg(alg, SilentProgress())
316+
QApplication.restoreOverrideCursor()
317+
if ret:
320318
SextantePostprocessing.handleAlgorithmResults(alg)
321-
except GeoAlgorithmExecutionException, e:
322-
QMessageBox.critical(None, "Error", e.msg)
319+
323320

324321

325322

src/sextante/core/SextanteConfig.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ def getSetting(name):
8080

8181

8282
class Setting():
83-
83+
'''A simple config parameter that will appear on the SEXTANTE config dialog'''
8484
def __init__(self, group, name, description, default):
8585
self.group=group
8686
self.name = name

src/sextante/ftools/Delaunay.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
from qgis.core import *
77
from sextante.parameters.ParameterVector import ParameterVector
88
from sextante.core.QGisLayers import QGisLayers
9-
from sextante.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException
109
from sextante.outputs.OutputVector import OutputVector
1110
import voronoi
1211
from sets import Set

src/sextante/ftools/LinesIntersection.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ def processAlgorithm(self, progress):
4646
field2.setName(unicode(field2.name()) + "_2")
4747
fieldList = {0:field1, 1:field2}
4848
sRs = provider1.crs()
49-
check = QFile(self.shapefileName)
49+
check = QFile(output)
5050
if check.exists():
5151
if not QgsVectorFileWriter.deleteShapeFile(output):
5252
raise GeoAlgorithmExecutionException("Could not delete existing output file")

src/sextante/ftools/PointsInPolygon.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ def processAlgorithm(self, progress):
3232
polyProvider = polyLayer.dataProvider()
3333
pointProvider = pointLayer.dataProvider()
3434
if polyProvider.crs() <> pointProvider.crs():
35-
SextanteLog.addToLog(SextanteLog.LOG_WARNING,
35+
SextanteLog.addToLog(SextanteLog.LOG_WARNING,
3636
"CRS warning!Warning: Input layers have non-matching CRS.\nThis may cause unexpected results.")
3737
allAttrs = polyProvider.attributeIndexes()
3838
polyProvider.select(allAttrs)
@@ -45,10 +45,10 @@ def processAlgorithm(self, progress):
4545
field = QgsField(unicode(inField), QVariant.Double, "real", 24, 15, "point count field")
4646
fieldList[index] = field
4747
sRs = polyProvider.crs()
48-
check = QFile(self.shapefileName)
48+
check = QFile(output)
4949
if check.exists():
50-
if not QgsVectorFileWriter.deleteShapeFile(self.shapefileName):
51-
return
50+
if not QgsVectorFileWriter.deleteShapeFile(output):
51+
raise GeoAlgorithmExecutionException("could not delete file: " + output)
5252
writer = QgsVectorFileWriter(output, systemEncoding, fieldList, polyProvider.geometryType(), sRs)
5353
inFeat = QgsFeature()
5454
inFeatB = QgsFeature()

src/sextante/ftools/SumLines.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from sextante.core.SextanteLog import SextanteLog
1111
from sextante.ftools import ftools_utils
1212
from sextante.parameters.ParameterString import ParameterString
13+
from sextante.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException
1314

1415
class SumLines(GeoAlgorithm):
1516

@@ -53,10 +54,10 @@ def processAlgorithm(self, progress):
5354
lineProvider.rewind()
5455
start = 15.00
5556
add = 85.00 / polyProvider.featureCount()
56-
check = QFile(self.shapefileName)
57+
check = QFile(output)
5758
if check.exists():
58-
if not QgsVectorFileWriter.deleteShapeFile(self.shapefileName):
59-
return
59+
if not QgsVectorFileWriter.deleteShapeFile(output):
60+
raise GeoAlgorithmExecutionException("Could not delete existing output file")
6061
writer = QgsVectorFileWriter(output, systemEncoding, fieldList, polyProvider.geometryType(), sRs)
6162
#writer = QgsVectorFileWriter(outPath, "UTF-8", fieldList, polyProvider.geometryType(), sRs)
6263
spatialIndex = ftools_utils.createIndex( lineProvider )

src/sextante/grass/GrassAlgorithm.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
from sextante.parameters.ParameterVector import ParameterVector
1111
from sextante.parameters.ParameterBoolean import ParameterBoolean
1212
from sextante.outputs.OutputVector import OutputVector
13-
from sextante.saga.SagaUtils import SagaUtils
1413
from sextante.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException
1514
from sextante.core.SextanteLog import SextanteLog
1615
from sextante.parameters.ParameterFactory import ParameterFactory
@@ -200,13 +199,12 @@ def processAlgorithm(self, progress):
200199
commands.append(command)
201200

202201
#4 Run GRASS
203-
GrassUtils.createGrassScript(commands)
204202
loglines = []
205203
loglines.append("GRASS execution commands")
206204
for line in commands:
207205
loglines.append(line)
208206
SextanteLog.addToLog(SextanteLog.LOG_INFO, loglines)
209-
GrassUtils.executeGrass(progress);
207+
GrassUtils.executeGrass(commands, progress);
210208

211209

212210
def exportVectorLayer(self, filename):

src/sextante/grass/GrassAlgorithmProvider.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from sextante.core.SextanteLog import SextanteLog
77
from sextante.grass.GrassUtils import GrassUtils
88
from sextante.grass.GrassAlgorithm import GrassAlgorithm
9+
from sextante.core.SextanteUtils import SextanteUtils
910

1011
class GrassAlgorithmProvider(AlgorithmProvider):
1112

@@ -15,29 +16,32 @@ def __init__(self):
1516

1617
def initializeSettings(self):
1718
AlgorithmProvider.initializeSettings(self)
18-
SextanteConfig.addSetting(Setting("GRASS", GrassUtils.GRASS_FOLDER, "GRASS folder", GrassUtils.grassPath()))
19+
if SextanteUtils.isWindows():
20+
SextanteConfig.addSetting(Setting("GRASS", GrassUtils.GRASS_FOLDER, "GRASS folder", GrassUtils.grassPath()))
21+
SextanteConfig.addSetting(Setting("GRASS", GrassUtils.GRASS_WIN_SHELL, "Shell executable", GrassUtils.grassWinShell()))
1922
SextanteConfig.addSetting(Setting("GRASS", GrassUtils.GRASS_AUTO_REGION, "Use min covering region", True))
2023
SextanteConfig.addSetting(Setting("GRASS", GrassUtils.GRASS_LATLON, "Coordinates are lat/lon", False))
2124
SextanteConfig.addSetting(Setting("GRASS", GrassUtils.GRASS_REGION_XMIN, "GRASS Region min x", 0))
2225
SextanteConfig.addSetting(Setting("GRASS", GrassUtils.GRASS_REGION_YMIN, "GRASS Region min y", 0))
2326
SextanteConfig.addSetting(Setting("GRASS", GrassUtils.GRASS_REGION_XMAX, "GRASS Region max x", 1000))
2427
SextanteConfig.addSetting(Setting("GRASS", GrassUtils.GRASS_REGION_YMAX, "GRASS Region max y", 1000))
2528
SextanteConfig.addSetting(Setting("GRASS", GrassUtils.GRASS_REGION_CELLSIZE, "GRASS Region cellsize", 1))
26-
SextanteConfig.addSetting(Setting("GRASS", GrassUtils.GRASS_WIN_SHELL, "Shell executable (Windows only)", GrassUtils.grassWinShell()))
29+
2730
SextanteConfig.addSetting(Setting("GRASS", GrassUtils.GRASS_HELP_FOLDER, "GRASS help folder", GrassUtils.grassHelpPath()))
2831
#SextanteConfig.addSetting(Setting("SAGA", SagaUtils.SAGA_USE_SELECTED, "Use only selected features in vector layers", False))
2932

3033
def unload(self):
3134
AlgorithmProvider.unload(self)
32-
SextanteConfig.removeSetting(GrassUtils.GRASS_FOLDER)
35+
if SextanteUtils.isWindows():
36+
SextanteConfig.removeSetting(GrassUtils.GRASS_FOLDER)
37+
SextanteConfig.removeSetting(GrassUtils.GRASS_WIN_SHELL)
3338
SextanteConfig.removeSetting(GrassUtils.GRASS_AUTO_REGION)
3439
SextanteConfig.removeSetting(GrassUtils.GRASS_LATLON)
3540
SextanteConfig.removeSetting(GrassUtils.GRASS_REGION_XMIN)
3641
SextanteConfig.removeSetting(GrassUtils.GRASS_REGION_YMIN)
3742
SextanteConfig.removeSetting(GrassUtils.GRASS_REGION_XMAX)
3843
SextanteConfig.removeSetting(GrassUtils.GRASS_REGION_YMAX)
3944
SextanteConfig.removeSetting(GrassUtils.GRASS_REGION_CELLSIZE)
40-
SextanteConfig.removeSetting(GrassUtils.GRASS_WIN_SHELL)
4145
SextanteConfig.removeSetting(GrassUtils.GRASS_HELP_FOLDER)
4246

4347
def createAlgsList(self):
@@ -53,7 +57,7 @@ def createAlgsList(self):
5357
else:
5458
SextanteLog.addToLog(SextanteLog.LOG_ERROR, "Could not open GRASS algorithm: " + descriptionFile)
5559
except Exception,e:
56-
pass
60+
SextanteLog.addToLog(SextanteLog.LOG_ERROR, "Could not open GRASS algorithm: " + descriptionFile)
5761

5862
def _loadAlgorithms(self):
5963
self.algs = self.preloadedAlgs

0 commit comments

Comments
 (0)