Skip to content

Commit

Permalink
Merge linfiniti2:/tmp/sextante into sextante-import
Browse files Browse the repository at this point in the history
  • Loading branch information
timlinux committed Aug 24, 2012
2 parents 17f91e7 + 9f47d96 commit 7ceb460
Show file tree
Hide file tree
Showing 59 changed files with 674 additions and 247 deletions.
4 changes: 3 additions & 1 deletion python/plugins/sextante/about/about.htm
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ <h2>SEXTANTE for QGIS</h2>
<p>A development by Victor Olaya (volayaf@gmail.com).</p>
<p>Portions of this software contributed by:
<ul>
<li>Michael Nimm (mmqgis algorithms)</li>
<li>Alexander Bruy</li>
<li>Carson Farmer (fTools algorithms)</li>
<li>Julien Malik (Orfeo Toolbox connectors)</li>
<li>Evgeniy Nikulin (Original Field Pyculator code)</li>
<li>Michael Nimm (mmqgis algorithms)</li>
<li>Camilo Polymeris (Threading). Developed as part of Google Summer of Code 2012</li>
</ul>
</p>
Expand Down
6 changes: 2 additions & 4 deletions python/plugins/sextante/algs/AddTableField.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,14 @@ def defineCharacteristics(self):
def processAlgorithm(self, progress):
fieldtype = self.getParameterValue(self.FIELD_TYPE)
fieldname = self.getParameterValue(self.FIELD_NAME)
settings = QSettings()
systemEncoding = settings.value( "/UI/encoding", "System" ).toString()
output = self.getOutputValue(self.OUTPUT_LAYER)
output = self.getOutputFromName(self.OUTPUT_LAYER)
vlayer = QGisLayers.getObjectFromUri(self.getParameterValue(self.INPUT_LAYER))
vprovider = vlayer.dataProvider()
allAttrs = vprovider.attributeIndexes()
vprovider.select( allAttrs )
fields = vprovider.fields()
fields[len(fields)] = QgsField(fieldname, self.TYPES[fieldtype])
writer = QgsVectorFileWriter( output, systemEncoding,fields, vprovider.geometryType(), vprovider.crs() )
writer = output.getVectorWriter(fields, vprovider.geometryType(), vprovider.crs() )
inFeat = QgsFeature()
outFeat = QgsFeature()
inGeom = QgsGeometry()
Expand Down
6 changes: 2 additions & 4 deletions python/plugins/sextante/algs/AutoincrementalField.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,14 @@ def getIcon(self):
return QtGui.QIcon(os.path.dirname(__file__) + "/../images/toolbox.png")

def processAlgorithm(self, progress):
settings = QSettings()
systemEncoding = settings.value( "/UI/encoding", "System" ).toString()
output = self.getOutputValue(self.OUTPUT)
output = self.getOutputFromName(self.OUTPUT)
vlayer = QGisLayers.getObjectFromUri(self.getParameterValue(self.INPUT))
vprovider = vlayer.dataProvider()
allAttrs = vprovider.attributeIndexes()
vprovider.select( allAttrs )
fields = vprovider.fields()
fields[len(fields)] = QgsField("AUTO", QVariant.Int)
writer = QgsVectorFileWriter( output, systemEncoding,fields, vprovider.geometryType(), vprovider.crs() )
writer = output.getVectorWriter(fields, vprovider.geometryType(), vprovider.crs() )
inFeat = QgsFeature()
outFeat = QgsFeature()
inGeom = QgsGeometry()
Expand Down
6 changes: 2 additions & 4 deletions python/plugins/sextante/algs/EquivalentNumField.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,14 @@ def getIcon(self):

def processAlgorithm(self, progress):
field_index = self.getParameterValue(self.FIELD)
settings = QSettings()
systemEncoding = settings.value( "/UI/encoding", "System" ).toString()
output = self.getOutputValue(self.OUTPUT)
output = self.getOutputFromName(self.OUTPUT)
vlayer = QGisLayers.getObjectFromUri(self.getParameterValue(self.INPUT))
vprovider = vlayer.dataProvider()
allAttrs = vprovider.attributeIndexes()
vprovider.select( allAttrs )
fields = vprovider.fields()
fields[len(fields)] = QgsField("NUM_FIELD", QVariant.Int)
writer = QgsVectorFileWriter( output, systemEncoding,fields, vprovider.geometryType(), vprovider.crs() )
writer = output.getVectorWriter(fields, vprovider.geometryType(), vprovider.crs() )
inFeat = QgsFeature()
outFeat = QgsFeature()
inGeom = QgsGeometry()
Expand Down
7 changes: 2 additions & 5 deletions python/plugins/sextante/algs/Explode.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,13 @@ def getIcon(self):
return QtGui.QIcon(os.path.dirname(__file__) + "/../images/toolbox.png")

