[Qgis-user] Storing and Retrieving Vector Layers with Styles to Postgres DB
peter.berger at bluewin.ch
peter.berger at bluewin.ch
Wed Apr 9 01:41:27 PDT 2025
Dear Etienne
The code is working! But I missed initial steps.
1. You must have one or many vector layers inside QGIS.
2. Upload these layers with drag and drop to the postgres DB.
3. Dowload these layers from the postgres DB.
4. Create styles or load styles from style files (*.qml) to the layers. For many style files there is a nice extention available « Layer Stayle Loader »
5. Run the code below in the Python-Console. Take care the code still has the appopriate indents ! When you then load such a layer again from postgres DB then the style is loaded as well.
mapGeometryType = {
0: "Point",
1: "Line",
2: "Polygon",
3: "UnknownGeometry",
4: "NullGeometry",
}
layers = QgsProject.instance().mapLayers()
for layer in layers.values():
if layer.type() == QgsMapLayer.VectorLayer:
if mapGeometryType[layer.geometryType()] != "NullGeometry":
layer.deleteStyleFromDatabase
layer.saveStyleToDatabase(name=layer.name(),description="Default style for {}".format(layer.name()), useAsDefault=True, uiFileContent="")
P.S.
I tested it with the beautiful dataset from the Swiss Topo, <https://www.swisstopo.admin.ch/de/landeskarte-swiss-map-vector-10#Weiterf%C3%BChrende-Informationen-> Swiss Map Vector 10. It’s free!
Many thanks to all of you!
Peter
Von: Etienne Trimaille <etienne.trimaille at gmail.com>
Gesendet: Dienstag, 8. April 2025 18:18
An: peter.berger at bluewin.ch
Cc: Jorge Gustavo Rocha <jgr at geomaster.pt>; qgis-user at lists.osgeo.org
Betreff: Re: [Qgis-user] Storing and Retrieving Vector Layers with Styles to Postgres DB
The code from Jorge does the job for ALL layers in the current project, can you describe your issue Peter ?
Le mar. 8 avr. 2025 à 17:51, peter.berger--- via QGIS-User < <mailto:qgis-user at lists.osgeo.org> qgis-user at lists.osgeo.org> a écrit :
Hi Jorge
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?
Many thanks in advance.
Peter
Von meinem iPhone gesendet
Am 08.04.2025 um 10:59 schrieb Jorge Gustavo Rocha via QGIS-User <qgis-user at lists.osgeo.org <mailto:qgis-user at lists.osgeo.org> >:
Hi,
This is the code I use (since some years ago...) to write styles from all layers to the database:
mapGeometryType = {
0: "Point",
1: "Line",
2: "Polygon",
3: "UnknownGeometry",
4: "NullGeometry",
}
layers = QgsProject.instance().mapLayers()
for layer in layers.values():
if layer.type() == QgsMapLayer.VectorLayer:
if mapGeometryType[layer.geometryType()] != "NullGeometry":
layer.deleteStyleFromDatabase
layer.saveStyleToDatabase(name= <http://layer.name> layer.name(),description="Default style for {}".format( <http://layer.name> layer.name()), useAsDefault=True, uiFileContent="")
Since we are writing all with the flag "default style" there is no need to load them when opening the project.
Regards,
Jorge Gustavo
On 07/04/25 19:11, Ludwig Kniprath via QGIS-User wrote:
Hello Peter,
everything that users do in QGis can be done by Python scripts, including your steps listed below.
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...
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...
Just my two cents...
Ludwig
Am 07.04.25 um 08:45 schrieb peter.berger--- via QGIS-User:
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.
Therfore I would do this in one step from QGIS. Is this in any way possible?
Peter Berger
Brunnwiesenstrasse 62
8049 Zürich
Schweiz
mobile +41 79 301 71 54
The several step procedure:
* Load the layer: Import the layer from a vector file into QGIS.
* Rename (if necessary): Give the layer an appropriate name.
* Transfer the layer to Postgres: Drag the layer into the PostgreSQL database. You will find it there with the specific name.
* Load the layer from Postgres: Import it back from the PostgreSQL database into QGIS.
* Assign the style: Copy the style from the original layer and assign it to the Postgres layer loaded in QGIS.
* Export the style: 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.
* Delete old layers and reload: Delete all associated layers and reload the layer from the PostgreSQL database.
_______________________________________________
QGIS-User mailing list
QGIS-User at lists.osgeo.org <mailto:QGIS-User at lists.osgeo.org>
List info: https://lists.osgeo.org/mailman/listinfo/qgis-user
Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-user
_______________________________________________
QGIS-User mailing list
<mailto:QGIS-User at lists.osgeo.org> QGIS-User at lists.osgeo.org
List info: https://lists.osgeo.org/mailman/listinfo/qgis-user
Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-user
--
Geomaster, LDA
VENHA DESCOBRIR O CAMINHO DO OPEN SOURCE CONNOSC
Rua Afonso Palmeira, 31, 7 D
4715-278 Braga
VAT/NIF 510 906 109
Phone +351 253 257 173
Site <http://geomaster.pt> geomaster.pt
GPS 41.54627, -8.40432
<cropped-geomaster300x300-1.png>
_____
Jorge Gustavo Rocha
CTO
Mobile +351 910 333 888
Email <mailto:jgr at geomaster.pt> jgr at geomaster.pt
_______________________________________________
QGIS-User mailing list
<mailto:QGIS-User at lists.osgeo.org> QGIS-User at lists.osgeo.org
List info: <https://lists.osgeo.org/mailman/listinfo/qgis-user> https://lists.osgeo.org/mailman/listinfo/qgis-user
Unsubscribe: <https://lists.osgeo.org/mailman/listinfo/qgis-user> https://lists.osgeo.org/mailman/listinfo/qgis-user
_______________________________________________
QGIS-User mailing list
<mailto:QGIS-User at lists.osgeo.org> QGIS-User at lists.osgeo.org
List info: <https://lists.osgeo.org/mailman/listinfo/qgis-user> https://lists.osgeo.org/mailman/listinfo/qgis-user
Unsubscribe: <https://lists.osgeo.org/mailman/listinfo/qgis-user> https://lists.osgeo.org/mailman/listinfo/qgis-user
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/qgis-user/attachments/20250409/f6567a4e/attachment-0001.htm>
More information about the QGIS-User
mailing list