1

I am creating some layers in QGIS using PostgreSQL connection.

I have a python plugin doing the load of the layer in Qgis, this part of the code looks like this :

        uri = QgsDataSourceURI()
        db = SettingsProperties.get_db()
        pg = SettingsProperties.get_postgre_settings()
        uri.setConnection(db['host'], str(db['port']), project_name, pg['user'], pg['password'])
        uri.setDataSource('work', table_name, str(column_geom))

        layer = QgsVectorLayer(uri.uri(), tr(layer_name), "postgres")

No problem with this, but now, I would like to setup style when creating this layer, this relying on the value of some column ... etc

Actually, I am able to setup style through a .qml prepared, but I wanted to know if there is a possibility to create the style either in PostgreSQL or in the Python script in a "simple" way and then add it to the database ?

I know you can save the style with QGIS GUI. But I would like to script it.

2

There are two possible solutions, that I know: First, is to use a predefined style, saved e.g. in a .qml file. You can than load that .qml file with LAYER_OBJECT.loadNamedStyle(PATH_TO_QML), where PATH_TO_QML obviously point to the file location of the saved style:

layer = QgsVectorLayer(uri.uri(), tr(layer_name), "postgres")
layer.loadNamedStyle(PATH_TO_QML)

Second, you script your own symbology by using a certain renderer object. You'll find examples in the official and famous PyQGIS-Cookbook

You can begin with single-symbol, categorized-symbol or graduated-symbol layer or fine tune with further symbols or your own renderer.

  • ok, i'll try with the links you gave from the cookbook, it may suit my needs, thanks for the feedback – Maximilien jaffrès Aug 26 at 9:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.