Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
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
  • Loading branch information
volayaf committed Apr 1, 2012
1 parent 0bd4ab3 commit c9b2d62
Show file tree
Hide file tree
Showing 31 changed files with 408 additions and 179 deletions.
2 changes: 1 addition & 1 deletion src/sextante/__init__.py
Expand Up @@ -5,7 +5,7 @@ def name():
def description():
return "SEXTANTE Geoprocessing platform for QGIS"
def version():
return "Version 0.1"
return "Version 1.0"
def icon():
return "icon.png"
def qgisMinimumVersion():
Expand Down
35 changes: 20 additions & 15 deletions src/sextante/core/AlgorithmProvider.py
Expand Up @@ -12,6 +12,25 @@ def __init__(self):
self.actions = []
self.contextMenuActions = []

def loadAlgorithms(self):
self.algs = []
name = "ACTIVATE_" + self.getName().upper().replace(" ", "_")
if not SextanteConfig.getSetting(name):
return
else:
self._loadAlgorithms()

#methods to be overridden.
#==============================


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

def initializeSettings(self):
'''this is the place where you should add config parameters to sextante using the SextanteConfig class.
this method is called when a provider is added to Sextante.
Expand All @@ -25,26 +44,12 @@ def unload(self):
Removal of config setting should be done here'''
name = "ACTIVATE_" + self.getName().upper().replace(" ", "_")
SextanteConfig.removeSetting(name)

def getName(self):
return "Generic algorithm provider"

def loadAlgorithms(self):
self.algs = []
name = "ACTIVATE_" + self.getName().upper().replace(" ", "_")
if not SextanteConfig.getSetting(name):
return
else:
self._loadAlgorithms()

#methods to be overridden.
#==============================

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

def getIcon(self):
return QtGui.QIcon(os.path.dirname(__file__) + "/../images/alg.png")
Expand Down
36 changes: 32 additions & 4 deletions src/sextante/core/GeoAlgorithm.py
Expand Up @@ -7,7 +7,9 @@
import os.path
from sextante.core.SextanteUtils import SextanteUtils
from sextante.parameters.ParameterMultipleInput import ParameterMultipleInput

from sextante.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException
import traceback
from sextante.core.SextanteLog import SextanteLog

class GeoAlgorithm:

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

self.defineCharacteristics()

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

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

def processAlgorithm(self):
Expand All @@ -43,7 +51,8 @@ def defineCharacteristics(self):
pass

def getCustomParametersDialog(self):
'''if the algorithm has a custom parameters dialog, it should be returned here, ready to be executed'''
'''if the algorithm has a custom parameters dialog, it should be returned
here, ready to be executed'''
return None

def getCustomModelerParametersDialog(self, modelAlg):
Expand All @@ -54,9 +63,29 @@ def getCustomModelerParametersDialog(self, modelAlg):
#=========================================================

def execute(self, progress):
'''The method to use to call a SEXTANTE algorithm.
Although the body of the algorithm is in processAlgorithm(),
it should be called using this method, since it performs
some additional operations.
The return value indicates whether the algorithm was canceled (false)
or successfully run (true).
Raises a GeoAlgorithmExecutionException in case anything goes wrong.'''
self.setOutputCRSFromInputLayers()
self.resolveTemporaryOutputs()
self.processAlgorithm(progress)
try:
self.processAlgorithm(progress)
return not self.canceled
except GeoAlgorithmExecutionException, gaee:
SextanteLog.addToLog(SextanteLog.LOG_ERROR, gaee.msg)
raise gaee
except Exception, e:
#if something goes wrong and is not caught in the algorithm,
#we catch it here and wrap it
lines = []
lines.append(str(e))
lines.append(traceback.format_exc().replace("\n", "|"))
SextanteLog.addToLog(SextanteLog.LOG_ERROR, lines)
raise GeoAlgorithmExecutionException(str(e))

def resolveTemporaryOutputs(self):
'''sets temporary outputs (output.value = None) with a temporary file instead'''
Expand Down Expand Up @@ -152,7 +181,6 @@ def getOutputValue(self, name):
return out.value
return None


def getAsCommand(self):
'''Returns the command that would run this same algorithm from the console'''
s="Sextante.runalg(\"" + self.commandLineName() + "\","
Expand Down
2 changes: 1 addition & 1 deletion src/sextante/core/QGisLayers.py
@@ -1,7 +1,7 @@
from qgis.core import *
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4 import QtCore, QtGui
from PyQt4 import QtGui
from os import path
from sextante.core.SextanteConfig import SextanteConfig
import os.path
Expand Down
29 changes: 13 additions & 16 deletions src/sextante/core/Sextante.py
Expand Up @@ -54,7 +54,7 @@ def removeProvider(provider):
Sextante.updateAlgsList()
except:
pass #This try catch block is here to avoid problems if the plugin with a provider is unloaded
#after SEXTANTEe itself has been unloaded. It is a quick fix before I found out how to
#after SEXTANTE itself has been unloaded. It is a quick fix before I found out how to
#properly avoid that

@staticmethod
Expand Down Expand Up @@ -82,6 +82,7 @@ def initialize():
Sextante.addProvider(ScriptAlgorithmProvider())
Sextante.addProvider(RAlgorithmProvider())
Sextante.addProvider(SagaAlgorithmProvider())
Sextante.addProvider(GrassAlgorithmProvider())
Sextante.modeler.initializeSettings();
#and initialize
SextanteLog.startLogging()
Expand Down Expand Up @@ -260,14 +261,11 @@ def runalg(name, *args):

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

try:
QApplication.setOverrideCursor(QCursor(Qt.WaitCursor))
AlgorithmExecutor.runalg(alg, SilentProgress())
QApplication.restoreOverrideCursor()
return alg.getOutputValuesAsDictionary()
except GeoAlgorithmExecutionException, e:
print "*****Error executing algorithm*****"
print e.msg
QApplication.setOverrideCursor(QCursor(Qt.WaitCursor))
AlgorithmExecutor.runalg(alg, SilentProgress())
QApplication.restoreOverrideCursor()
return alg.getOutputValuesAsDictionary()


@staticmethod
def load(layer):
Expand All @@ -283,7 +281,7 @@ def loadFromAlg(layersdict):
@staticmethod
def getObject(uri):
'''Returns the QGIS object identified the given URI'''
QGisLayers.getObjectFromUri(uri)
return QGisLayers.getObjectFromUri(uri)

@staticmethod
def runandload(name, *args):
Expand Down Expand Up @@ -313,13 +311,12 @@ def runandload(name, *args):
return
i = i +1

try:
QApplication.setOverrideCursor(QCursor(Qt.WaitCursor))
AlgorithmExecutor.runalg(alg, SilentProgress())
QApplication.restoreOverrideCursor()
QApplication.setOverrideCursor(QCursor(Qt.WaitCursor))
ret = AlgorithmExecutor.runalg(alg, SilentProgress())
QApplication.restoreOverrideCursor()
if ret:
SextantePostprocessing.handleAlgorithmResults(alg)
except GeoAlgorithmExecutionException, e:
QMessageBox.critical(None, "Error", e.msg)




Expand Down
2 changes: 1 addition & 1 deletion src/sextante/core/SextanteConfig.py
Expand Up @@ -80,7 +80,7 @@ def getSetting(name):


class Setting():

