2012-02-03 15:03:47 +00:00
|
|
|
from PyQt4.QtGui import *
|
2012-02-12 10:57:30 +00:00
|
|
|
from sextante.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException
|
2012-04-23 21:08:18 +00:00
|
|
|
from sextante.core.QGisLayers import QGisLayers
|
|
|
|
from sextante.core.SextanteUtils import SextanteUtils
|
2012-02-03 15:03:47 +00:00
|
|
|
|
|
|
|
class AlgorithmExecutor:
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def runalg(alg, progress):
|
2012-04-01 09:15:16 +00:00
|
|
|
'''executes a given algorithm, showing its progress in the progress object passed along.
|
|
|
|
Return true if everything went OK, false if the algorithm was canceled or there was
|
|
|
|
any problem and could not be completed'''
|
2012-02-12 10:57:30 +00:00
|
|
|
try:
|
|
|
|
alg.execute(progress)
|
2012-04-01 09:15:16 +00:00
|
|
|
return not alg.canceled
|
2012-02-12 10:57:30 +00:00
|
|
|
except GeoAlgorithmExecutionException, e :
|
2012-02-26 12:47:07 +00:00
|
|
|
QMessageBox.critical(None, "Error", e.msg)
|
2012-04-01 09:15:16 +00:00
|
|
|
return False
|
2012-02-12 10:57:30 +00:00
|
|
|
|
2012-04-18 19:42:16 +00:00
|
|
|
@staticmethod
|
2012-04-23 21:08:18 +00:00
|
|
|
def runalgIterating(alg,paramToIter,progress):
|
|
|
|
#generate all single-feature layers
|
|
|
|
settings = QSettings()
|
|
|
|
systemEncoding = settings.value( "/UI/encoding", "System" ).toString()
|
|
|
|
layerfile = alg.getParameterFromName(paramToIter)
|
|
|
|
layer = QGisLayers.getObjectFromUri(layerfile, False)
|
|
|
|
provider = layer.dataProvider()
|
|
|
|
allAttrs = provider.attributeIndexes()
|
|
|
|
provider.select( allAttrs )
|
|
|
|
feat = QgsFeature()
|
|
|
|
filelist = []
|
|
|
|
outputs = {}
|
|
|
|
while provider.nextFeature(feat):
|
|
|
|
output = SextanteUtils.getTempFilename("shp")
|
|
|
|
filelist.append(output)
|
|
|
|
writer = QgsVectorFileWriter(output, systemEncoding,provider.fields(), provider.geometryType(), provider.crs() )
|
|
|
|
writer.addFeature(feat)
|
|
|
|
del writer
|
|
|
|
#now run all the algorithms
|
|
|
|
for out in alg.outputs:
|
|
|
|
output[out.name] = out.value
|
|
|
|
|
|
|
|
i = 1
|
|
|
|
for f in filelist:
|
|
|
|
alg.setOutputValue(paramToIter, f)
|
|
|
|
for out in alg.outputs:
|
|
|
|
filename = outputs[out.name]
|
|
|
|
if filename:
|
|
|
|
filename = filename[:filename.rfind(".")] + "_" + str(i) + filename[filename.rfind("."):]
|
|
|
|
out.value = filename
|
|
|
|
if AlgorithmExecutor.runalg(alg, SilentProgress()):
|
|
|
|
progress.setValue(i/len(f))
|
|
|
|
i+=1
|
|
|
|
else:
|
|
|
|
return False;
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
2012-04-18 19:42:16 +00:00
|
|
|
|
2012-02-03 15:03:47 +00:00
|
|
|
class SilentProgress():
|
|
|
|
|
2012-02-26 12:47:07 +00:00
|
|
|
def setText(self, text):
|
2012-02-03 15:03:47 +00:00
|
|
|
pass
|
|
|
|
|
|
|
|
def setPercentage(self, i):
|
2012-02-12 10:57:30 +00:00
|
|
|
pass
|