<div dir="ltr"><div>Hi Jésahel </div>It is ok to share and verify bugs in the dev mailing list. BTW if it is almost sure you have a bug, IMHO it is better to open an issue where to store any info to replicate it.<br clear="all"><div><div dir="ltr" class="gmail_signature" data-smartmail="gmail_signature"><div dir="ltr"><div><div dir="ltr"><div><div dir="ltr"><div><div dir="ltr"><div><br></div><div>Luigi Pirelli<br><br>**************************************************************************************************<br>* LinkedIn: <a href="https://www.linkedin.com/in/luigipirelli" target="_blank">https://www.linkedin.com/in/luigipirelli</a><br>* Stackexchange: <a href="http://gis.stackexchange.com/users/19667/luigi-pirelli" target="_blank">http://gis.stackexchange.com/users/19667/luigi-pirelli</a><br>* GitHub: <a href="https://github.com/luipir" target="_blank">https://github.com/luipir</a><br>* Book: <a href="https://www.packtpub.com/eu/application-development/mastering-geospatial-development-qgis-3x-third-edition" target="_blank">Mastering QGIS3 - 3rd Edition</a></div><div>* Hire a team: <a href="http://www.qcooperative.net" target="_blank">http://www.qcooperative.net</a><br>**************************************************************************************************</div></div></div></div></div></div></div></div></div></div><br></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Fri, 23 Apr 2021 at 16:42, Jésahel Benoist <<a href="mailto:djes1975@gmail.com">djes1975@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div>Hi ! I'm happy to join this list. Hope you can help with this nasty bug, two days I'm on it...</div><div><br></div><div>Regards,</div><div><br></div><div>Jésahel</div><div> </div><div>**Describe the bug**<br>For a plugin, I need a way to save my layers in a geopackage with their own style, exactly like the "Package" Tool.<br>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](<a href="https://github.com/qgis/QGIS/blob/master/src/analysis/processing/qgsalgorithmpackage.cpp" target="_blank">https://github.com/qgis/QGIS/blob/master/src/analysis/processing/qgsalgorithmpackage.cpp</a>)<br><br>**How to Reproduce**<br>Create two layers with their own style named test and test 2 (or download this one [<a href="http://djes.free.fr/qgis/test.gpkg%5D(http://djes.free.fr/qgis/test.gpkg)" target="_blank">http://djes.free.fr/qgis/test.gpkg](http://djes.free.fr/qgis/test.gpkg)</a>). 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.<br><br>**QGIS and OS versions**<br>3.16.5 and 3.18.1 / Windows 10  <br></div><div><br></div>Simplified code (or download it on <a href="http://djes.free.fr/qgis/saveLayerWithStyle.py" target="_blank">http://djes.free.fr/qgis/saveLayerWithStyle.py</a>)<br>[code]<br>layer_name = 'test' #change me<br>gpkg_name = 'c:\\temp\\mytest.gpkg' #change me if needed<div><br># First, saves the layer</div><div>src_layer = QgsProject.instance().mapLayersByName(layer_name)[0]<br>options = QgsVectorFileWriter.SaveVectorOptions()<br>options.actionOnExistingFile = QgsVectorFileWriter.CreateOrOverwriteLayer<br>options.layerName = layer_name<br>#options.SymbologyExport = QgsVectorFileWriter.FeatureSymbology<br>#options.driverName = "GPKG"<br>error = QgsVectorFileWriter.writeAsVectorFormatV2(src_layer, gpkg_name, QgsProject.instance().transformContext(), options)<br><br>if error[0] == QgsVectorFileWriter.ErrCreateDataSource:<br>    print("Create mode")<br>    options.actionOnExistingFile = QgsVectorFileWriter.CreateOrOverwriteFile #Create mode<br>    error = QgsVectorFileWriter.writeAsVectorFormatV2(src_layer, gpkg_name, QgsProject.instance().transformContext(), options)<br>del options<br>if error[0] == QgsVectorFileWriter.NoError:<br>    print('Layer "' + layer_na<br><br>me + '" saved in "' + gpkg_name + '"')        <br>else:<br>    print(error)<br>    raise Exception('Failed to save layer')<br><br># Second, saves the style</div><div># Load just saved layer<br></div><div>dst_layer = QgsVectorLayer(gpkg_name, layer_name)<br>if not dst_layer.isValid():<br>    raise Exception('Failed to load layer')<br>#print(dst_layer)<br><br>myDocument = QDomDocument('qgis')<br>src_layer.exportNamedStyle(myDocument)<br>#print(myDocument.toString())<br>success, message = dst_layer.importNamedStyle(myDocument)<br>dst_layer.saveStyleToDatabase(layer_name, '', True, '')<br>QgsProject.instance().removeMapLayer(dst_layer)</div><div>del src_layer<br>del dst_layer<br>del myDocument<br>[/code]<div><br><br></div></div></div>
_______________________________________________<br>
QGIS-Developer mailing list<br>
<a href="mailto:QGIS-Developer@lists.osgeo.org" target="_blank">QGIS-Developer@lists.osgeo.org</a><br>
List info: <a href="https://lists.osgeo.org/mailman/listinfo/qgis-developer" rel="noreferrer" target="_blank">https://lists.osgeo.org/mailman/listinfo/qgis-developer</a><br>
Unsubscribe: <a href="https://lists.osgeo.org/mailman/listinfo/qgis-developer" rel="noreferrer" target="_blank">https://lists.osgeo.org/mailman/listinfo/qgis-developer</a><br>
</blockquote></div>