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.

4
  • i have used this solution : gis.stackexchange.com/questions/285346/… but it's not working Commented Aug 3, 2021 at 16:29
  • You can do that by using the processing algorithm native:savefeatures. See this answer Commented Aug 3, 2021 at 18:01
  • i believe that it will only work for one layer i want to add multiple layers Commented Aug 3, 2021 at 20:39
  • Oh, that's new info that the question does not mention. Additionally, QgsVectorFileWriter.writeAsVectorFormat() also works for a single layer, but you have Python to iterate. Commented Aug 3, 2021 at 21:25

2 Answers 2

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
2
  • 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
    Commented Aug 24, 2023 at 11:23
  • @Valentin yes since version 3.20, I edited my response, previous version needs to use QgsVectorFileWriter.writeAsVectorFormatV2()
    – JULESG
    Commented Aug 25, 2023 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")

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.