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 gis.stackexchange.com

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 & functions #----------------------------------------------------------- library(RSQLite); library(sqldf); library(brew) function(x = "SQLite",y = locdb){ require(RSQLite) 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 <- file(paste(getwd(), "/", outputXML, sep=""),"w") locdb <- "/home/paulo/grassdb/vegmap/regional_legend.db3" con1 <- connect() legendTable <- dbReadTable(conn=con1, "legend") disconnect(con1) # Set parameters. I am defining them separately rather then # within the brew template to make it # easier to adapt later #----------------------------------------------------------- base.PNVcode <- legendTable$code2 base.ColorFill <- legendTable$Color.RGB ColorBorder <- "0,0,0,255" FillStyle <- "solid" BorderStyle <- "no" BorderWidth <- "0.2" # Run brew using inline template #---------------------------------------------------------- brew(text=paste("<!DOCTYPE qgis_style> <qgis_style version=\"0\"> <symbols><%-%>", sep=""), output=xml.out) for(i in 1:dim(legendTable)[1]){ PNVcode <- base.PNVcode[i] ColorFill <- gsub(":",",", base.ColorFill[i],) brew(text=paste(" <symbol outputUnit=\"MM\" alpha=\"1\" type=\"fill\" name=\"<%=PNVcode%>\"> <layer pass=\"0\" class=\"SimpleFill\" locked=\"0\"> <prop k=\"color\" v=\"<%=ColorFill%>\"/> <prop k=\"color_border\" v=\"<%=ColorBorder%>\"/> <prop k=\"offset\" v=\"0,0\"/> <prop k=\"style\" v=\"<%=FillStyle%>\"/> <prop k=\"style_border\" v=\"<%=BorderStyle%>\"/> <prop k=\"width_border\" v=\"<%=BorderWidth%>\"/> </layer> </symbol><%-%>", sep=""), output=xml.out) } brew(text=paste(" </symbols> </qgis_style>", sep=""), 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: