From attribute table to QGIS style file – step 2

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.

Figure 1. The style of a vector layer, based on the values in the attribute table. The attributes in the attribute table are used to define 1) the symbol fill colour, 2) the value, and 2+3) the label.

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 & functions


# Set parameters.

# Names of the columns from the attribute table used to create the style file
Link <- "ISO"             # Country ID (the ISO code)
Label <- "NAME_ENGLISH"   # Country name
ColorFill <- "RGB"        # Column with color code

# Symbol parameters
ColorBorder <- "0,0,0,255"
BorderStyle <- "no"
BorderWidth <- "0.2"

# Name and path of style file
xml.out <- file(paste(getwd(), "/QGIS_template.qml", sep=""),"w")

# Create vectors with unique value, label and colour values
Value <- africabounds[,Link]
africabounds <- africabounds[!duplicated(Value),]
Value <- africabounds[,Link]
Label <- africabounds[,Label]
ColorFill <- africabounds[,ColorFill]

# Write header information to the style file

brew(text=paste("<!DOCTYPE qgis PUBLIC '' 'SYSTEM'>
<qgis version=\"1.9.90-Alpha\" minimumScale=\"0\" maximumScale=\"1e+08\" hasScaleBasedVisibilityFlag=\"0\">
<renderer-v2 attr=\"<%=Link%>\" symbollevels=\"0\" type=\"categorizedSymbol\">", sep=""), output=xml.out)

# Write the categories definitions to the style file

<%-%>", sep=""), output=xml.out)

for(i in 1:dim(africabounds)[1]){
Value2 <- Value[i]
Label2 <- Label[i]
brew(text=paste("  <category symbol=\"<%=i%>\" value=\"<%=Value2%>\" label=\"<%=Label2%>\"/>
<%-%>", sep=""), output=xml.out)

<%-%>", sep=""), output=xml.out)

# Write the style definitions to the style file
for(i in 1:dim(africabounds)[1]){
Value2 <- Value[i]
ColorFill2 <- gsub(":",",", ColorFill[i],)
brew(text=paste("    <symbol outputUnit=\"MM\" alpha=\"1\" type=\"fill\" name=\"<%=i%>\">
<layer pass=\"0\" class=\"SimpleFill\" locked=\"0\">
<prop k=\"color\" v=\"<%=ColorFill2%>\"/>
<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%>\"/>
<%-%>", sep=""), output=xml.out)

# Write closing lines to the footer
</qgis>", sep=""), 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).

Open a new style for your layer

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.

About these ads

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s