7

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" )

Taras
  • 32,823
  • 4
  • 66
  • 137

2 Answers2

7

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

tool

Taras
  • 32,823
  • 4
  • 66
  • 137
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")

Taras
  • 32,823
  • 4
  • 66
  • 137
Mayo
  • 3,902
  • 3
  • 22
  • 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! – Kate Blackwell Jun 20 '22 at 19:37
  • @KateBlackwell, I updated my answer. – Mayo Nov 16 '22 at 22:22