2

In QGIS processing scripts we define input vector layers by

##inputlayer=vector

wich in the gui generates a drop down list from what the user can select one of all available layers.

Normaly, processing is defined for only one distinct geometry type, let's say Point. If the project contains of a great bunch of, let's say Line Layers, we have all these Line layers in a dropdown, where only the Point layer should be selectable.

So the question is:

In QGIS processing scripts, is there a way to restrict vector input to certain geometry type (sth. like ##input=vector point)?

  • 2
    Did you try ##input=vector point? ;) – Joseph Sep 30 '16 at 9:19
  • No, just guessed, but: funny, it works! This is not in the doku (at least the german ver.), but, yes, that's the answer :-) – Jochen Schwarze Sep 30 '16 at 9:58
  • Nah, I don't think it's mentioned in the English docs either (or atleast I couldn't find it). But a very nice guess! Post it as an answer as atleast then it will be documented on the internet =) – Joseph Sep 30 '16 at 9:59
3

The guess in my question really works. So as I have tried so far the declarations

##input_points=vector point
##input_lines=vector line
##input_polygons=vector polygon

are valid and restrict the combo box to the declared geometry type.

| improve this answer | |
0

In QGIS 3.x suppose you have an input file, using the template file it uses the following code:

def initAlgorithm(self, config=None):
    """
    Here we define the inputs and output of the algorithm, along
    with some other properties.
"""

# We add the input vector features source. It can have any kind of
# geometry.
self.addParameter(
    QgsProcessingParameterFeatureSource(
        self.INPUT,
        self.tr('Input Layer'),
        [QgsProcessing.TypeVectorAnyGeometry] # accepts any geometry
    )
)

The default input will look like this in the tool:

enter image description here

You can change the default line [QgsProcessing.TypeVectorAnyGeometry] that accepts any geometry to use only specific geometry as follows:

[QgsProcessing.TypeVectorPoint] # accepts only point geometry.
[QgsProcessing.TypeVectorLine] # accepts only Line geometry.
[QgsProcessing.TypeVectorPolygon] # accepts only polygon geometry.

In case of point geometry the tool will look like this after customising the above line for point geometry:

enter image description here

In case of polygon geometry the tool will look like this after customising the above line for polygon geometry:

enter image description here

| improve this answer | |

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.