1

While running the following code:

from qgis.utils import iface
from qgis.core import QgsVectorFileWriter

canvas = iface.mapCanvas() layer = canvas.currentLayer() print(layer.name())

options = QgsVectorFileWriter.SaveVectorOptions() options.actionOnExistingFile = QgsVectorFileWriter.CreateOrOverwriteLayer

#options.layerName = "_".join(layer.name().split(' ')) writer = QgsVectorFileWriter.writeAsVectorFormat(layer, "C:/Users/ilyasse2.0/Documents/SQRLAND/hh.gpkg", options) print(writer)

I got this error:

(2, 'Opening of data source in update mode failed (OGR error: )')

The layer is created by with:

from qgis.core import QgsVectorLayer

layer = QgsVectorLayer("Point", 'layerName', "memory")

I tried to use this solution: Adding layers to GeoPackage using PyQGIS but it is not working for me.

Taras
  • 32,823
  • 4
  • 66
  • 137

2 Answers2

4

If you want to create a GeoPackage or if you already have a GeoPackage with layers in it and want to add another layer to it you can use this example where I create a memory layer with a process (extent of a layer) and then add it to the GeoPackage:

import processing
from os.path import abspath, isfile, join
from qgis.utils import iface
from qgis.core import QgsProject, QgsVectorFileWriter

path = abspath("path/to/layer") gpgk_name = 'test.gpkg' gpkg_path = join(path, gpgk_name) # your geopackage

polygon_layer = iface.activeLayer()

extent = processing.run("qgis:polygonfromlayerextent",{ 'INPUT': polygon_layer, 'ROUND_TO': 0, 'OUTPUT': 'TEMPORARY_OUTPUT' }) layer = extent['OUTPUT'] # your memory layer

context = QgsProject.instance().transformContext() options = QgsVectorFileWriter.SaveVectorOptions()

if isfile(gpkg_path): options.actionOnExistingFile = QgsVectorFileWriter.CreateOrOverwriteLayer options.layerName = layer.name() options.fileEncoding = layer.dataProvider().encoding() options.driverName = "GPKG"

QgsVectorFileWriter.writeAsVectorFormatV3(layer, gpkg_path, context, options)

At the begining I had this, a GeoPackage with three layers:

before the code

After creating the extent as a memory layer and add it to the GeoPackage I have this:

after the code

If you want to overwrite an existing GeoPackage you need to delete this condition:

from os.path import isfile

if isfile(gpkg_path): options.actionOnExistingFile = QgsVectorFileWriter.CreateOrOverwriteLayer

Taras
  • 32,823
  • 4
  • 66
  • 137
JULESG
  • 1,627
  • 3
  • 12
  • 1
    It seems like the QgsVectorFileWriter.writeAsVectorFormatV2() function is deprecated and gives a warning. However, i used the code above except i was using QgsVectorFileWriter.writeAsVectorFormatV3() which removes the deprecation warning - function arguments and result seem to be the same... – Valentin Aug 24 '23 at 11:23
  • @Valentin yes since version 3.20, I edited my response, previous version needs to use QgsVectorFileWriter.writeAsVectorFormatV2() – JULESG Aug 25 '23 at 13:01
0

These PyQGIS solutions seem very convoluted. You can just use GeoPandas to write a new layer to an existing GeoPackage like this:

import geopandas as gpd

gdf = gpd.read_file(#whatever your source is#) gpkg_file = 'name.gpkg' new_layer = 'newlayer' gdf.to_file(gpkg_file, layer=new_layer, driver="GPKG")

Taras
  • 32,823
  • 4
  • 66
  • 137
Encomium
  • 3,133
  • 2
  • 14
  • 41