PyQGIS – Fixing TypeError When Extracting Vertices in QgsProcessingFeatureSourceDefinition

layerspyqgisqgsvectorlayertypeerrorvertices

I want to extract 70% of the vertices based on a selection but I get the following error code:

Traceback (most recent call last):
  File "C:\PROGRA~1\QGIS32~1.1\apps\Python39\lib\code.py", line 90, in runcode
    exec(code, self.locals)
  File "<input>", line 1, in <module>
  File "<string>", line 15, in <module>
TypeError: QgsProcessingFeatureSourceDefinition(): arguments did not match any overloaded call:
  overload 1: argument 1 has unexpected type 'QgsVectorLayer'
  overload 2: argument 1 has unexpected type 'QgsVectorLayer'
  overload 3: argument 1 has unexpected type 'QgsVectorLayer'

I get the error when I use the code below. I can see in my active layer that the selection works.

import os
import shutil
import time

t_begin = time.localtime()
print(time.strftime('%H:%M:%S', t_begin))

particulier = "C:/Users/laptop Innoforte 1/Innoforte/Innoforte Druten - Documenten/9. ICT/QGIS projecten/GOV haalbaarheidsstudie v1/70_particulier"

layer = iface.activeLayer()
layer.selectByExpression("Wtype != 'Appartement' and Wtype != 'Utiliteit' and cooperatie is not 2")

processing.run("native:randomextract", {
'INPUT': QgsProcessingFeatureSourceDefinition(layer, selectedFeaturesOnly=True,
featureLimit=-1, geometryCheck=QgsFeatureRequest.GeometryAbortOnInvalid),
'METHOD': 0, 'NUMBER': 70, 'OUTPUT': particulier})

iface.addVectorLayer(particulier, '', 'ogr')

Best Answer

I could get the same error:

Traceback (most recent call last):
  File "C:\PROGRA~1\QGIS32~1.0\apps\Python39\lib\code.py", line 90, in runcode
    exec(code, self.locals)
  File "<input>", line 1, in <module>
  File "<string>", line 14, in <module>
TypeError: QgsProcessingFeatureSourceDefinition(): arguments did not match any overloaded call:
  overload 1: argument 1 has unexpected type 'QgsVectorLayer'
  overload 2: argument 1 has unexpected type 'QgsVectorLayer'
  overload 3: argument 1 has unexpected type 'QgsVectorLayer'

To overcome this TypeError, please modify this part of the code:

processing.run("native:randomextract", {
    'INPUT': QgsProcessingFeatureSourceDefinition(
                layer,
                selectedFeaturesOnly=True,
                featureLimit=-1,
                geometryCheck=QgsFeatureRequest.GeometryAbortOnInvalid),
    'METHOD':0,
    'NUMBER':70,
    'OUTPUT':particulier
    })

enhancing it with layer.id(), like this:

processing.run("native:randomextract", {
    'INPUT': QgsProcessingFeatureSourceDefinition(
                layer.id(),
                selectedFeaturesOnly=True,
                featureLimit=-1,
                geometryCheck=QgsFeatureRequest.GeometryAbortOnInvalid),
    'METHOD':0,
    'NUMBER':70,
    'OUTPUT':particulier
    })

or using the layer.name().

The QgsProcessingFeatureSourceDefinition class requires the source to be a QString or a QgsProperty:

Usually a static property set to a source layer's ID or file name.


References: