4

I am getting the following error while saving my temporary layer.

Traceback (most recent call last):
  File "C:\PROGRA~1\QGIS31~1.14\apps\Python39\lib\code.py", line 90, in runcode
    exec(code, self.locals)
  File "<input>", line 1, in <module>
  File "<string>", line 22, in <module>
TypeError: QgsVectorFileWriter.writeAsVectorFormatV2(): argument 4 has unexpected type 'str'

This is the script I am using:

from qgis.core import QgsVectorFileWriter, QgsVectorLayer
import os
layer = iface.activeLayer() # layer which has a selection 
feat = layer.selectedFeatures()
fields = layer.fields()
crs = layer.crs().authid()
selection = QgsVectorLayer('Point?crs=' + crs, 'temp', 'memory')
dp = selection.dataProvider()
dp.addAttributes(fields)
selection.updateFields()
feats = layer.selectedFeatures()
for feat in feats:
    feature = QgsFeature(layer.fields())
    feature.setAttributes(feat.attributes())
    feature.setGeometry(feat.geometry())
    dp.addFeatures([feature])
    dp.updateExtents()
QgsProject.instance().addMapLayer(selection)
print(selection)
dg = QgsVectorFileWriter.writeAsVectorFormatV2(selection, r"D:\Destn.shp",QgsCoordinateTransformContext(),"ESRI Shapefile")
2
  • Welcome to Geographic Information Systems! Welcome to GIS SE! We're a little different from other sites; this isn't a discussion forum but a Q&A site. Your questions should as much as possible describe not just what you want to do, but precisely what you have tried and where you are stuck trying that. Please check out our short tour for more about how the site works
    – Ian Turton
    Jan 11 at 10:51
  • please do not use screen shots for code and text
    – Ian Turton
    Jan 11 at 10:51
5

You have to use like this:

QgsVectorFileWriter.writeAsVectorFormatV2(#layer#, #file_path#, #transform_context#, #options#)
file_path= "/Path/To/File.shp"    
transform_context = QgsCoordinateTransformContext() 
options = QgsVectorFileWriter.SaveVectorOptions()
options.driverName = "ESRI Shapefile"

QgsVectorFileWriter.writeAsVectorFormatV2(selection,
                                          file_path, 
                                          transform_context,
                                          options
                                          )


You can also use writeAsVectorFormat:

QgsVectorFileWriter.writeAsVectorFormat(#layer#, #file path#, #encoding#, #crs#, #driver name#)
QgsVectorFileWriter.writeAsVectorFormat(selection,       # layer
                                        r"D:\Destn.shp", # file path
                                        "UTF-8",         # encoding
                                        selection.crs(), # crs
                                        "ESRI Shapefile" # driver name
                                       )
2
  • 3
    Just a heads up, the question specifies writeAsVectorFormatV2(), which has different parameters to writeAsVectorFormat(). QgsVectorFileWriter.writeAsVectorFormatV2(layer, out_path, QgsCoordinateTransformContext(), options). Where options = QgsVectorFileWriter.SaveVectorOptions() ... options.driverName = 'ESRI Shapefile'
    – Matt
    Jan 11 at 17:32
  • Thanks Matt. I've added more explanation. Jan 12 at 18:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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