'''A simple config parameter that will appear on the SEXTANTE config dialog'''
def __init__(self, group, name, description, default):
self.group=group
self.name = name
Expand Down
1 change: 0 additions & 1 deletion src/sextante/ftools/Delaunay.py
Expand Up @@ -6,7 +6,6 @@
from qgis.core import *
from sextante.parameters.ParameterVector import ParameterVector
from sextante.core.QGisLayers import QGisLayers
from sextante.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException
from sextante.outputs.OutputVector import OutputVector
import voronoi
from sets import Set
Expand Down
2 changes: 1 addition & 1 deletion src/sextante/ftools/LinesIntersection.py
Expand Up @@ -46,7 +46,7 @@ def processAlgorithm(self, progress):
field2.setName(unicode(field2.name()) + "_2")
fieldList = {0:field1, 1:field2}
sRs = provider1.crs()
check = QFile(self.shapefileName)
check = QFile(output)
if check.exists():
if not QgsVectorFileWriter.deleteShapeFile(output):
raise GeoAlgorithmExecutionException("Could not delete existing output file")
Expand Down
8 changes: 4 additions & 4 deletions src/sextante/ftools/PointsInPolygon.py
Expand Up @@ -32,7 +32,7 @@ def processAlgorithm(self, progress):
polyProvider = polyLayer.dataProvider()
pointProvider = pointLayer.dataProvider()
if polyProvider.crs() <> pointProvider.crs():
SextanteLog.addToLog(SextanteLog.LOG_WARNING,
SextanteLog.addToLog(SextanteLog.LOG_WARNING,
"CRS warning!Warning: Input layers have non-matching CRS.\nThis may cause unexpected results.")
allAttrs = polyProvider.attributeIndexes()
polyProvider.select(allAttrs)
Expand All @@ -45,10 +45,10 @@ def processAlgorithm(self, progress):
field = QgsField(unicode(inField), QVariant.Double, "real", 24, 15, "point count field")
fieldList[index] = field
sRs = polyProvider.crs()
check = QFile(self.shapefileName)
check = QFile(output)
if check.exists():
if not QgsVectorFileWriter.deleteShapeFile(self.shapefileName):
return
if not QgsVectorFileWriter.deleteShapeFile(output):
raise GeoAlgorithmExecutionException("could not delete file: " + output)
writer = QgsVectorFileWriter(output, systemEncoding, fieldList, polyProvider.geometryType(), sRs)
inFeat = QgsFeature()
inFeatB = QgsFeature()
Expand Down
7 changes: 4 additions & 3 deletions src/sextante/ftools/SumLines.py
Expand Up @@ -10,6 +10,7 @@
from sextante.core.SextanteLog import SextanteLog
from sextante.ftools import ftools_utils
from sextante.parameters.ParameterString import ParameterString
from sextante.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException

class SumLines(GeoAlgorithm):

Expand Down Expand Up @@ -53,10 +54,10 @@ def processAlgorithm(self, progress):
lineProvider.rewind()
start = 15.00
add = 85.00 / polyProvider.featureCount()
check = QFile(self.shapefileName)
check = QFile(output)
if check.exists():
if not QgsVectorFileWriter.deleteShapeFile(self.shapefileName):
return
if not QgsVectorFileWriter.deleteShapeFile(output):
raise GeoAlgorithmExecutionException("Could not delete existing output file")
writer = QgsVectorFileWriter(output, systemEncoding, fieldList, polyProvider.geometryType(), sRs)
#writer = QgsVectorFileWriter(outPath, "UTF-8", fieldList, polyProvider.geometryType(), sRs)
spatialIndex = ftools_utils.createIndex( lineProvider )
Expand Down
4 changes: 1 addition & 3 deletions src/sextante/grass/GrassAlgorithm.py
Expand Up @@ -10,7 +10,6 @@
from sextante.parameters.ParameterVector import ParameterVector
from sextante.parameters.ParameterBoolean import ParameterBoolean
from sextante.outputs.OutputVector import OutputVector
from sextante.saga.SagaUtils import SagaUtils
from sextante.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException
from sextante.core.SextanteLog import SextanteLog
from sextante.parameters.ParameterFactory import ParameterFactory
Expand Down Expand Up @@ -200,13 +199,12 @@ def processAlgorithm(self, progress):
commands.append(command)

#4 Run GRASS
GrassUtils.createGrassScript(commands)
loglines = []
loglines.append("GRASS execution commands")
for line in commands:
loglines.append(line)
SextanteLog.addToLog(SextanteLog.LOG_INFO, loglines)
GrassUtils.executeGrass(progress);
GrassUtils.executeGrass(commands, progress);


def exportVectorLayer(self, filename):
Expand Down
14 changes: 9 additions & 5 deletions src/sextante/grass/GrassAlgorithmProvider.py
Expand Up @@ -6,6 +6,7 @@
from sextante.core.SextanteLog import SextanteLog
from sextante.grass.GrassUtils import GrassUtils
from sextante.grass.GrassAlgorithm import GrassAlgorithm
from sextante.core.SextanteUtils import SextanteUtils

class GrassAlgorithmProvider(AlgorithmProvider):

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

def initializeSettings(self):
AlgorithmProvider.initializeSettings(self)
SextanteConfig.addSetting(Setting("GRASS", GrassUtils.GRASS_FOLDER, "GRASS folder", GrassUtils.grassPath()))
if SextanteUtils.isWindows():
SextanteConfig.addSetting(Setting("GRASS", GrassUtils.GRASS_FOLDER, "GRASS folder", GrassUtils.grassPath()))
SextanteConfig.addSetting(Setting("GRASS", GrassUtils.GRASS_WIN_SHELL, "Shell executable", 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_WIN_SHELL, "Shell executable (Windows only)", GrassUtils.grassWinShell()))

SextanteConfig.addSetting(Setting("GRASS", GrassUtils.GRASS_HELP_FOLDER, "GRASS help folder", GrassUtils.grassHelpPath()))
#SextanteConfig.addSetting(Setting("SAGA", SagaUtils.SAGA_USE_SELECTED, "Use only selected features in vector layers", False))

def unload(self):
AlgorithmProvider.unload(self)
SextanteConfig.removeSetting(GrassUtils.GRASS_FOLDER)
if SextanteUtils.isWindows():
SextanteConfig.removeSetting(GrassUtils.GRASS_FOLDER)
SextanteConfig.removeSetting(GrassUtils.GRASS_WIN_SHELL)
SextanteConfig.removeSetting(GrassUtils.GRASS_AUTO_REGION)
SextanteConfig.removeSetting(GrassUtils.GRASS_LATLON)
SextanteConfig.removeSetting(GrassUtils.GRASS_REGION_XMIN)
SextanteConfig.removeSetting(GrassUtils.GRASS_REGION_YMIN)
SextanteConfig.removeSetting(GrassUtils.GRASS_REGION_XMAX)
SextanteConfig.removeSetting(GrassUtils.GRASS_REGION_YMAX)
SextanteConfig.removeSetting(GrassUtils.GRASS_REGION_CELLSIZE)
SextanteConfig.removeSetting(GrassUtils.GRASS_WIN_SHELL)
SextanteConfig.removeSetting(GrassUtils.GRASS_HELP_FOLDER)

def createAlgsList(self):
Expand All @@ -53,7 +57,7 @@ def createAlgsList(self):
else:
SextanteLog.addToLog(SextanteLog.LOG_ERROR, "Could not open GRASS algorithm: " + descriptionFile)
except Exception,e:
pass
SextanteLog.addToLog(SextanteLog.LOG_ERROR, "Could not open GRASS algorithm: " + descriptionFile)

def _loadAlgorithms(self):
self.algs = self.preloadedAlgs
Expand Down

0 comments on commit c9b2d62

Please sign in to comment.