1

I get error message when trying enter few layers to geopackage using PyQGIS 3.6.2.

This is my code:

from qgis.core import   QgsVectorFileWriter 
options = QgsVectorFileWriter.SaveVectorOptions()
options.actionOnExistingFile = QgsVectorFileWriter.CreateOrOverwriteLayer  
options.EditionCapability = QgsVectorFileWriter.CanAddNewLayer  
options.onlySelectedFeatures = True 
GpName = 'c:\\qgis_dat\\proj\\proj1' 

for layer in  iface.mapCanvas().layers() :  
    namee = layer.name()  # it can be 'roads' or 'buildings' ...
    options.layerName =  namee    
    _writer = QgsVectorFileWriter.writeAsVectorFormat(layer, GpName, options )
    print (_writer) 
    # print the error's message : 
    #    (2, 'Opening of data source in update mode failed (OGR error: )')  .
    # the geoPackage and layers are not created
2
  • 1
    @BERA, thanks. I already checked "namee" = "paved_road". it is ok, I understood it will be the name of the layer in geopackage. The error message I get when "print (_writer) "
    – Arie
    May 8, 2019 at 9:03
  • update your question then, and put the error in line print (_writer)
    – Fran Raga
    May 8, 2019 at 9:57

2 Answers 2

2

If the file does not exists the create file option has to be set:

    if not os.path.exists(geo_package): # if the ouput file doesn't already exist
        options.actionOnExistingFile = QgsVectorFileWriter.CreateOrOverwriteFile
    else:
        options.actionOnExistingFile = QgsVectorFileWriter.CreateOrOverwriteLayer
1
  • The above solution worked for me. Additional note, you can only have one .actionOnExistingFile setting; I had tried to also set options.actionOnExistingFile = QgsVectorFileWriter.CreateOrOverwriteLayer, and it overwrote the option (and failed to create a new Geopackage)
    – grego
    Aug 5, 2021 at 23:58
1

If you're using QGIS 3.x-3.16, use QgsVectorFileWriter.writeAsVectorFormatV2(). If you're using QGIS 3.20 or beyond, QgsVectorFileWriter.writeAsVectorFormatV3()

Here's how your code might look ( I cleaned it up a little) using .writeAsVectorFormatV2(). It's important that GpName be a complete path, not just a directory. One big difference is that the V2() function needs a context argument:

from qgis.core import QgsVectorFileWriter 
options = QgsVectorFileWriter.SaveVectorOptions()
options.actionOnExistingFile = QgsVectorFileWriter.CreateOrOverwriteLayer  
options.onlySelectedFeatures = True 
GpName = 'c:\\qgis_dat\\proj\\proj1\\test_01.gpkg' # full path here 

for layer in  iface.mapCanvas().layers() :  
    context = QgsProject.instance().transformContext()
    options.layerName = layer.name()  # it can be 'roads' or 'buildings' ...   
    _writer = QgsVectorFileWriter.writeAsVectorFormatV2(layer, GpName, context, options)
    print (_writer)
    if _writer[0] == QgsVectorFileWriter.NoError:
        print("Success!\n")
    else:
        print(_writer)

Good luck, hope that helps.

1
  • Thank you very much for help. I will check it
    – Arie
    Aug 12, 2021 at 6:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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