3

I am trying to write two layers into geopackage without success.

from qgis.PyQt.QtCore import *

def drawLay ( namee, x1, y1 ) :
    lay = QgsVectorLayer('Point?crs=epsg:2039', namee , 'memory')
    pr = lay.dataProvider()
    lay.startEditing()
    fet = QgsFeature()
    fet.setGeometry( QgsGeometry.fromPointXY(QgsPointXY(x1,y1)) )
    pr.addFeatures( [ fet ] )
    lay.commitChanges()
    lay.updateExtents()
    QgsProject.instance().addMapLayer(lay)

# create two layers    
drawLay ( 'aa', 150000, 400000)
drawLay ( 'bb', 150100, 400000)

#write this layers into geoPackage 
GpName = r'c:\temp\gpk_file.gpkg'
options = QgsVectorFileWriter.SaveVectorOptions()
options.actionOnExistingFile = QgsVectorFileWriter.CreateOrOverwriteLayer 
options.EditionCapability = QgsVectorFileWriter.CanAddNewLayer  
for layer in  iface.mapCanvas().layers() :  
    namee =layer.name()
    print (namee)
    options.layerName = namee  
    _writer = QgsVectorFileWriter.writeAsVectorFormat(layer, GpName, options )
  • 1
    what doesn't work? what does happen? – Ian Turton May 19 at 14:15
  • Your code work perfectly, print _writer for show a error, maybe you don't have administrator permission for write on C – Fran Raga May 19 at 15:57
  • Tested the code on QGIS3 on LINUX @FranRaga it showed a (2, 'Opening of data source in update mode failed (OGR error: )') – Riccardo May 19 at 17:52
  • On linux but your path is a windows format!you need admin permisions I think for write a geopackage – Fran Raga May 19 at 19:12
  • Thanks to all. @IanTurton- I am trying to write more than one layer to geopackage allready a week without success. – Arie May 20 at 6:55
2

To solve this problem you have to check that the file doesn't exist. Depending on whether it is a new file or an update, you must change the value of actionOnExistingFile

Another change you must make is iface.mapCanvas(). Layers(), and use QgsProject.instance ().MapLayers ().Values()

Add code :

from qgis.PyQt.QtCore import *

def drawLay ( namee, x1, y1 ) :
    lay = QgsVectorLayer('Point?crs=epsg:2039', namee , 'memory')
    pr = lay.dataProvider()
    lay.startEditing()
    fet = QgsFeature()
    fet.setGeometry( QgsGeometry.fromPointXY(QgsPointXY(x1,y1)) )
    pr.addFeatures( [ fet ] )
    lay.commitChanges()
    lay.updateExtents()
    QgsProject.instance().addMapLayer(lay)

# create two layers    
drawLay ( 'aa', 150000, 400000)
drawLay ( 'bb', 150100, 400000)

#write this layers into geoPackage 
GpName = r'D:\temp\gpk_file.gpkg'
for layer in  QgsProject.instance().mapLayers().values() :  
    options = QgsVectorFileWriter.SaveVectorOptions() 
    options.actionOnExistingFile = QgsVectorFileWriter.CreateOrOverwriteLayer #Update mode
    options.EditionCapability = QgsVectorFileWriter.CanAddNewLayer 
    options.layerName = layer.name()  
    print("Update mode")
    _writer = QgsVectorFileWriter.writeAsVectorFormat(layer, GpName, options)
    if _writer:
            print(layer.name(), _writer)
    if _writer[0] == QgsVectorFileWriter.ErrCreateDataSource :
        print("Create mode")
        options.actionOnExistingFile = QgsVectorFileWriter.CreateOrOverwriteFile #Create mode
        _writer= QgsVectorFileWriter.writeAsVectorFormat(layer, GpName, options)
        if _writer:
                print(layer.name(), _writer)

tested on QGIS 3.6.2 and win 10

  • thanks, I saw your answer after I had write my "discovery". – Arie May 20 at 8:59
2

I found it! Thanks to hint in this post Deleting vector layer from Geopackage using PyQGIS?
I found that there is difference for entering first layer to geopackage and for adding next layers. Here is my code:

GpName = r'c:\temp\gpk_file.gpkg'
firstt = True
options = QgsVectorFileWriter.SaveVectorOptions() 
for layer in  QgsProject.instance().mapLayers().values() :  
    if firstt :            
        firstt = False
    else :
        options.actionOnExistingFile = QgsVectorFileWriter.CreateOrOverwriteLayer 
        options.EditionCapability = QgsVectorFileWriter.CanAddNewLayer  
    options.layerName = layer.name()
    _writer = QgsVectorFileWriter.writeAsVectorFormat(layer, GpName, options )
  • this resolvs "Opening of data source in update mode failed (OGR error: )" – Enrico Ferreguti Jun 7 at 14:54

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.