[QGIS-Developer] QGIS project maintainability, forms and styles, xml verbosity
John Stevenson - BGS
jostev at bgs.ac.uk
Tue Aug 20 04:22:09 PDT 2024
We have a QGIS plugin that we use locally that stores layer styles as .qml files. We also had the issues with the order of the fields and sections in the XML changing. This meant that a diff between two files was useless.
The code below uses `canonicalize` to fix the ordering issue, and now we can see the actual changes between files in our GitLab repo.
```python
from pathlib import Path
from xml.etree.ElementTree import canonicalize
# Save the style of the current layer
layer_style_path = Path(‘my_layer_style.qml’)
layer = QgsProject.instance().mapLayersByName(table_name)[0]
layer.saveNamedStyle(
str(layer_style_path),
categories=QgsMapLayer.Symbology | QgsMapLayer.Labeling | QgsMapLayer.Fields | QgsMapLayer.Forms | QgsMapLayer.MapTips
)
# Read the newly created XML file
raw_xml = layer_style_path.read_text()
# Canonicalize the XML data and save back to the same file
# Set the newline manually to force LF line endings
with open(layer_style_path, "w", newline="\n") as qml_file:
canonicalize(xml_data=raw_xml, out=qml_file)
```
Cheers
John
From: QGIS-Developer <qgis-developer-bounces at lists.osgeo.org> On Behalf Of Emma Hain via QGIS-Developer
Sent: Tuesday, August 20, 2024 2:48 AM
To: Thomas Larsen Wessel <mrvelle at gmail.com>
Cc: qgis-developer at lists.osgeo.org
Subject: Re: [QGIS-Developer] QGIS project maintainability, forms and styles, xml verbosity
Thanks Thomas
I have put that into the notes for us to prepare if it is submitted into a grant.
Let us know if there are any more aspects to add,
Cheers
Em
On Tue, 20 Aug 2024 at 08:35, Thomas Larsen Wessel via QGIS-Developer <qgis-developer at lists.osgeo.org<mailto:qgis-developer at lists.osgeo.org>> wrote:
I forgot an important detail, that explains why it gets intolerable for me. The qgis project I maintain has several layers that are close to identical, but covering different municipalities. So when I change a form, I need to make that change to several layers (each having several styles).
_______________________________________________
QGIS-Developer mailing list
QGIS-Developer at lists.osgeo.org<mailto: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
--
Emma Hain — Product Manager/Senior GIS Analyst
emma at north-road.com<mailto:emma at north-road.com>
[https://ci3.googleusercontent.com/mail-sig/AIorK4z-QyPD9kQRyEtpg0YHp_bVoq466HfPMkJXDWxnwTf1qCIBS8c19W29po6b7akLq2btEedEMKg]
North Road
Cartography • Development • Spatial Analysis
________________________________
north-road.com<http://north-road.com>
[https://ci3.googleusercontent.com/mail-sig/AIorK4xbsQ72BNIM2cWMZcuEKVer1Bwaq_gqYkdbtrjK2ifrqv-4o7aWjcvilgYXtNGpXLNgD00xgmQ]<https://twitter.com/northroadgeo>[https://ci3.googleusercontent.com/mail-sig/AIorK4zEBWGJKio8gqmFT0h_51t4ZnmoGgbmV7SeL-4FKvwWApFWDWyK7p-c0-cSskUe7pjQzjEZj9Q]<https://www.linkedin.com/company/north-road-studios>[https://ci3.googleusercontent.com/mail-sig/AIorK4wlMciFPm8hqQZB_Qi57PUCv1dJqUC1A2i9qEMzWRFLcJ7Jil30YnULdJLxT6-qMfI6J4kcac4]<https://www.facebook.com/North-Road-997236690392419/home>
This email and any attachments are intended solely for the use of the named recipients. If you are not the intended recipient you must not use, disclose, copy or distribute this email or any of its attachments and should notify the sender immediately and delete this email from your system. UK Research and Innovation (UKRI) has taken every reasonable precaution to minimise risk of this email or any attachments containing viruses or malware but the recipient should carry out its own virus and malware checks before opening the attachments. UKRI does not accept any liability for any losses or damages which the recipient may sustain due to presence of any viruses.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/qgis-developer/attachments/20240820/044415c1/attachment-0001.htm>
More information about the QGIS-Developer
mailing list