From attribute table to QGIS style file – step 1

I have a large number of GRASS vector layers with in the attribute table a RGB column defining the colours of the corresponding mapping units. Unfortunately, unlike GRASS, QGIS does not offer the option to to set colours from an RGB column. The feature has been requested before, but it isn’t implemented (yet).

Edit: Since writing of this post, the feature has been implemented; see this post and this post on

As an alternative, I want to create a R script that takes the RGB values from the attribute tables and uses this to create a QGIS style file (qml file). The first step, worked out in this post, is to define the styles (fill and border style and color for each mapping unit).

To do this, I am using the brew function from the brew package. Brew  provides a template system for text reporting. In my case, I have a template that defines a fill style (border and fill colour and style). The text between <%= and %> are place holders that are replaced by user defined text when running Brew. This includes the colour and symbol name place holders, which are replaced by respectively the RGB and code values from the attribute table.

# Libraries &amp; functions

library(RSQLite); library(sqldf); library(brew)

function(x = &quot;SQLite&quot;,y = locdb){
  dbConnect(x, dbname=y)

function(x=con) { dbDisconnect(conn = x) }

# Get database with legend and RGB from a SQLite table
# (this is the attribute table of a GRASS vector layer.
# Obviously, you can use any table that lists a set of
# RGB values against a code that can in turn be linked
# to a vector layer).

xml.out &lt;- file(paste(getwd(), &quot;/&quot;, outputXML, sep=&quot;&quot;),&quot;w&quot;)
locdb &lt;- &quot;/home/paulo/grassdb/vegmap/regional_legend.db3&quot;
con1 &lt;- connect()
legendTable &lt;- dbReadTable(conn=con1, &quot;legend&quot;)

# Set parameters. I am defining them separately rather then
# within the brew template to make it # easier to adapt later

base.PNVcode &lt;- legendTable$code2
base.ColorFill &lt;- legendTable$Color.RGB
ColorBorder &lt;- &quot;0,0,0,255&quot;
FillStyle &lt;- &quot;solid&quot;
BorderStyle &lt;- &quot;no&quot;
BorderWidth &lt;- &quot;0.2&quot;

# Run brew using inline template

brew(text=paste(&quot;&lt;!DOCTYPE qgis_style&gt;
  &lt;qgis_style version=\&quot;0\&quot;&gt;
  &lt;symbols&gt;&lt;%-%&gt;&quot;, sep=&quot;&quot;), output=xml.out)

for(i in 1:dim(legendTable)[1]){
  PNVcode &lt;- base.PNVcode[i]
  ColorFill &lt;- gsub(&quot;:&quot;,&quot;,&quot;, base.ColorFill[i],)

    &lt;symbol outputUnit=\&quot;MM\&quot; alpha=\&quot;1\&quot; type=\&quot;fill\&quot; name=\&quot;&lt;%=PNVcode%&gt;\&quot;&gt;
      &lt;layer pass=\&quot;0\&quot; class=\&quot;SimpleFill\&quot; locked=\&quot;0\&quot;&gt;
        &lt;prop k=\&quot;color\&quot; v=\&quot;&lt;%=ColorFill%&gt;\&quot;/&gt;
        &lt;prop k=\&quot;color_border\&quot; v=\&quot;&lt;%=ColorBorder%&gt;\&quot;/&gt;
        &lt;prop k=\&quot;offset\&quot; v=\&quot;0,0\&quot;/&gt;
        &lt;prop k=\&quot;style\&quot; v=\&quot;&lt;%=FillStyle%&gt;\&quot;/&gt;
        &lt;prop k=\&quot;style_border\&quot; v=\&quot;&lt;%=BorderStyle%&gt;\&quot;/&gt;
        &lt;prop k=\&quot;width_border\&quot; v=\&quot;&lt;%=BorderWidth%&gt;\&quot;/&gt;
    &lt;/symbol&gt;&lt;%-%&gt;&quot;, sep=&quot;&quot;), output=xml.out)

  &lt;/qgis_style&gt;&quot;, sep=&quot;&quot;), output=xml.out)

You can import the xml file QGISstyles.xml with the just created styles in QGIS using the style manager (menu: settings – style manager – import). The next step is to create the legend style file (qml file), linking the above created symbols to the mapping units. See this post how to do this.

I have written this in R because I am not very familiar with potentially better tools for the job, such as python or bash scripting. And it is a good way to get a grip on how styles are defined in QGIS. You can check out GIS – Stack Exchange for related questions and different solutions or the Quantum GIS Code Snippets:


5 thoughts on “From attribute table to QGIS style file – step 1

  1. Martin Laloux

    I am the author of the Python solution in GIS – Stack Exchange and in Quantum GIS Code Snippets and I’m glad to see your alternative solution with R.

    1. pvanb

      Thanks :-). It is only the first step though. I hope to add the second step, from symbol fill style definition to a legend (qml) file, soon.

  2. Pingback: From attribute table to QGIS style file – step 2 | Ecostudies

  3. Pingback: RGB colours for vector layers in Quantum GIS « Duncan Golicher’s weblog

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s