Skip to content
Permalink
Browse files

Fix auto creation of variables when running script algorithms

  • Loading branch information
nyalldawson committed Jun 26, 2017
1 parent 94619d0 commit a72eea22568862add43af1133e6cc277eae57e5c
@@ -1,8 +1,14 @@
##Table=group
##Input=vector
##Fields=Field Input

#inputs

##Input=source
##Fields=field Input
##Frequency=output table

#outputs


from processing.tools.vector import TableWriter
from collections import defaultdict
from qgis.core import QgsProcessingUtils
@@ -19,14 +19,12 @@
feedback.pushInfo("'To keep' value has been modified to be at least 1.")
To_keep = 1


source = self.parameterAsSource(parameters, 'Polygons', context)
count = source.featureCount()
count = Polygons.featureCount()
(sink, Biggest_parts) = self.parameterAsSink(parameters, 'Biggest parts', context,
source.fields(), QgsWkbTypes.MultiPolygon, source.sourceCrs())
Polygons.fields(), QgsWkbTypes.MultiPolygon, Polygons.sourceCrs())


for n, feat in enumerate(source.getFeatures()):
for n, feat in enumerate(Polygons.getFeatures()):
if feedback.isCanceled():
break
feedback.setProgress(int(100 * n / count))
@@ -1,25 +1,36 @@
##Vector table tools=group
##input=vector

# inputs


##input=source
##class_field=field input
##value_field=field input
##N_unique_values=output vector
##N_unique_values=sink

#outputs
##N_unique_values=output outputVector

from qgis.PyQt.QtCore import QVariant
from qgis.core import QgsFeature, QgsField, QgsProcessingUtils

layer = QgsProcessingUtils.mapLayerFromString(input, context)
fields = layer.fields()
fields = input.fields()
fields.append(QgsField('UNIQ_COUNT', QVariant.Int))
writer, writer_dest = QgsProcessingUtils.createFeatureSink(N_unique_values, context, fields, layer.wkbType(), layer.crs())

class_field_index = layer.fields().lookupField(class_field)
value_field_index = layer.fields().lookupField(value_field)
(sink, N_unique_values) = self.parameterAsSink(parameters, 'N_unique_values', context,
fields, input.wkbType(), input.sourceCrs())


class_field_index = input.fields().lookupField(class_field)
value_field_index = input.fields().lookupField(value_field)

outFeat = QgsFeature()
classes = {}
feats = QgsProcessingUtils.getFeatures(layer, context)
nFeat = QgsProcessingUtils.featureCount(layer, context)
feats = input.getFeatures()
nFeat = input.featureCount()
for n, inFeat in enumerate(feats):
if feedback.isCanceled():
break
feedback.setProgress(int(100 * n / nFeat))
attrs = inFeat.attributes()
clazz = attrs[class_field_index]
@@ -29,15 +40,15 @@
if value not in classes[clazz]:
classes[clazz].append(value)

feats = processing.features(layer)
feats = input.getFeatures()
for n, inFeat in enumerate(feats):
if feedback.isCanceled():
break
feedback.setProgress(int(100 * n / nFeat))
inGeom = inFeat.geometry()
outFeat.setGeometry(inGeom)
attrs = inFeat.attributes()
clazz = attrs[class_field_index]
attrs.append(len(classes[clazz]))
outFeat.setAttributes(attrs)
writer.addFeature(outFeat)

del writer
sink.addFeature(outFeat)
@@ -181,16 +181,53 @@ def processAlgorithm(self, parameters, context, feedback):
ns['scriptDescriptionFile'] = self.descriptionFile

for param in self.parameterDefinitions():
ns[param.name()] = parameters[param.name()]
method = None
if param.type() == "boolean":
method = self.parameterAsBool
elif param.type() == "crs":
method = self.parameterAsCrs
elif param.type() == "layer":
method = self.parameterAsLayer
elif param.type() == "extent":
method = self.parameterAsExtent
elif param.type() == "point":
method = self.parameterAsPoint
elif param.type() == "file":
method = self.parameterAsFile
elif param.type() == "matrix":
method = self.parameterAsMatrix
elif param.type() == "multilayer":
method = self.parameterAsLayerList
elif param.type() == "number":
method = self.parameterAsDouble
elif param.type() == "range":
method = self.parameterAsRange
elif param.type() == "raster":
method = self.parameterAsRasterLayer
elif param.type() == "enum":
method = self.parameterAsEnum
elif param.type() == "string":
method = self.parameterAsString
elif param.type() == "expression":
method = self.parameterAsString
elif param.type() == "vector":
method = self.parameterAsVectorLayer
elif param.type() == "field":
method = self.parameterAsString
elif param.type() == "source":
method = self.parameterAsSource

if method:
ns[param.name()] = method(parameters, param.name(), context)

for out in self.outputDefinitions():
ns[out.name()] = None

ns['self'] = self
ns['parameters'] = parameters
ns['feedback'] = feedback
ns['context'] = context

# for out in self.outputDefinitions():
# ns[out.name()] = out.value

variables = re.findall('@[a-zA-Z0-9_]*', self.script)
script = 'import processing\n'
script += self.script

0 comments on commit a72eea2

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