8

Isn't there a save method for QgsVectorLayer created for example like this:

i_out = processing.run("native:buffer", 
    {'INPUT':'C:/GIS/data/testdata/ak_riks.shp',
    'DISTANCE':100000,'SEGMENTS':5,'END_CAP_STYLE':0,
    'JOIN_STYLE':0,'MITER_LIMIT':2,'DISSOLVE':True,
    'OUTPUT':'TEMPORARY_OUTPUT'})['OUTPUT']

print(type(i_out))
<class 'qgis._core.QgsVectorLayer'>

The way I save it now is:

processing.run("gdal:convertformat",{
    'INPUT':i_out,
    'OPTIONS':'',
    'OUTPUT':'C:/GIS/data/testdata/fdsfdsds2.shp'
    })

Does not seem to be a save method:

for m in dir(i_out):
   if m.startswith("save"):
      print(m)

saveDefaultMetadata
saveDefaultStyle
saveNamedMetadata
saveNamedStyle
saveSldStyle
saveStyleToDatabase
0

2 Answers 2

11

QgsVectorLayer and QgsVectorDataProvider classes don't have any "save as"-like method. Therefore, you have to use writeAsVectorFormatV2 method of QgsVectorFileWriter class in version 3.10.3+ (or writeAsVectorFormat in earlier 3.x version ).

Using writeAsVectorFormatV2:

i_out = processing.run("native:buffer", {.....})["OUTPUT"]

path = "/Path/To/File.shp"    
options = QgsVectorFileWriter.SaveVectorOptions()
options.driverName = "ESRI Shapefile"

QgsVectorFileWriter.writeAsVectorFormatV2(i_out, path, QgsCoordinateTransformContext(), options)
-1

I developed a plugin that saves the qgis project as .qgs and all vector layers as .gpkg while maintaining their original styles and formatting. Published it on the official plugin repo for all to use and modify as needed. Hope this helps! https://plugins.qgis.org/plugins/SaveAllScript/

2
  • 1
    This is not answering the question. The Q is about programming. Commented Jun 30, 2023 at 8:07
  • I would like to clarify that this comment is relevant to the question because I created a QGIS plug-in that addresses some of the questions that the original poster had. I am sharing it here because maybe people in the future who also share the same questions can easily fix their problem with the plug-in. People are welcome to reference the code for the plug-in in order to help them with their own programming goals. Commented Jun 30, 2023 at 23:15

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.