2

I created a model and I would like to have it as a python script.

this is what i see when i run my model

This is what I see when I run my model and I can select a layer from this window. I don't necessarily need to select a layer, it can just process the active layer.

When I export this model as python code this is the corresponding part that select myLayer parameter.

from qgis.core import QgsProcessing
from qgis.core import QgsProcessingAlgorithm
from qgis.core import QgsProcessingMultiStepFeedback
from qgis.core import QgsProcessingParameterVectorLayer
from qgis.core import QgsProcessingParameterFeatureSink
import processing


class myModel(QgsProcessingAlgorithm):

    def initAlgorithm(self, config=None):
        self.addParameter(QgsProcessingParameterVectorLayer('addlayer', 'myLayer', types=[QgsProcessing.TypeVectorAnyGeometry], defaultValue=None))
        self.addParameter(QgsProcessingParameterFeatureSink('Result', 'Result', type=QgsProcessing.TypeVectorAnyGeometry, createByDefault=True, defaultValue=None))

Is it possible to edit this code in order to make it target the active layer to process the model?

2
  • 1
    You ask how you can achieve what the modeler does using the python code. But you've already exported as python equivalent (exported by Processing Modeler).Therefore I've editted the question body and the title to be compatible with last question sentence: "Is it possible to edit this code in order to make it target the active layer to process the model?" Jan 12 '20 at 20:42
  • Thank you for the edits, look better and clearer.
    – puredata
    Jan 12 '20 at 20:45
2
  • Add from qgis.utils import iface line to import section.
  • Remove self.addParameter(QgsProcessingParameterVectorLayer('addlayer', 'myLayer', ...) line.
  • And change all parameters['addlayer'] into iface.activeLayer().

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.