With PyQGIS I'm trying to split selected features into another vector layer using the Split layer function
The code I have is:
area_layer = map.gpkg
fpath_v = C:/output dirctory/filename.gpkg
processing.run("native:splitvectorlayer", {'INPUT':QgsProcessingFeatureSourceDefinition(area_layer, selectedFeaturesOnly=True, featureLimit=-1, geometryCheck=QgsFeatureRequest.GeometryAbortOnInvalid),'FIELD':'Code','PREFIX_FIELD':False,'FILE_TYPE':0,'OUTPUT':'fpath_v'})
But this produces the following error:
File "C:\PROGRA~1\QGIS33~1.3\apps\Python39\lib\code.py", line 90, in runcode
exec(code, self.locals)
File "<input>", line 1, in <module>
File "<string>", line 28, 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'm not sure how to troubleshoot or go about fixing this.
1 Answer 1
The only issue I've seen is about syntax errors in your Python code.
Try the following:
area_layer_path = 'map.gpkg'
fpath_v = 'C:/output directory/filename.gpkg'
fieldname = 'Code'
vlayer = iface.addVectorLayer(area_layer_path, 'demo', 'ogr')
result = processing.run("native:splitvectorlayer", {
'INPUT': QgsProcessingFeatureSourceDefinition(vlayer.id(), selectedFeaturesOnly=True, featureLimit=-1, geometryCheck=QgsFeatureRequest.GeometryAbortOnInvalid),
'FIELD': fieldname,
'PREFIX_FIELD': False,
'FILE_TYPE':0,
'OUTPUT': fpath_v
})
# will show an OUTPUT_LAYERS empty if selectedFeaturesOnly=True used but not selected before
print(result)
As mentioned in the comment, you may need to set a selection manually or automatically on the layer before executing the processing.run(...
part. Otherwise, selectedFeaturesOnly=True
may cause issues.
map.gpkg
will give a syntax error. And please specify where you make your selection. When you use only a filepath as the source, QGIS is not aware of any selection, it should be a selection made on aQgsVectorLayer
within the current project.