def processAlgorithm(self, progress):
settings = QSettings()
systemEncoding = settings.value( "/UI/encoding", "System" ).toString()
vlayer = QGisLayers.getObjectFromUri(self.getParameterValue(self.INPUT))
output = self.getOutputValue(self.OUTPUT)
output = self.getOutputFromName(self.OUTPUT)
vprovider = vlayer.dataProvider()
allAttrs = vprovider.attributeIndexes()
vprovider.select( allAttrs )
fields = vprovider.fields()
writer = QgsVectorFileWriter( output, systemEncoding,
fields, QGis.WKBLineString, vprovider.crs() )
writer = output.getVectorWriter(fields, QGis.WKBLineString, vprovider.crs() )
inFeat = QgsFeature()
outFeat = QgsFeature()
inGeom = QgsGeometry()
Expand Down
6 changes: 2 additions & 4 deletions python/plugins/sextante/algs/FieldsCalculator.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,14 @@ def defineCharacteristics(self):
def processAlgorithm(self, progress):
fieldname = self.getParameterValue(self.FIELD_NAME)
formula = self.getParameterValue(self.FORMULA)
settings = QSettings()
systemEncoding = settings.value( "/UI/encoding", "System" ).toString()
output = self.getOutputValue(self.OUTPUT_LAYER)
output = self.getOutputFromName(self.OUTPUT_LAYER)
vlayer = QGisLayers.getObjectFromUri(self.getParameterValue(self.INPUT_LAYER))
vprovider = vlayer.dataProvider()
allAttrs = vprovider.attributeIndexes()
vprovider.select( allAttrs )
fields = vprovider.fields()
fields[len(fields)] = QgsField(fieldname, QVariant.Double)
writer = QgsVectorFileWriter( output, systemEncoding,fields, vprovider.geometryType(), vprovider.crs() )
writer = output.getVectorWriter(fields, vprovider.geometryType(), vprovider.crs() )
inFeat = QgsFeature()
outFeat = QgsFeature()
inGeom = QgsGeometry()
Expand Down
10 changes: 4 additions & 6 deletions python/plugins/sextante/algs/SaveSelectedFeatures.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def processAlgorithm(self, progress):
#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)
output = self.getOutputFromName(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))
Expand All @@ -60,12 +60,10 @@ def processAlgorithm(self, progress):
#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()
#To do so, we call the getVectorWriter method in the Output object.
#That will give as a SextanteVectorWriter, that we can later use to add features.
provider = vectorLayer.dataProvider()
writer = QgsVectorFileWriter( output, systemEncoding, provider.fields(), provider.geometryType(), provider.crs() )
writer = output.getVectorWriter( provider.fields(), provider.geometryType(), provider.crs() )

#Now we take the selected features and add them to the output layer
selection = vectorLayer.selectedFeatures()
Expand Down
6 changes: 5 additions & 1 deletion python/plugins/sextante/algs/SextanteAlgorithmProvider.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@
from sextante.algs.SaveSelectedFeatures import SaveSelectedFeatures
from sextante.algs.Explode import Explode
from sextante.algs.AutoincrementalField import AutoincrementalField
from sextante.algs.FieldPyculator import FieldsPyculator

class SextanteAlgorithmProvider(AlgorithmProvider):

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

def initializeSettings(self):
AlgorithmProvider.initializeSettings(self)
Expand All @@ -33,3 +34,6 @@ def getIcon(self):

def _loadAlgorithms(self):
self.algs = self.alglist

def supportsNonFileBasedOutput(self):
return True
17 changes: 15 additions & 2 deletions python/plugins/sextante/core/AlgorithmProvider.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from sextante.core.SextanteConfig import Setting, SextanteConfig
import os
from PyQt4 import QtGui
from qgis.core import *

class AlgorithmProvider():
'''this is the base class for algorithms providers.
Expand Down Expand Up @@ -67,7 +68,19 @@ def getSupportedOutputRasterLayerExtensions(self):
return ["tif"]

def getSupportedOutputVectorLayerExtensions(self):
return ["shp"]
formats = QgsVectorFileWriter.supportedFiltersAndFormats()
extensions = ["shp"]#shp is the default, should be the first
for extension in formats.keys():
extension = str(extension)
extension = extension[extension.find('*.') + 2:]
extension = extension[:extension.find(" ")]
if extension.lower() != "shp":
extensions.append(extension)
return extensions
#return ["shp"]

def getSupportedOutputTableExtensions(self):
return ["dbf"]
return ["dbf"]

def supportsNonFileBasedOutput(self):
return False
21 changes: 16 additions & 5 deletions python/plugins/sextante/core/GeoAlgorithm.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
from sextante.parameters.ParameterRaster import ParameterRaster
from sextante.parameters.ParameterVector import ParameterVector
from PyQt4 import QtGui
from PyQt4.QtCore import *
from qgis.core import *
import os.path
from sextante.core.SextanteUtils import SextanteUtils
from sextante.parameters.ParameterMultipleInput import ParameterMultipleInput
Expand Down Expand Up @@ -117,20 +119,27 @@ def execute(self, progress):
except GeoAlgorithmExecutionException, gaee:
SextanteLog.addToLog(SextanteLog.LOG_ERROR, gaee.msg)
raise gaee
except Exception, e:
except:
#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", "|"))
lines = ["Uncaught error while executing algorithm"]
errstring = traceback.format_exc()
newline = errstring.find("\n")
if newline != -1:
lines.append(errstring[:newline])
else:
lines.append(errstring)
lines.append(errstring.replace("\n", "|"))
SextanteLog.addToLog(SextanteLog.LOG_ERROR, lines)
raise GeoAlgorithmExecutionException(str(e))
raise GeoAlgorithmExecutionException(errstring)

def checkOutputFileExtensions(self):
'''Checks if the values of outputs are correct and have one of the supported output extensions.
If not, it adds the first one of the supported extensions, which is assumed to be the default one'''
for out in self.outputs:
if (not out.hidden) and out.value != None:
if not os.path.isabs(out.value):
continue
if isinstance(out, OutputRaster):
exts = self.provider.getSupportedOutputRasterLayerExtensions()
elif isinstance(out, OutputVector):
Expand Down Expand Up @@ -263,3 +272,5 @@ def getAsCommand(self):
s+=out.getValueAsCommandLineParameter() + ","
s= s[:-1] + ")"
return s


13 changes: 5 additions & 8 deletions python/plugins/sextante/core/Sextante.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,24 +287,21 @@ def runAlgorithm(algOrName, onFinish, *args):

msg = alg.checkParameterValuesBeforeExecuting()
if msg:
try:
QMessageBox.critical(None, "Unable to execute algorithm", msg)
return
except:
print ("Unable to execute algorithm\n" + msg)
return

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

QApplication.setOverrideCursor(QCursor(Qt.WaitCursor))
if SextanteConfig.getSetting(SextanteConfig.USE_THREADS) and onFinish:
if SextanteConfig.getSetting(SextanteConfig.USE_THREADS):
algEx = AlgorithmExecutor(alg)
progress = QProgressDialog()
progress.setWindowTitle(alg.name)
progress.setLabelText("Executing %s..." % alg.name)
def finish():
QApplication.restoreOverrideCursor()
onFinish(alg)
if onFinish is not None:
onFinish(alg)
progress.close()
def error(msg):
QApplication.restoreOverrideCursor()
Expand All @@ -323,8 +320,8 @@ def cancel():
algEx.start()
algEx.wait()
else:
UnthreadedAlgorithmExecutor.runalg(alg, SilentProgress())
if onFinish:
ret = UnthreadedAlgorithmExecutor.runalg(alg, SilentProgress())
if onFinish is not None and ret:
onFinish(alg)
QApplication.restoreOverrideCursor()
return alg
Expand Down
9 changes: 7 additions & 2 deletions python/plugins/sextante/core/SextanteConfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ class SextanteConfig():
USE_FILENAME_AS_LAYER_NAME = "USE_FILENAME_AS_LAYER_NAME"
KEEP_DIALOG_OPEN = "KEEP_DIALOG_OPEN"
USE_THREADS = "USE_THREADS"
SHOW_DEBUG_IN_DIALOG = "SHOW_DEBUG_IN_DIALOG"
RECENT_ALGORITHMS = "RECENT_ALGORITHMS"

settings = {}
settingIcons= {}
Expand All @@ -24,6 +26,7 @@ def initialize():
icon = QtGui.QIcon(os.path.dirname(__file__) + "/../images/alg.png")
SextanteConfig.settingIcons["General"] = icon
SextanteConfig.addSetting(Setting("General", SextanteConfig.USE_THREADS, "Run algorithms in a new thread (still unstable)", False))
SextanteConfig.addSetting(Setting("General", SextanteConfig.SHOW_DEBUG_IN_DIALOG, "Show debug information and commands executed in the execution dialog's Log panel (threaded execution only)", True))
SextanteConfig.addSetting(Setting("General", SextanteConfig.KEEP_DIALOG_OPEN, "Keep dialog open after running an algorithm", False))
SextanteConfig.addSetting(Setting("General", SextanteConfig.USE_SELECTED, "Use only selected features in external applications", True))
SextanteConfig.addSetting(Setting("General", SextanteConfig.TABLE_LIKE_PARAM_PANEL, "Show table-like parameter panels", False))
Expand All @@ -35,6 +38,7 @@ def initialize():
SextanteConfig.addSetting(Setting("General", SextanteConfig.VECTOR_POINT_STYLE,"Style for point layers",""))
SextanteConfig.addSetting(Setting("General", SextanteConfig.VECTOR_LINE_STYLE,"Style for line layers",""))
SextanteConfig.addSetting(Setting("General", SextanteConfig.VECTOR_POLYGON_STYLE,"Style for polygon layers",""))
SextanteConfig.addSetting(Setting("General", SextanteConfig.RECENT_ALGORITHMS,"Recent algs","", hidden=True))

@staticmethod
def setGroupIcon(group, icon):
Expand Down Expand Up @@ -112,12 +116,13 @@ def setSettingValue(name, value):

class Setting():
'''A simple config parameter that will appear on the SEXTANTE config dialog'''
def __init__(self, group, name, description, default):
def __init__(self, group, name, description, default, hidden = False):
self.group=group
self.name = name
self.description = description
self.default = default
self.value = default
self.hidden = hidden

def __str__(self):
return self.name + "=" + str(self.value)
return self.name + "=" + str(self.value)
8 changes: 8 additions & 0 deletions python/plugins/sextante/core/SextanteLog.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import datetime
import os
from sextante.core.SextanteUtils import SextanteUtils
from sextante.core.SextanteConfig import SextanteConfig
import codecs
from PyQt4 import QtGui
class SextanteLog():
Expand Down Expand Up @@ -47,6 +48,8 @@ def addToLog(msgtype, msg):
algname = algname[:algname.index("\"")]
if algname not in SextanteLog.recentAlgs:
SextanteLog.recentAlgs.append(algname)
recentAlgsString = ';'.join(SextanteLog.recentAlgs[-6:])
SextanteConfig.setSettingValue(SextanteConfig.RECENT_ALGORITHMS, recentAlgsString)
except:
pass

Expand Down Expand Up @@ -86,6 +89,11 @@ def getLogEntries():

@staticmethod
def getRecentAlgorithms():
recentAlgsSetting = SextanteConfig.getSetting(SextanteConfig.RECENT_ALGORITHMS)
try:
SextanteLog.recentAlgs = recentAlgsSetting.split(';')
except:
pass
return SextanteLog.recentAlgs


Expand Down
12 changes: 3 additions & 9 deletions python/plugins/sextante/ftools/Buffer.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@
from sextante.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException
from sextante.core.SextanteLog import SextanteLog

def buffering(progress, output, distance, field, useSelection, useField, layer, dissolve, segments ):
settings = QSettings()
systemEncoding = settings.value( "/UI/encoding", "System" ).toString()
def buffering(progress, writer, distance, field, useSelection, useField, layer, dissolve, segments ):
GEOS_EXCEPT = True
FEATURE_EXCEPT = True
vproviderA = layer.dataProvider()
Expand All @@ -15,10 +13,6 @@ def buffering(progress, output, distance, field, useSelection, useField, layer,
fields = vproviderA.fields()
if useField:
field = vproviderA.fieldNameIndex(field)
writer = QgsVectorFileWriter(output, systemEncoding, fields, QGis.WKBPolygon, vproviderA.crs() )
# check if writer was created properly, if not, return with error
if writer.hasError():
raise GeoAlgorithmExecutionException("Could not create output file");
outFeat = QgsFeature()
inFeat = QgsFeature()
inGeom = QgsGeometry()
Expand Down Expand Up @@ -64,9 +58,9 @@ def buffering(progress, output, distance, field, useSelection, useField, layer,
for inFeat in selectionA:
atMap = inFeat.attributeMap()
if useField:
value = atMap[ self.myParam ].toDouble()[ 0 ]
value = atMap[ field ].toDouble()[ 0 ]
else:
value = self.myParam
value = distance
inGeom = QgsGeometry( inFeat.geometry() )
try:
outGeom = inGeom.buffer( float( value ), segments )
Expand Down
7 changes: 1 addition & 6 deletions python/plugins/sextante/ftools/Centroids.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,9 @@ def defineCharacteristics(self):
def processAlgorithm(self, progress):
layer = QGisLayers.getObjectFromUri(self.getParameterValue(Centroids.INPUT_LAYER))

outFileName = self.getOutputValue(Centroids.OUTPUT_LAYER)

provider = layer.dataProvider()

settings = QSettings()
encoding = settings.value( "/UI/encoding", "System" ).toString()

writer = QgsVectorFileWriter(outFileName, encoding, provider.fields(),
writer = self.getOutputFromName(Centroids.OUTPUT_LAYER).getVectorWriter(provider.fields(),
QGis.WKBPoint, provider.crs())

allAttrs = provider.attributeIndexes()
Expand Down
Loading

0 comments on commit 7ceb460

Please sign in to comment.