3

After running processins in a loop, I try to export results on a single shapefile. I've created a vector layer called "regroupee", I merge results with it, and I overwrite "regroupee" with the result.

As Kadir Şahbaz said in this post on the option 8, run() method with memory output make a dictionnary, with an instance of a layer (QgsVectorLayer).

The loop works, but when I try to export the final result with QgsVectorFileWriter.writeAsVectorFormat, an error say me that "regroupee" is a string...

My code, in the end of the for loop :

        alg_params = {
        'CRS': None,
        'LAYERS': [regroupee, calculatrice['OUTPUT']],
        'OUTPUT': 'memory:merge'
        }

        merge = processing.run('native:mergevectorlayers', alg_params, context=context, is_child_algorithm=True)
        regroupee = merge['OUTPUT']

    QgsVectorFileWriter.writeAsVectorFormat(regroupee, "I:/moulin_z/DONNEES/temp_us235/test_iteration_pyqgis/regroupee.shp", "UTF-8", crs.createFromId(2154), "ESRI Shapefile")

And the error message :

Traceback (most recent call last):
File "<string>", line 231, in processAlgorithm
TypeError: QgsVectorFileWriter.writeAsVectorFormat(): arguments did not 
match any overloaded call:
overload 1: argument 1 has unexpected type 'str'
overload 2: argument 1 has unexpected type 'str'
overload 3: argument 1 has unexpected type 'str'

How can I export the final result as a shapefile ?

1 Answer 1

1

The error in this case is because, you don't have a QgsVectorLayer inside the ['OUTPUT'].

First, you have to verify the regrouppe have a QgsVectorLayer in the ['OUTPUT'] with:

print(merge['OUTPUT'])

Then when you verify that you have QGsVectorLayer, you can try with this line:

QgsVectorFileWriter.writeAsVectorFormat(regroupee, "I:/moulin_z/DONNEES/temp_us235/test_iteration_pyqgis/regroupee.shp", "UTF-8", QgsCoordinateReferenceSystem(2154, QgsCoordinateReferenceSystem.EpsgCrsId), "ESRI Shapefile")

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.