Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

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

Learn how you can maximize big data in the cloud with Apache Hadoop. Download this eBook now. Brought to you in partnership with Hortonworks.

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.


require(RColorBrewer)
require(XML)
 
tf <- read.delim("D:/GIS_DataBase/Environmental_Data/soil/vector/WRB_FULL.txt", 
                 header = T, sep = "\t", stringsAsFactors = F)
 
# the file looks like this
head(tf)
#   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")
addAttributes(base,version="1.8.0-Lisboa",minimumScale="0",maximumScale="1e+08",hasScaleBasedVisibilityFlag="0")
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]))
 })
addChildren(categories,category)
 
# 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 DataFlow is an integrated platform that makes data ingestion fast, easy, and secure. Download the white paper now.  Brought to you in partnership with Hortonworks

Topics:

Published at DZone with permission of Kay Cichini, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

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

{{ parent.tldr }}

{{ parent.urlSource.name }}