Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env Rscript
- # R Script R to create Quantum GIS legend file from table -----------
- # create .qml file from a table with RGB values
- # Source: http://pvanb.wordpress.com
- # Blog entries: http://bit.ly/1pXVlJJ http://bit.ly/1wET222
- # Libraries --------------------------------------------------------
- library(brew)
- # Set workspace from script is runnning (on Linux)
- setwd(system("pwd", intern = TRUE))
- # Import table -----------------------------------------------------
- legend_table <- read.csv("./files/legend_table.csv")
- # Create XML file - qml file ---------------------------------------
- xml.out <- file(paste(getwd(), "/", "QGIS_template.qml", sep=""),"w")
- # Set parameters ---------------------------------------------------
- # Names of the columns from the attribute table used to create the style file
- Link <- "ID2" # campo ID2
- Label <- "NAME" # Country name
- ColorFill <- "RGB" # Column with color code (R:G:B)
- # Create vectors with unique value, label and colour values
- Value <- legend_table[,Link]
- legend_table <- legend_table[!duplicated(Value),]
- Value <- legend_table[,Link] # I'm not really understand this steps ¿?
- Label <- legend_table[,Label] # Is it not the same as Label <- legend_table$NAME ??
- ColorFill <- legend_table[,ColorFill] # Is it not the same as ColorFill <- legend_table$RGB ??
- # Symbol parameters
- ColorBorder <- "0,0,0,255"
- FillStyle <- "solid"
- BorderStyle <- "no"
- BorderWidth <- "0.2"
- # Write header information to the style file --------------------------------
- brew(text=paste("<!DOCTYPE qgis PUBLIC 'http://mrcc.com/qgis.dtd' 'SYSTEM'>
- <qgis version=\"2.4.0-Chugiak\" minimumScale=\"0\" maximumScale=\"1e+08\" hasScaleBasedVisibilityFlag=\"0\">
- <transparencyLevelInt>255</transparencyLevelInt>
- <renderer-v2 attr=\"<%=Link%>\" symbollevels=\"0\" type=\"categorizedSymbol\">", sep=""), output=xml.out)
- # Write the categories definitions to the style file -------------------------
- # Write start line <categories>
- brew(text=paste("<categories>
- <%-%>", sep=""), output=xml.out)
- # Write categories with
- for(i in 1:dim(legend_table)[1]){
- Value2 <- Value[i]
- Label2 <- as.character(Label[i]) # added (from original code) to transform to character type
- brew(text=paste("<category symbol=\"<%=i%>\" value=\"<%=Value2%>\" label=\"<%=Label2%>\"/>
- <%-%>", sep=""), output=xml.out)
- }
- # Write end lines </categories> \n <symbols>
- brew(text=paste("</categories>
- <symbols>
- <%-%>", sep=""), output=xml.out)
- # Write the style definitions to the style file --------------------------------
- for(i in 1:dim(legend_table)[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%>\"/>
- </layer>
- </symbol>
- <%-%>", sep=""), output=xml.out)
- }
- # Write closing lines to the footer -----------------------------------------
- brew(text=paste("</symbols>
- </renderer-v2>
- </qgis>", sep=""), output=xml.out)
- # It works!! :D
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement