0

I have problems to save a vectorlayer at QGIS 3.0 with his original name in a GeoPackage.

canvas = qgis.utils.iface.mapCanvas()
layer = canvas.currentLayer()
writer = QgsVectorFileWriter.writeAsVectorFormat(layer, "V:/test_layer/test_gpkg", 'utf-8', layer.crs(), "GPKG")

With this code I create a Geopackage with the selected vectorlayer in it. The only Problem is that the saved vectorlayer has the same name as the GeoPackage.

Can somebody help me to save the vectorlayer in a different name than the GeoPackage and maybe how to add other vectorlayers?

1

This post: Add layers to Geopackage using pyqgis (QGIS 3) solves your problem but to be a pure answer to your question, try in this way:

canvas = qgis.utils.iface.mapCanvas()
layer = canvas.currentLayer()

options = QgsVectorFileWriter.SaveVectorOptions()
options.actionOnExistingFile = QgsVectorFileWriter.CreateOrOverwriteLayer    
# to get rid of spaces in the layer name
options.layerName = "_".join(layer.name().split(' '))

writer = QgsVectorFileWriter.writeAsVectorFormat(layer, "V:/test_layer/test_gpkg", options)

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.