<div dir="ltr">The code from Jorge does the job for ALL layers in the current project, can you describe your issue Peter ?<br></div><br><div class="gmail_quote gmail_quote_container"><div dir="ltr" class="gmail_attr">Le mar. 8 avr. 2025 à 17:51, peter.berger--- via QGIS-User <<a href="mailto:qgis-user@lists.osgeo.org">qgis-user@lists.osgeo.org</a>> a écrit :<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="auto">Hi Jorge<div><br></div><div>For me it is too minimal with this code alone. Would it be possible to describe how to run it when I have 65 Layers with styles and like to bring it all with styles to the postgres DB?</div><div><br></div><div>Many thanks in advance.</div><div><br></div><div>Peter</div><div><br></div><div><br id="m_-1079566373167674141lineBreakAtBeginningOfSignature"><div dir="ltr">Von meinem iPhone gesendet</div><div dir="ltr"><br><blockquote type="cite">Am 08.04.2025 um 10:59 schrieb Jorge Gustavo Rocha via QGIS-User <<a href="mailto:qgis-user@lists.osgeo.org" target="_blank">qgis-user@lists.osgeo.org</a>>:<br><br></blockquote></div><blockquote type="cite"><div dir="ltr">
<p>Hi,</p>
<p>This is the code I use (since some years ago...) to write styles
from all layers to the database:</p>
<div style="color:rgb(59,59,59);background-color:rgb(255,255,255);font-family:"Droid Sans Mono","monospace",monospace;font-weight:normal;font-size:12px;line-height:16px;white-space:pre-wrap"><div><span style="color:rgb(0,0,0)">mapGeometryType = {</span></div><div><span style="color:rgb(0,0,0)"> </span><span style="color:rgb(9,134,88)">0</span><span style="color:rgb(0,0,0)">: </span><span style="color:rgb(163,21,21)">"Point"</span><span style="color:rgb(0,0,0)">,</span></div><div><span style="color:rgb(0,0,0)"> </span><span style="color:rgb(9,134,88)">1</span><span style="color:rgb(0,0,0)">: </span><span style="color:rgb(163,21,21)">"Line"</span><span style="color:rgb(0,0,0)">,</span></div><div><span style="color:rgb(0,0,0)"> </span><span style="color:rgb(9,134,88)">2</span><span style="color:rgb(0,0,0)">: </span><span style="color:rgb(163,21,21)">"Polygon"</span><span style="color:rgb(0,0,0)">,</span></div><div><span style="color:rgb(0,0,0)"> </span><span style="color:rgb(9,134,88)">3</span><span style="color:rgb(0,0,0)">: </span><span style="color:rgb(163,21,21)">"UnknownGeometry"</span><span style="color:rgb(0,0,0)">,</span></div><div><span style="color:rgb(0,0,0)"> </span><span style="color:rgb(9,134,88)">4</span><span style="color:rgb(0,0,0)">: </span><span style="color:rgb(163,21,21)">"NullGeometry"</span><span style="color:rgb(0,0,0)">,</span></div><div><span style="color:rgb(0,0,0)">}</span></div>
<div><span style="color:rgb(0,0,0)">layers = QgsProject.instance().mapLayers()</span></div>
<div><span style="color:rgb(175,0,219)">for</span><span style="color:rgb(0,0,0)"> layer </span><span style="color:rgb(175,0,219)">in</span><span style="color:rgb(0,0,0)"> layers.values():</span></div><div><span style="color:rgb(0,0,0)"> </span><span style="color:rgb(175,0,219)">if</span><span style="color:rgb(0,0,0)"> layer.type() == QgsMapLayer.VectorLayer:</span></div><div><span style="color:rgb(0,0,0)"> </span><span style="color:rgb(175,0,219)">if</span><span style="color:rgb(0,0,0)"> mapGeometryType[layer.geometryType()] != </span><span style="color:rgb(163,21,21)">"NullGeometry"</span><span style="color:rgb(0,0,0)">:</span></div><div><span style="color:rgb(0,0,0)"> layer.deleteStyleFromDatabase</span></div><div><span style="color:rgb(0,0,0)"> layer.saveStyleToDatabase(</span><span style="color:rgb(0,16,128)">name</span><span style="color:rgb(0,0,0)">=<a href="http://layer.name" target="_blank">layer.name</a>(),</span><span style="color:rgb(0,16,128)">description</span><span style="color:rgb(0,0,0)">=</span><span style="color:rgb(163,21,21)">"Default style for </span><span style="color:rgb(0,0,255)">{}</span><span style="color:rgb(163,21,21)">"</span><span style="color:rgb(0,0,0)">.format(<a href="http://layer.name" target="_blank">layer.name</a>()), </span><span style="color:rgb(0,16,128)">useAsDefault</span><span style="color:rgb(0,0,0)">=</span><span style="color:rgb(0,0,255)">True</span><span style="color:rgb(0,0,0)">, </span><span style="color:rgb(0,16,128)">uiFileContent</span><span style="color:rgb(0,0,0)">=</span><span style="color:rgb(163,21,21)">""</span><span style="color:rgb(0,0,0)">)</span></div></div>
<p></p>
<div>Since we are writing all with the flag
"default style" there is no need to load them when opening the
project.</div>
<div><br>
</div>
<div>Regards,</div>
<div><br>
</div>
<div>Jorge Gustavo</div>
<div><br>
</div>
<div>On 07/04/25 19:11, Ludwig Kniprath via
QGIS-User wrote:<br>
</div>
<blockquote type="cite">
<p>Hello Peter,</p>
<p>everything that users do in QGis can be done by Python scripts,
including your steps listed below. <br>
</p>
<p>There are some difficulties for the desired "one step"
solution, f. e. the name of the file-based layer must be
converted in a valid and unique postgis table-name, the fields
of the file-based layer must be stored with valid postgis
field-types and -names and so on...</p>
<p>Taking into account all special features, exceptions and
necessary settings, this results in a rather complex program,
the implementation of which may be more complex than the manual
implementation in QGis...</p>
<p>Just my two cents...</p>
<p>Ludwig<br>
</p>
<p><br>
</p>
<div>Am 07.04.25 um 08:45 schrieb
peter.berger--- via QGIS-User:<br>
</div>
<blockquote type="cite">
<div>
<p class="MsoNormal">There are serveral steps necessary to
store from QGIS a vector layer with his style in a postgres
DB. For a big amout of layers this is impracticaI.<u></u><u></u></p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal">Therfore I would do this in one step from
QGIS. <span lang="FR-CH">Is this in any way possible?<u></u><u></u></span></p>
<p class="MsoNormal"><span lang="FR-CH"><u></u> <u></u></span></p>
<p class="MsoNormal"><b><span style="font-size:8pt;font-family:"Trebuchet MS",sans-serif">Peter
Berger<u></u><u></u></span></b></p>
<p class="MsoNormal"><span style="font-size:8pt;font-family:"Trebuchet MS",sans-serif">Brunnwiesenstrasse
62<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:8pt;font-family:"Trebuchet MS",sans-serif">8049
Zürich<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:8pt;font-family:"Trebuchet MS",sans-serif">Schweiz<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:8pt;font-family:"Trebuchet MS",sans-serif">mobile
+41 79 301 71 54<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:8pt;font-family:"Trebuchet MS",sans-serif"><u></u> <u></u></span></p>
<p class="MsoNormal"><span style="font-size:8pt;font-family:"Trebuchet MS",sans-serif">The
several step procedure:<u></u><u></u></span></p>
<p><span style="font-family:Symbol">·</span><span lang="FR-CH">
</span><strong><span lang="FR-CH" style="font-family:"Calibri",sans-serif">Load
the layer</span></strong><span lang="FR-CH">: Import the
layer from a vector file into QGIS.<u></u><u></u></span></p>
<p><span style="font-family:Symbol">·</span><span lang="FR-CH">
</span><strong><span lang="FR-CH" style="font-family:"Calibri",sans-serif">Rename
(if necessary)</span></strong><span lang="FR-CH">: Give
the layer an appropriate name.<u></u><u></u></span></p>
<p><span style="font-family:Symbol">·</span> <strong><span style="font-family:"Calibri",sans-serif">Transfer
the layer to Postgres</span></strong>: Drag the layer
into the PostgreSQL database. You will find it there with
the specific name.<u></u><u></u></p>
<p><span style="font-family:Symbol">·</span> <strong><span style="font-family:"Calibri",sans-serif">Load
the layer from Postgres</span></strong>: Import it back
from the PostgreSQL database into QGIS.<u></u><u></u></p>
<p><span style="font-family:Symbol">·</span> <strong><span style="font-family:"Calibri",sans-serif">Assign
the style</span></strong>: Copy the style from the
original layer and assign it to the Postgres layer loaded in
QGIS.<u></u><u></u></p>
<p><span style="font-family:Symbol">·</span> <strong><span style="font-family:"Calibri",sans-serif">Export
the style</span></strong>: Export the style of the
Postgres layer. You will now find an option to save the
style with the exact name of the layer in the PostgreSQL
database.<u></u><u></u></p>
<p><span style="font-family:Symbol">·</span> <strong><span style="font-family:"Calibri",sans-serif">Delete
old layers and reload</span></strong>: Delete all
associated layers and reload the layer from the PostgreSQL
database.<u></u><u></u></p>
<p class="MsoNormal"><span style="font-size:8pt;font-family:"Trebuchet MS",sans-serif"><u></u> <u></u></span></p>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
<br>
<fieldset></fieldset>
<pre>_______________________________________________
QGIS-User mailing list
<a href="mailto:QGIS-User@lists.osgeo.org" target="_blank">QGIS-User@lists.osgeo.org</a>
List info: <a href="https://lists.osgeo.org/mailman/listinfo/qgis-user" target="_blank">https://lists.osgeo.org/mailman/listinfo/qgis-user</a>
Unsubscribe: <a href="https://lists.osgeo.org/mailman/listinfo/qgis-user" target="_blank">https://lists.osgeo.org/mailman/listinfo/qgis-user</a>
</pre>
</blockquote>
<br>
<fieldset></fieldset>
<pre>_______________________________________________
QGIS-User mailing list
<a href="mailto:QGIS-User@lists.osgeo.org" target="_blank">QGIS-User@lists.osgeo.org</a>
List info: <a href="https://lists.osgeo.org/mailman/listinfo/qgis-user" target="_blank">https://lists.osgeo.org/mailman/listinfo/qgis-user</a>
Unsubscribe: <a href="https://lists.osgeo.org/mailman/listinfo/qgis-user" target="_blank">https://lists.osgeo.org/mailman/listinfo/qgis-user</a>
</pre>
</blockquote>
<div>-- <br>
<div> <span> <b> <font size="+2">Geomaster, LDA</font> </b> </span><br>
<div> <b> <font color="#999999" size="-2">VENHA DESCOBRIR O CAMINHO DO OPEN SOURCE CONNOSC</font>
</b>
<table width="450" border="0" height="126" cellspacing="1" cellpadding="2">
<tbody>
<tr>
<td width="150" height="50" valign="top"> <font size="-1"><span> <font size="-2"> </font><br>
Rua Afonso Palmeira, 31, 7 D<br>
4715-278 Braga<br>
VAT/NIF </span><span> 510 906 109<br>
Phone</span><span> +351 253 257 173<br>
</span>Site <a href="http://geomaster.pt" rel="noreferrer" target="_blank">geomaster.pt</a><br>
GPS 41.54627, -8.40432 </font><br>
<span><div><cropped-geomaster300x300-1.png></div></span>
</td>
<td width="25" valign="top" align="right"> <font size="-1"><span></span></font>
<hr width="2" size="100" align="right"> </td>
<td width="150" height="50" valign="top" nowrap>
<font size="-1"> <font size="-1"> <font size="-2"> </font><br>
Jorge Gustavo Rocha </font><br>
<font color="#999999" size="-2">CTO<br>
<br>
</font>Mobile </font> <font size="-1"><span> +351 910 333 888<br>
Email <a href="mailto:jgr@geomaster.pt" target="_blank">jgr@geomaster.pt</a></span></font> </td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
<span>_______________________________________________</span><br><span>QGIS-User mailing list</span><br><span><a href="mailto:QGIS-User@lists.osgeo.org" target="_blank">QGIS-User@lists.osgeo.org</a></span><br><span>List info: <a href="https://lists.osgeo.org/mailman/listinfo/qgis-user" target="_blank">https://lists.osgeo.org/mailman/listinfo/qgis-user</a></span><br><span>Unsubscribe: <a href="https://lists.osgeo.org/mailman/listinfo/qgis-user" target="_blank">https://lists.osgeo.org/mailman/listinfo/qgis-user</a></span><br></div></blockquote></div></div>_______________________________________________<br>
QGIS-User mailing list<br>
<a href="mailto:QGIS-User@lists.osgeo.org" target="_blank">QGIS-User@lists.osgeo.org</a><br>
List info: <a href="https://lists.osgeo.org/mailman/listinfo/qgis-user" rel="noreferrer" target="_blank">https://lists.osgeo.org/mailman/listinfo/qgis-user</a><br>
Unsubscribe: <a href="https://lists.osgeo.org/mailman/listinfo/qgis-user" rel="noreferrer" target="_blank">https://lists.osgeo.org/mailman/listinfo/qgis-user</a><br>
</blockquote></div>