I'm trying to export QgsVectorLayer format into a csv with WKT?

I'm really have a hard time understanding the QGIS python documentation. I don't even see any option for Geometry. Even though I saw other people use it on stackexchange. https://api.qgis.org/api/classQgsVectorFileWriter_1_1SaveVectorOptions.html#ad98c015173618ad5a4729af18eeace82 Can anyone help me with the correct formatting and is this even possible.

The code works, it's just I never see the WKT in the csv, unless I do it manually from QGIS.

output_file_name = self.parameterDefinition('input_boundary').valueAsPythonString(parameters['input_boundary'], context).replace("'","")

boundary_layer = QgsVectorLayer(output_file_name,'boundary','ogr')        
#df = pd.read_csv(points_file, encoding='iso-8859-1')
options = QgsVectorFileWriter.SaveVectorOptions()
options.driverName = "csv"
options.fileEncoding = "utf-8"
#options.destCRS =
#options.ct =
boundary_csv_path = os.path.join(work_space_path,"test8.csv")

#QgsVectorFileWriter.writeAsVectorFormat(boundary_layer, boundary_csv_path, "utf-8", None, "CSV", layerOptions =['GEOMETRY=AS_XY'])

1 Answer 1


You can use this structure:

options = QgsVectorFileWriter.SaveVectorOptions()
options.driverName = "csv"
options.fileEncoding = "utf-8"
options.layerOptions = ['GEOMETRY=AS_WKT']

transform_context = QgsProject.instance().transformContext()

boundary_csv_path = os.path.join(work_space_path,"test8.csv")

You can add additional layer options within a list as follows:

options.layerOptions = ['GEOMETRY=AS_WKT',

enter image description here

  • Nice! Yeah that worked. Thank you. Like I see layerOptions in the documentation. But I don't see the options for GEOMETRY=AS_WKT Where do you see this in the documentation ?
    – AdamGIS
    Mar 9, 2023 at 3:43
  • Trial and error. I got errors and tried to solve them. Mar 9, 2023 at 13:54
  • What is the difference between using QgsProject.instance().transformContext() and QgsCoordinateTransformContext() in QgsVectorFileWriter.writeAsVectorFormatV3? If I want to export the new layer in the same CRS from input layer, is it ok to use only QgsCoordinateTransformContext()? Aug 8, 2023 at 20:55

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.