8

I have dozens of temporary Layers. I'd like to export them all as GeoPackages, but I am unsure how to go about doing so without doing each one individually.

I was reviewing this code Exporting several files at same time in QGIS, but it exports files as .shp.

I have tried changing .shp to .gkpg and ESRI Shapefile to GeoPackage, but the code generated errors.

I did verify, however, that I can get the original code to run:

myDir = 'C:/temp/'

for vLayer in iface.mapCanvas().layers():
    QgsVectorFileWriter.writeAsVectorFormat( vLayer, 
        myDir + vLayer.name() + ".shp", "utf-8", 
        vLayer.crs(), "ESRI Shapefile" )
1

3 Answers 3

9

Another solution is to use the "Package layers" tool as a Batch Process.

tool

3

Try the Package layers tool, and you can export the styles and metadata.

The problem in your code is the name of the OGR driver. You must set the OGR driver to GPKG instead of GeoPackage.

myDir = ''

for vLayer in iface.mapCanvas().layers():
    QgsVectorFileWriter.writeAsVectorFormat(vLayer, 
        myDir + vLayer.name() + ".gpkg",
        "utf-8", 
        vLayer.crs(),
        "GPKG")
2
  • Thank you for the suggestion. It looks like this may work as a workaround, but I would still prefer to find a solution in PyQGIS form as it will be quicker and easier for naming purposes. Much obliged if you have any suggestions in that form! Commented Jun 20, 2022 at 19:37
  • @KateBlackwell, I updated my answer.
    – Mayo
    Commented Nov 16, 2022 at 22:22
0

enter image description here

There's a 'Save vector features to file' tool and in my case the expression that I used to batch name the outputs was this:

 file_path(  @INPUT  ) || '\\' ||  left(  base_file_name( @INPUT ),11) || 'RF02.gpkg.' 

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