Hi ! I'm happy to join this list. Hope you can help with this nasty bug, two days I'm on it...
Regards, Jésahel **Describe the bug** For a plugin, I need a way to save my layers in a geopackage with their own style, exactly like the "Package" Tool. The problem arises when two or more layers are saved. In the destination gpkg, the layer_styles table contains one row by layer, the styleName field is OK but the f_table_name field contains another layer name. I'm almost sure that the bug is in the saveStyleToDatabase() function. I've tried everything, almost reproducing the [Package tool function]( https://github.com/qgis/QGIS/blob/master/src/analysis/processing/qgsalgorithmpackage.cpp ) **How to Reproduce** Create two layers with their own style named test and test 2 (or download this one [ http://djes.free.fr/qgis/test.gpkg](http://djes.free.fr/qgis/test.gpkg)). Adjust the script to the layers name and destination geopackage. Launch the script for the first layer, destination file is OK. For the second, the style is not correctly saved. **QGIS and OS versions** 3.16.5 and 3.18.1 / Windows 10 Simplified code (or download it on http://djes.free.fr/qgis/saveLayerWithStyle.py) [code] layer_name = 'test' #change me gpkg_name = 'c:\\temp\\mytest.gpkg' #change me if needed # First, saves the layer src_layer = QgsProject.instance().mapLayersByName(layer_name)[0] options = QgsVectorFileWriter.SaveVectorOptions() options.actionOnExistingFile = QgsVectorFileWriter.CreateOrOverwriteLayer options.layerName = layer_name #options.SymbologyExport = QgsVectorFileWriter.FeatureSymbology #options.driverName = "GPKG" error = QgsVectorFileWriter.writeAsVectorFormatV2(src_layer, gpkg_name, QgsProject.instance().transformContext(), options) if error[0] == QgsVectorFileWriter.ErrCreateDataSource: print("Create mode") options.actionOnExistingFile = QgsVectorFileWriter.CreateOrOverwriteFile #Create mode error = QgsVectorFileWriter.writeAsVectorFormatV2(src_layer, gpkg_name, QgsProject.instance().transformContext(), options) del options if error[0] == QgsVectorFileWriter.NoError: print('Layer "' + layer_na me + '" saved in "' + gpkg_name + '"') else: print(error) raise Exception('Failed to save layer') # Second, saves the style # Load just saved layer dst_layer = QgsVectorLayer(gpkg_name, layer_name) if not dst_layer.isValid(): raise Exception('Failed to load layer') #print(dst_layer) myDocument = QDomDocument('qgis') src_layer.exportNamedStyle(myDocument) #print(myDocument.toString()) success, message = dst_layer.importNamedStyle(myDocument) dst_layer.saveStyleToDatabase(layer_name, '', True, '') QgsProject.instance().removeMapLayer(dst_layer) del src_layer del dst_layer del myDocument [/code]
_______________________________________________ QGIS-Developer mailing list [email protected] List info: https://lists.osgeo.org/mailman/listinfo/qgis-developer Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-developer
