[QGIS-Developer] How to use postProcessLayer in plugin processing
René-Luc Dhont
rldhont at gmail.com
Thu May 16 00:33:39 PDT 2019
Hi Devs,
I would like to set the style an output layer in a script processing. My
algorithm is added to QGIS Processing by a plugin.
I have tested this :
```
class loadNamedStylePostProcessor(QgsProcessingLayerPostProcessorInterface):
def __init__(self, outputName, feedback):
super().__init__()
self.outputName = outputName
feedback.pushInfo('loadNamedStylePostProcessor init')
feedback.pushInfo(self.outputName)
def postProcessLayer(layer, context, feedback):
feedback.pushInfo('postProcessLayer')
feedback.pushInfo(self.outputName)
feedback.pushInfo(layer.name())
class testPostProcessLayerAlgorithm(QgsProcessingAlgorithm):
OUTPUT = 'OUTPUT'
INPUT = 'INPUT'
def initAlgorithm(self, config):
self.addParameter(
QgsProcessingParameterFeatureSource(
self.INPUT,
self.tr('Input layer'),
[QgsProcessing.TypeVectorAnyGeometry]
)
)
self.addParameter(
QgsProcessingParameterFeatureSink(
self.OUTPUT,
self.tr('Output layer')
)
)
def processAlgorithm(self, parameters, context, feedback):
source = self.parameterAsSource(parameters, self.INPUT, context)
(sink, dest_id) = self.parameterAsSink(parameters, self.OUTPUT,
context, source.fields(), source.wkbType(),
source.sourceCrs())
total = 100.0 / source.featureCount() if source.featureCount()
else 0
features = source.getFeatures()
for current, feature in enumerate(features):
# Stop the algorithm if cancel button has been clicked
if feedback.isCanceled():
break
# Add a feature in the sink
sink.addFeature(feature, QgsFeatureSink.FastInsert)
# Update the progress bar
feedback.setProgress(int(current * total))
context.layerToLoadOnCompletionDetails(
dest_id
).setPostProcessor(
loadNamedStylePostProcessor(self.OUTPUT, feedback)
)
return {self.OUTPUT: dest_id}
def name(self):
return 'test_post_process_layer'
def displayName(self):
return self.tr('Test postProcessLayer')
def group(self):
return self.tr('Tests')
def groupId(self):
return 'tests'
def tr(self, string):
return QCoreApplication.translate('Processing', string)
def createInstance(self):
return self.__class__()
```
The postProcesser is well initialize, but I never have the messages form
postProcessLayer.
Any one has already use it in some code ?
Regards,
René-Luc
More information about the QGIS-Developer
mailing list