[QGIS-Developer] [python] saveStyleToDatabase() bug when saving more than one layer to a GPKG

Luigi Pirelli luipir at gmail.com
Sat Apr 24 13:39:10 PDT 2021


Hi Jésahel
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.

Luigi Pirelli

**************************************************************************************************
* LinkedIn: https://www.linkedin.com/in/luigipirelli
* Stackexchange: http://gis.stackexchange.com/users/19667/luigi-pirelli
* GitHub: https://github.com/luipir
* Book: Mastering QGIS3 - 3rd Edition
<https://www.packtpub.com/eu/application-development/mastering-geospatial-development-qgis-3x-third-edition>
* Hire a team: http://www.qcooperative.net
**************************************************************************************************


On Fri, 23 Apr 2021 at 16:42, Jésahel Benoist <djes1975 at gmail.com> wrote:

> 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
> QGIS-Developer at lists.osgeo.org
> List info: https://lists.osgeo.org/mailman/listinfo/qgis-developer
> Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-developer
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/qgis-developer/attachments/20210424/97944c35/attachment.html>


More information about the QGIS-Developer mailing list