4

When using the @alg decorator to scaffold a processing algorithm, we use the @alg.input to declare new widgets for the interface. We can declare these as alg.SOURCE for layers we use in the algorithm.

Unlike the standard approach, there does not appear to be a place where we can identify allowable geometry types (Point/Line/Polygon) for the layer. I've tried adding a parameter types=[QgsWkbTypes.MultiLineString] however, it does not appear to filter the allowable selections in the drop-down. I still get polygons and points as well.

Is this a bug because while the @alg.input accepts the types=[] parameter, it does not appear to have any impact on what appears in the dropdown?

import pyodbc
from PyQt5.QtCore import QVariant
from qgis import processing
from qgis.processing import alg
from qgis.core import QgsProject, QgsFeatureRequest, QgsDataSourceUri, QgsVectorLayer,\
    QgsFeatureSink, QgsWkbTypes, QgsCoordinateReferenceSystem, QgsField, QgsFeature,\
    QgsVectorFileWriter, QgsProcessingException

@alg(name='roadsassessment', label='Call 3 Mobile Roads Assessment', group='bbfcall3', group_label='BBF Call 3')


@alg.input(type=alg.INT, name='APPNUM', label='Application number')
@alg.input(type=alg.SOURCE, name='ELIGIBLEROADSSOURCE', types= [QgsWkbTypes.MultiLineString], label='Eligible Roads')
@alg.input(type=alg.SOURCE, name='APPCOVERAGESOURCE', types=[QgsWkbTypes.MultiPolygon], label='Application coverage')
@alg.input(type=alg.SOURCE, name='MOBILECOVERAGESOURCE', label='Full mobile coverage')

1 Answer 1

6

You have to use QgsProcessing.SourceType enum:

Options for a vector layer

QgsProcessing.TypeVector 
QgsProcessing.TypeVectorPoint
QgsProcessing.TypeVectorLine
QgsProcessing.TypeVectorPolygon
QgsProcessing.TypeVectorAnyGeometry

Example:

types= [QgsProcessing.TypeVectorLine, QgsProcessing.TypeVectorPolygon]

enter image description here

4
  • Thank you so much Kadir ! QGIS is not easy to code for me as I find I'm not very comfortable with the way their documentation is laid out. It feels a little out of reach for me. I'm always struggling to find the right class. Commented Mar 11, 2023 at 12:17
  • I was just like you when I started coding in QGIS. Commented Mar 11, 2023 at 12:21
  • Is there a way to select only geometryless tables? Commented Sep 27, 2023 at 11:38
  • 1
    @FlorianJenn I guess no. QgsProcessing.TypeVector may be the best option because geometryless layer is a vector layer in QGIS. Please check the docs in the link above. Commented Sep 27, 2023 at 11:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.