Over a million developers have joined DZone.

Creating a QGIS-Style (qml-file) with an R-Script

DZone's Guide to

Creating a QGIS-Style (qml-file) with an R-Script

· Big Data Zone ·
Free Resource

The open source HPCC Systems platform is a proven, easy to use solution for managing data at scale. Visit our Easy Guide to learn more about this completely free platform, test drive some code in the online Playground, and get started today.

How to get from a txt-file with short names and labels to a QGIS-Style (qml-file)? I used the below R-script to create a style for this legend table where I copy-pasted the parts I needed to a txt-file, like for the WRB-FULL (WRB-FULL: Full soil code of the STU from the World Reference Base for Soil Resources). The vector data to which I applied the style is freely available at ESDAC - you just need to submit a form to get access to the data. BTW, thanks to a helping hand on SO.

tf <- read.delim("D:/GIS_DataBase/Environmental_Data/soil/vector/WRB_FULL.txt", 
                 header = T, sep = "\t", stringsAsFactors = F)
# the file looks like this
#   Value                  Label
# 1    AB            Albeluvisol
# 2  ABal       Alic Albeluvisol
# 3  ABap   Abruptic Albeluvisol
# 4  ABar     Arenic Albeluvisol
# 5  ABau     Alumic Albeluvisol
# 6 ABeun Endoeutric Albeluvisol
n <- nrow(tf)
# random colors
rand_rgb <- function() paste(paste(sample(1:255, 3, replace=T), collapse=","), "255", sep=",")
for(i in 1:n) tf$v[i] <- rand_rgb() 
# set alpha
alpha = 0.15
base = newXMLNode("qgis")
trans <- newXMLNode("transparencyLevelInt", 255)
rend <- newXMLNode("renderer-v2", attrs = c(attr="WRBFU",symbollevels="0",type="categorizedSymbol"))
# sort the categories
categories <- newXMLNode("categories")
category <- lapply(seq_along(tf$Value),function(x){newXMLNode("category",
 attrs = c(symbol = as.character(x-1), value = tf$Value[x], label = tf$Label[x]))
# sort the symbols
symbols <- newXMLNode("symbols")
symbol <- lapply(seq_along(tf$Value),function(x){dum.sym <- newXMLNode("symbol",
 attrs = c(outputUnit="MM",alpha=alpha,type="fill",name=as.character(x-1)))
 layer <- newXMLNode("layer", attrs =c(pass="0",class="SimpleFill",locked="0"))
 prop <- newXMLNode("prop", attrs =c(k="color",v= tf$v[x]))
 addChildren(layer, prop)
 addChildren(dum.sym, layer)
addChildren(symbols, symbol)
# add categories and symbols to rend
addChildren(rend, list(categories, symbols))
addChildren(base, list(trans, rend))
# save to qml-file
writeLines(saveXML(base), "D:/GIS_DataBase/Environmental_Data/soil/vector/WRBFU.qml")

Managing data at scale doesn’t have to be hard. Find out how the completely free, open source HPCC Systems platform makes it easier to update, easier to program, easier to integrate data, and easier to manage clusters. Download and get started today.


Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}