6

I wrote a Progressing algorithm using Python. The tool clips DEM by a select polygon in the vector layer. If there is no selection, the tool gives an error. As the script runs anyway for the selected object, I would like to disable the "Selected feature only" option if it is possible.

Any idea if it is possible?

enter image description here

4
+50

Processing adds the Selected features only checkbox if you use a QgsProcessingParameterFeatureSource as input parameter, but it does not add the checkbox if you use a QgsProcessingParameterVectorLayer.

Compare:

enter image description here

Therefore, in your script, instead of using QgsProcessingParameterFeatureSource as input parameter type, use QgsProcessingParameterVectorLayer. Example:

self.addParameter(
    QgsProcessingParameterVectorLayer(
        self.INPUT,
        self.tr('Input layer with QgsProcessingParameterVectorLayer'),
        [QgsProcessing.TypeVectorPolygon]
    )
)

Now, in processAlgorithm() method, get the parameter in this way:

polygon_layer = self.parameterAsVectorLayer(
    parameters,
    self.INPUT,
    context
)

NOTE: As you can see in the screenshot above, the QgsProcessingParameterFeatureSource has some advantages over QgsProcessingParameterVectorLayer. So, if you use the latter, you won't get the handy options at the right hand side. Namely, iterate through features and advanced options buttons.

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.