[Qgis-developer] Vector Layer Symbology Registry

Martin Dobias wonder.sk at gmail.com
Tue Feb 16 05:23:49 EST 2010

On Mon, Feb 15, 2010 at 4:14 AM, Jeremy Palmer <JPalmer at linz.govt.nz> wrote:
> I’m planning on creating a tool for the centralised storage and retrieval of
> QGIS vector layer styling. What I would like to do is whenever PostGIS/Shape
> file layers are loaded into any project, the associated QGIS styling
> information stored in a database is automatically set for that layer. This
> would be particularly useful for accessing consistent styling for data
> sources in multiple user environments.
> My initial thought is to create a python plug-in that does two things:
> 1)     Stores QML styling information and a layer identifier into a database
> for any selected vector layer. This database could be a PostgreSQL instance
> or the QGIS Sqlite DB.
> 2)     On the load of a layer its identifier is matched in the database and
> will automatically have its styling set.

Sounds good.

> A small enhancement would be to apply a stored style to any given vector
> layer. I’m also planning on storing and retrieving styling information for
> v1 and v2 renderers.

A good news is that you don't have to deal with the QML contents by
yourself - QgsMapLayer::loadNamedStyle() triggers all the hard work
for you.

> At this point, I have three questions:
> 1)     What is the best layer identifier to use for storage in the database?
> I currently have a set PostGIS layers are not unique across the whole
> database (i.e. some tables with same name in difference schemas), so this
> could be an issue when using the layer name. I thought about using the
> datasource URI, but that does not look very useful for PostGIS layers as the
> table schema is only populated when not in the PostgreSQL user’s schema
> search path.

I believe the layer should be identified by a triple (schema, table,
geometry_column) - as used by PostGIS in geometry_columns table.

> 2)     Is the best way to set the layer styling information on load by
> subscribing to the QgsMapLayerRegistry layerWasAdded signal? Does this event
> occur before the layer is first rendered?

Using that signal might work, though I'm not 100% sure.
I have another suggestion: during creation, every map layer first
tries to load it's default QML style (calling
QgsMapLayer::loadDefaultStyle). For layers in files it tries to use
the same file base name with .qml extension, if that fails, it tries
to load the style from sqlite database ~/.qgis/qgis.qmldb. So, what
you could do is to synchronize the local qgis.qmldb file from the
PostGIS database (e.g. on every load, once a day or only when it's

> 3)     I guess I’m going to have issues with the QML styling XML changing
> between QGIS versions. What is the best way to deal with this?

As stated above, you don't have to care about this. The styling is
done by QGIS code, you'll just call it.

Btw. I've just committed a small fix for python wrappers in r12949 to
make calling the styling functions better.


More information about the Qgis-developer mailing list