<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<p>Hi Jésahel,</p>
<p>A shoot in the dark: use writeAsVectorFormatV3 instead of
writeAsVectorFormatV2...</p>
<p>Regards,<br>
Benoit</p>
<p><br>
</p>
<div class="moz-cite-prefix">On 23/04/2021 16:42, Jésahel Benoist
wrote:<br>
</div>
<blockquote type="cite"
cite="mid:CAOSu6ZEMbExmVqX+G4WtfW5ACE6hdHoxZYPGnoT0GFxP69gWnw@mail.gmail.com">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<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"
moz-do-not-send="true">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](http://djes.free.fr/qgis/test.gpkg)"
moz-do-not-send="true">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"
moz-do-not-send="true">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>
<fieldset class="mimeAttachmentHeader"></fieldset>
<pre class="moz-quote-pre" wrap="">_______________________________________________
QGIS-Developer mailing list
<a class="moz-txt-link-abbreviated" href="mailto:QGIS-Developer@lists.osgeo.org">QGIS-Developer@lists.osgeo.org</a>
List info: <a class="moz-txt-link-freetext" href="https://lists.osgeo.org/mailman/listinfo/qgis-developer">https://lists.osgeo.org/mailman/listinfo/qgis-developer</a>
Unsubscribe: <a class="moz-txt-link-freetext" href="https://lists.osgeo.org/mailman/listinfo/qgis-developer">https://lists.osgeo.org/mailman/listinfo/qgis-developer</a>
</pre>
</blockquote>
</body>
</html>