1

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.

Taras
35.8k5 gold badges77 silver badges152 bronze badges
asked Apr 8, 2024 at 14:57
2
  • 2
    Please provide a minimum working example of your code. 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 a QgsVectorLayer within the current project. Commented Apr 8, 2024 at 16:01
  • Please, do not forget about "What should I do when someone answers my question?" Commented Jul 3 at 11:49

1 Answer 1

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.

Taras
35.8k5 gold badges77 silver badges152 bronze badges
answered Apr 8, 2024 at 22:08
0

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.