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

Hortonworks Sandbox for HDP and HDF is your chance to get started on learning, developing, testing and trying out new features. Each download comes preconfigured with interactive tutorials, sample data and developments from the Apache community.

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")

Hortonworks Community Connection (HCC) is an online collaboration destination for developers, DevOps, customers and partners to get answers to questions, collaborate on technical articles and share code examples from GitHub.  Join the discussion.


Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}