Skip to content
Permalink
Browse files

changed adddfield and fieldscalculator algorithms (outputs are not hi…

…dden now)

Fixed problem with logging in unthreaded execution

git-svn-id: http://sextante.googlecode.com/svn/trunk/soft/bindings/qgis-plugin@287 881b9c09-3ef8-f3c2-ec3d-21d735c97f4d
  • Loading branch information
volayaf@gmail.com
volayaf@gmail.com committed Jul 5, 2012
1 parent 81c375e commit a289e139685bf1b27134a982a0797e7a66419453
@@ -29,15 +29,34 @@ def defineCharacteristics(self):
self.addParameter(ParameterVector(self.INPUT_LAYER, "Input layer", ParameterVector.VECTOR_TYPE_ANY, False))
self.addParameter(ParameterString(self.FIELD_NAME, "Field name"))
self.addParameter(ParameterSelection(self.FIELD_TYPE, "Field type", self.TYPE_NAMES))
self.addOutput(OutputVector(self.OUTPUT_LAYER, "Output layer", True))
self.addOutput(OutputVector(self.OUTPUT_LAYER, "Output layer"))

def processAlgorithm(self, progress):
inputFilename = self.getParameterValue(self.INPUT_LAYER)
layer = QGisLayers.getObjectFromUri(inputFilename)
caps = layer.dataProvider().capabilities()
if caps & QgsVectorDataProvider.AddAttributes:
fieldName = self.getParameterValue(self.FIELD_NAME)
fieldType = self.TYPES[self.getParameterValue(self.FIELD_TYPE)]
layer.dataProvider().addAttributes([QgsField(fieldName, fieldType)])

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)
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() )
inFeat = QgsFeature()
outFeat = QgsFeature()
inGeom = QgsGeometry()
nFeat = vprovider.featureCount()
nElement = 0
while vprovider.nextFeature(inFeat):
progress.setPercentage(int((100 * nElement)/nFeat))
nElement += 1
inGeom = inFeat.geometry()
outFeat.setGeometry( inGeom )
atMap = inFeat.attributeMap()
outFeat.setAttributeMap( atMap )
outFeat.addAttribute( len(vprovider.fields()), QVariant() )
writer.addFeature( outFeat )
del writer

@@ -27,32 +27,44 @@ def defineCharacteristics(self):
self.addParameter(ParameterVector(self.INPUT_LAYER, "Input layer", ParameterVector.VECTOR_TYPE_ANY, False))
self.addParameter(ParameterString(self.FIELD_NAME, "Result field name"))
self.addParameter(ParameterString(self.FORMULA, "Formula"))
self.addOutput(OutputVector(self.OUTPUT_LAYER, "Output layer", True))
self.addOutput(OutputVector(self.OUTPUT_LAYER, "Output layer"))

def processAlgorithm(self, progress):
inputFilename = self.getParameterValue(self.INPUT_LAYER)
fieldname = self.getParameterValue(self.FIELD_NAME)
formula = self.getParameterValue(self.FORMULA)
layer = QGisLayers.getObjectFromUri(inputFilename)
provider = layer.dataProvider()
caps = provider.capabilities()
if caps & QgsVectorDataProvider.AddAttributes:
fieldName = self.getParameterValue(self.FIELD_NAME)
layer.dataProvider().addAttributes([QgsField(fieldName, QVariant.Double)])
feat = QgsFeature()
allAttrs = provider.attributeIndexes()
provider.select(allAttrs)
fields = provider.fields()
while provider.nextFeature(feat):
attrs = feat.attributeMap()
expression = formula
for (k,attr) in attrs.iteritems():
expression = expression.replace(str(fields[k].name()), str(attr.toString()))
try:
result = eval(expression)
except Exception:
raise GeoAlgorithmExecutionException("Problem evaluation formula: Wrong field values or formula")
attrs[len(attrs) - 1] = QVariant(result)
provider.changeAttributeValues({feat.id() : attrs})
settings = QSettings()
systemEncoding = settings.value( "/UI/encoding", "System" ).toString()
output = self.getOutputValue(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() )
inFeat = QgsFeature()
outFeat = QgsFeature()
inGeom = QgsGeometry()
nFeat = vprovider.featureCount()
nElement = 0
while vprovider.nextFeature(inFeat):
progress.setPercentage(int((100 * nElement)/nFeat))
attrs = inFeat.attributeMap()
expression = formula
for (k,attr) in attrs.iteritems():
expression = expression.replace(str(fields[k].name()), str(attr.toString()))
try:
result = eval(expression)
except Exception:
raise GeoAlgorithmExecutionException("Problem evaluation formula: Wrong field values or formula")
nElement += 1
inGeom = inFeat.geometry()
outFeat.setGeometry( inGeom )
atMap = inFeat.attributeMap()
outFeat.setAttributeMap( atMap )
outFeat.addAttribute( len(vprovider.fields()), QVariant(result) )
writer.addFeature( outFeat )
del writer


def checkParameterValuesBeforeExecuting(self):
@@ -81,7 +81,7 @@ def processAlgorithm(self, progress):
nElement += 1
progress.setPercentage(int(nElement/nFeat * 100))
atMap = inFeat.attributeMap()
tempItem = atMap[ self.myParam ]
tempItem = atMap[ field ]
if tempItem.toString().trimmed() == item.toString().trimmed():
add = True
if first:
@@ -138,7 +138,7 @@ def processAlgorithm(self, progress):
nElement += 1
progress.setPercentage(int(nElement/nFeat * 100))
atMap = inFeat.attributeMap()
tempItem = atMap[ self.myParam ]
tempItem = atMap[ field ]
if tempItem.toString().trimmed() == item.toString().trimmed():
if first:
QgsGeometry( inFeat.geometry() )
@@ -79,7 +79,7 @@ def runalgIterating(self):
self.runalg()
self.iterated.emit(i)
i += 1
except BaseException as e:
except BaseException, e:
self.error.emit(str(e))
print "Error iterating " + str(e)
except:
@@ -202,6 +202,9 @@ def accept(self):
if iterateParam:
UnthreadedAlgorithmExecutor.runalgIterating(self.alg, iterateParam, self)
else:
command = self.alg.getAsCommand()
if command:
SextanteLog.addToLog(SextanteLog.LOG_ALGORITHM, command)
if UnthreadedAlgorithmExecutor.runalg(self.alg, self):
SextantePostprocessing.handleAlgorithmResults(self.alg, not keepOpen)
self.dialog.executed = True
@@ -254,8 +257,8 @@ def error(self, msg):
@pyqtSlot(int)
def iterate(self, i):
SextanteLog.addToLog(SextanteLog.LOG_INFO,
"Algorithm %s iteration #%i completed" % (elf.alg.name, i))
"Algorithm %s iteration #%i completed" % (self.alg.name, i))

@pyqtSlot()
def cancel(self):
SextanteLog.addToLog(SextanteLog.LOG_INFO,
@@ -30,5 +30,6 @@ def handleAlgorithmResults(alg, showResults = True):
SextanteResults.addResult(out.description, out.value)
htmlResults = True
if showResults and htmlResults:
QApplication.restoreOverrideCursor()
dlg = ResultsDialog()
dlg.exec_()

0 comments on commit a289e13

Please sign in to comment.
You can’t perform that action at this time.