<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="Generator" content="Microsoft Word 15 (filtered medium)">
<!--[if !mso]><style>v\:* {behavior:url(#default#VML);}
o\:* {behavior:url(#default#VML);}
w\:* {behavior:url(#default#VML);}
.shape {behavior:url(#default#VML);}
</style><![endif]--><style><!--
/* Font Definitions */
@font-face
{font-family:"Cambria Math";
panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
{font-family:Calibri;
panose-1:2 15 5 2 2 2 4 3 2 4;}
@font-face
{font-family:Verdana;
panose-1:2 11 6 4 3 5 4 4 2 4;}
@font-face
{font-family:Aptos;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
{margin:0cm;
font-size:12.0pt;
font-family:"Aptos",sans-serif;}
a:link, span.MsoHyperlink
{mso-style-priority:99;
color:blue;
text-decoration:underline;}
span.gmailsignatureprefix
{mso-style-name:gmail_signature_prefix;}
span.EmailStyle19
{mso-style-type:personal-reply;
font-family:"Arial",sans-serif;
color:windowtext;}
.MsoChpDefault
{mso-style-type:export-only;
font-size:11.0pt;
mso-fareast-language:EN-US;}
@page WordSection1
{size:612.0pt 792.0pt;
margin:72.0pt 72.0pt 72.0pt 72.0pt;}
div.WordSection1
{page:WordSection1;}
--></style><!--[if gte mso 9]><xml>
<o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext="edit">
<o:idmap v:ext="edit" data="1" />
</o:shapelayout></xml><![endif]-->
</head>
<body lang="EN-GB" link="blue" vlink="purple" style="word-wrap:break-word">
<div class="WordSection1">
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Arial",sans-serif;mso-fareast-language:EN-US">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.<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Arial",sans-serif;mso-fareast-language:EN-US"><o:p> </o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Arial",sans-serif;mso-fareast-language:EN-US">The code below uses `canonicalize` to fix the ordering issue, and now we can see the actual changes between files in our GitLab repo.<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Arial",sans-serif;mso-fareast-language:EN-US"><br>
```python<br>
from pathlib import Path<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Arial",sans-serif;mso-fareast-language:EN-US">from xml.etree.ElementTree import canonicalize<br>
<br>
<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Arial",sans-serif;mso-fareast-language:EN-US"># Save the style of the current layer<br>
layer_style_path = Path(‘my_layer_style.qml’)<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Arial",sans-serif;mso-fareast-language:EN-US">layer = QgsProject.instance().mapLayersByName(table_name)[0]<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Arial",sans-serif;mso-fareast-language:EN-US">layer.saveNamedStyle(<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Arial",sans-serif;mso-fareast-language:EN-US"> str(layer_style_path),<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Arial",sans-serif;mso-fareast-language:EN-US"> categories=QgsMapLayer.Symbology | QgsMapLayer.Labeling | QgsMapLayer.Fields | QgsMapLayer.Forms | QgsMapLayer.MapTips<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Arial",sans-serif;mso-fareast-language:EN-US"> )<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Arial",sans-serif;mso-fareast-language:EN-US"><o:p> </o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Arial",sans-serif;mso-fareast-language:EN-US"># Read the newly created XML file<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Arial",sans-serif;mso-fareast-language:EN-US">raw_xml = layer_style_path.read_text()<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Arial",sans-serif;mso-fareast-language:EN-US"># Canonicalize the XML data and save back to the same file<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Arial",sans-serif;mso-fareast-language:EN-US"># Set the newline manually to force LF line endings<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Arial",sans-serif;mso-fareast-language:EN-US">with open(layer_style_path, "w", newline="\n") as qml_file:<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Arial",sans-serif;mso-fareast-language:EN-US"> canonicalize(xml_data=raw_xml, out=qml_file)<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Arial",sans-serif;mso-fareast-language:EN-US"><br>
```<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Arial",sans-serif;mso-fareast-language:EN-US"><br>
Cheers<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Arial",sans-serif;mso-fareast-language:EN-US">John<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Arial",sans-serif;mso-fareast-language:EN-US"><o:p> </o:p></span></p>
<div style="border:none;border-top:solid #E1E1E1 1.0pt;padding:3.0pt 0cm 0cm 0cm">
<p class="MsoNormal"><b><span lang="EN-US" style="font-size:11.0pt;font-family:"Calibri",sans-serif">From:</span></b><span lang="EN-US" style="font-size:11.0pt;font-family:"Calibri",sans-serif"> QGIS-Developer <qgis-developer-bounces@lists.osgeo.org>
<b>On Behalf Of </b>Emma Hain via QGIS-Developer<br>
<b>Sent:</b> Tuesday, August 20, 2024 2:48 AM<br>
<b>To:</b> Thomas Larsen Wessel <mrvelle@gmail.com><br>
<b>Cc:</b> qgis-developer@lists.osgeo.org<br>
<b>Subject:</b> Re: [QGIS-Developer] QGIS project maintainability, forms and styles, xml verbosity<o:p></o:p></span></p>
</div>
<p class="MsoNormal"><o:p> </o:p></p>
<div>
<div>
<p class="MsoNormal">Thanks Thomas<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal">I have put that into the notes for us to prepare if it is submitted into a grant.<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">Let us know if there are any more aspects to add,<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">Cheers<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal">Em<o:p></o:p></p>
</div>
</div>
<p class="MsoNormal"><o:p> </o:p></p>
<div>
<div>
<p class="MsoNormal">On Tue, 20 Aug 2024 at 08:35, Thomas Larsen Wessel via QGIS-Developer <<a href="mailto:qgis-developer@lists.osgeo.org">qgis-developer@lists.osgeo.org</a>> wrote:<o:p></o:p></p>
</div>
<blockquote style="border:none;border-left:solid #CCCCCC 1.0pt;padding:0cm 0cm 0cm 6.0pt;margin-left:4.8pt;margin-right:0cm">
<div>
<p class="MsoNormal">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). <o:p></o:p></p>
</div>
<p class="MsoNormal">_______________________________________________<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" target="_blank">
https://lists.osgeo.org/mailman/listinfo/qgis-developer</a><br>
Unsubscribe: <a href="https://lists.osgeo.org/mailman/listinfo/qgis-developer" target="_blank">
https://lists.osgeo.org/mailman/listinfo/qgis-developer</a><o:p></o:p></p>
</blockquote>
</div>
<p class="MsoNormal"><br clear="all">
<br>
<span class="gmailsignatureprefix">-- </span><o:p></o:p></p>
<div>
<div>
<div style="margin-bottom:15.0pt">
<p class="MsoNormal"><span style="font-family:"Verdana",sans-serif;color:#444444">Emma Hain — Product Manager/Senior GIS Analyst<br>
<a href="mailto:emma@north-road.com" target="_blank"><span style="color:#4E8DAF;text-decoration:none">emma@north-road.com</span></a>
<o:p></o:p></span></p>
</div>
<div>
<div>
<div>
<div>
<div>
<div>
<div>
<div>
<div>
<div>
<div>
<div>
<div>
<table class="MsoNormalTable" border="0" cellpadding="0">
<tbody>
<tr>
<td style="padding:.75pt .75pt .75pt .75pt">
<p class="MsoNormal"><img border="0" width="104" height="100" style="width:1.0833in;height:1.0416in" id="_x0000_i1030" src="https://ci3.googleusercontent.com/mail-sig/AIorK4z-QyPD9kQRyEtpg0YHp_bVoq466HfPMkJXDWxnwTf1qCIBS8c19W29po6b7akLq2btEedEMKg"><o:p></o:p></p>
</td>
<td style="padding:.75pt .75pt .75pt .75pt">
<p class="MsoNormal"><b><span style="font-family:"Verdana",sans-serif;color:#444444">North Road</span></b><span style="font-family:"Verdana",sans-serif;color:#444444"><br>
Cartography • Development • Spatial Analysis</span><span style="color:#444444"><o:p></o:p></span></p>
<div class="MsoNormal" align="center" style="text-align:center"><span style="color:#444444">
<hr size="1" width="100%" noshade="" style="color:#DBD28F" align="center">
</span></div>
<p class="MsoNormal" style="margin-bottom:12.0pt"><span style="font-family:"Verdana",sans-serif;color:#444444"><a href="http://north-road.com" target="_blank"><b><span style="color:#4E8DAF;text-decoration:none">north-road.com</span></b></a><br>
</span><a href="https://twitter.com/northroadgeo" target="_blank"><span style="font-family:"Verdana",sans-serif;text-decoration:none"><img border="0" width="25" height="26" style="width:.2604in;height:.2708in" id="_x0000_i1029" src="https://ci3.googleusercontent.com/mail-sig/AIorK4xbsQ72BNIM2cWMZcuEKVer1Bwaq_gqYkdbtrjK2ifrqv-4o7aWjcvilgYXtNGpXLNgD00xgmQ"></span></a><a href="https://www.linkedin.com/company/north-road-studios" target="_blank"><span style="text-decoration:none"><img border="0" width="25" height="26" style="width:.2604in;height:.2708in" id="_x0000_i1028" src="https://ci3.googleusercontent.com/mail-sig/AIorK4zEBWGJKio8gqmFT0h_51t4ZnmoGgbmV7SeL-4FKvwWApFWDWyK7p-c0-cSskUe7pjQzjEZj9Q"></span></a><a href="https://www.facebook.com/North-Road-997236690392419/home" target="_blank"><span style="text-decoration:none"><img border="0" width="24" height="26" style="width:.25in;height:.2708in" id="_x0000_i1027" src="https://ci3.googleusercontent.com/mail-sig/AIorK4wlMciFPm8hqQZB_Qi57PUCv1dJqUC1A2i9qEMzWRFLcJ7Jil30YnULdJLxT6-qMfI6J4kcac4"></span></a><o:p></o:p></p>
</td>
</tr>
</tbody>
</table>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<br>
<br>
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.<br>
<br>
</body>
</html>