This post is a follow up on an earlier post where I described how to use a column with RGB values in an (attribute) table to create mapping symbols for QGIS.

The next step is to create the qml legend file. A qml file is a xml file which describes the legend symbol, the value and the label. In the example below I will show how to create a qml file, based on the attribute table of a vector layer, using R.

The first step is to get the attribute table as a data frame in R. How you will do this will depend on the format of the attribute table / map. The R-data manual is an excellent starting point if you need to know more about data import in R. In the example below, I have already imported the attribute table “AfricaMainLand”. This table has three columns with the country code (ISO), the country names (NAME_ENGLISH) and the the colour code (RGB).

# Libraries &amp; functions #----------------------------------------------------------- library(brew) # Set parameters. #----------------------------------------------------------- # Names of the columns from the attribute table used to create the style file Link &lt;- &quot;ISO&quot; # Country ID (the ISO code) Label &lt;- &quot;NAME_ENGLISH&quot; # Country name ColorFill &lt;- &quot;RGB&quot; # Column with color code # Symbol parameters ColorBorder &lt;- &quot;0,0,0,255&quot; BorderStyle &lt;- &quot;no&quot; BorderWidth &lt;- &quot;0.2&quot; # Name and path of style file xml.out &lt;- file(paste(getwd(), &quot;/QGIS_template.qml&quot;, sep=&quot;&quot;),&quot;w&quot;) # Create vectors with unique value, label and colour values Value &lt;- africabounds[,Link] africabounds &lt;- africabounds[!duplicated(Value),] Value &lt;- africabounds[,Link] Label &lt;- africabounds[,Label] ColorFill &lt;- africabounds[,ColorFill] # Write header information to the style file #----------------------------------------------------------- brew(text=paste(&quot;&lt;!DOCTYPE qgis PUBLIC 'http://mrcc.com/qgis.dtd' 'SYSTEM'&gt; &lt;qgis version=\&quot;1.9.90-Alpha\&quot; minimumScale=\&quot;0\&quot; maximumScale=\&quot;1e+08\&quot; hasScaleBasedVisibilityFlag=\&quot;0\&quot;&gt; &lt;transparencyLevelInt&gt;255&lt;/transparencyLevelInt&gt; &lt;renderer-v2 attr=\&quot;&lt;%=Link%&gt;\&quot; symbollevels=\&quot;0\&quot; type=\&quot;categorizedSymbol\&quot;&gt;&quot;, sep=&quot;&quot;), output=xml.out) # Write the categories definitions to the style file #----------------------------------------------------------- brew(text=paste(&quot;&lt;categories&gt; &lt;%-%&gt;&quot;, sep=&quot;&quot;), output=xml.out) for(i in 1:dim(africabounds)[1]){ Value2 &lt;- Value[i] Label2 &lt;- Label[i] brew(text=paste(&quot; &lt;category symbol=\&quot;&lt;%=i%&gt;\&quot; value=\&quot;&lt;%=Value2%&gt;\&quot; label=\&quot;&lt;%=Label2%&gt;\&quot;/&gt; &lt;%-%&gt;&quot;, sep=&quot;&quot;), output=xml.out) } brew(text=paste(&quot;&lt;/categories&gt; &lt;symbols&gt; &lt;%-%&gt;&quot;, sep=&quot;&quot;), output=xml.out) # Write the style definitions to the style file #----------------------------------------------------------- for(i in 1:dim(africabounds)[1]){ Value2 &lt;- Value[i] ColorFill2 &lt;- gsub(&quot;:&quot;,&quot;,&quot;, ColorFill[i],) brew(text=paste(&quot; &lt;symbol outputUnit=\&quot;MM\&quot; alpha=\&quot;1\&quot; type=\&quot;fill\&quot; name=\&quot;&lt;%=i%&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;%=ColorFill2%&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;/layer&gt; &lt;/symbol&gt; &lt;%-%&gt;&quot;, sep=&quot;&quot;), output=xml.out) } # Write closing lines to the footer #----------------------------------------------------------- brew(text=paste(&quot;&lt;/symbols&gt; &lt;/renderer-v2&gt; &lt;/qgis&gt;&quot;, sep=&quot;&quot;), output=xml.out)

After creating the QGIS_template.qml file, you can open it in QGIS. Open your vector layer (AfricaMainLand in my example) in QGIS and open the layer properties (double click on the layer). Then, click on ‘Load Style..’ and find your newly created style file (GIS_template.qml in my example).

As I wrote earlier, you can check out GIS – Stack Exchange for related questions and different solutions. Let me also repeat the links I posted in my first post on creating QGIS styles.

Edit: it is now also possible to use RGB values defined in the attribute table of the vector layer. I added two links for explanations how to do this (first two links).

Hope this is of use 🙂