0

I'm new to 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

3
  • 1
    Where did you get the code you have?
    – til_b
    Commented Apr 8 at 15:01
  • I used split-vector on QGIS and under advanced copied the python command code, I then changed a few thing, such as changing the file directory and which map layer, I had done something similar for rasterizing
    – Bee
    Commented Apr 8 at 15:30
  • 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.
    – Matt
    Commented Apr 8 at 16:01

1 Answer 1

0

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.

1
  • Thanks! I will give this a try today and update!
    – Bee
    Commented Apr 9 at 7:49

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.