Advertisement
Guest User

R script to create QGIS legend file (qml)

a guest
Oct 30th, 2014
308
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 3.31 KB | None | 0 0
  1. #!/usr/bin/env Rscript
  2.  
  3. # R Script R to create Quantum GIS legend file from table -----------
  4. # create .qml file from a table with RGB values
  5. # Source: http://pvanb.wordpress.com
  6. # Blog entries: http://bit.ly/1pXVlJJ http://bit.ly/1wET222
  7.  
  8.  
  9. # Libraries --------------------------------------------------------
  10. library(brew)
  11.  
  12. # Set workspace from script is runnning (on Linux)
  13. setwd(system("pwd", intern = TRUE))
  14.  
  15. # Import table -----------------------------------------------------
  16. legend_table <- read.csv("./files/legend_table.csv")
  17.  
  18. # Create XML file - qml file ---------------------------------------
  19. xml.out <- file(paste(getwd(), "/", "QGIS_template.qml", sep=""),"w")
  20.  
  21.  
  22. # Set parameters ---------------------------------------------------
  23.  
  24. # Names of the columns from the attribute table used to create the style file
  25. Link <- "ID2"         # campo ID2
  26. Label <- "NAME"       # Country name
  27. ColorFill <- "RGB"    # Column with color code (R:G:B)
  28.  
  29. # Create vectors with unique value, label and colour values
  30. Value <- legend_table[,Link]
  31. legend_table <- legend_table[!duplicated(Value),]
  32. Value <- legend_table[,Link] # I'm not really understand this steps ¿?
  33. Label <- legend_table[,Label]  # Is it not the same as Label <- legend_table$NAME ??
  34. ColorFill <- legend_table[,ColorFill] # Is it not the same as ColorFill <- legend_table$RGB ??
  35.  
  36. # Symbol parameters
  37. ColorBorder <- "0,0,0,255"
  38. FillStyle <- "solid"
  39. BorderStyle <- "no"
  40. BorderWidth <- "0.2"
  41.  
  42.  
  43. # Write header information to the style file --------------------------------
  44.  
  45. brew(text=paste("<!DOCTYPE qgis PUBLIC 'http://mrcc.com/qgis.dtd' 'SYSTEM'>
  46. <qgis version=\"2.4.0-Chugiak\" minimumScale=\"0\" maximumScale=\"1e+08\" hasScaleBasedVisibilityFlag=\"0\">
  47. <transparencyLevelInt>255</transparencyLevelInt>
  48. <renderer-v2 attr=\"<%=Link%>\" symbollevels=\"0\" type=\"categorizedSymbol\">", sep=""), output=xml.out)
  49.  
  50.  
  51. # Write the categories definitions to the style file -------------------------
  52.  
  53. # Write start line <categories>
  54.  
  55. brew(text=paste("<categories>
  56. <%-%>", sep=""), output=xml.out)
  57.  
  58. # Write categories with
  59.  
  60. for(i in 1:dim(legend_table)[1]){
  61.   Value2 <- Value[i]
  62.   Label2 <- as.character(Label[i]) # added (from original code) to transform to character type
  63.   brew(text=paste("<category symbol=\"<%=i%>\" value=\"<%=Value2%>\" label=\"<%=Label2%>\"/>
  64. <%-%>", sep=""), output=xml.out)
  65. }
  66.  
  67. # Write end lines </categories> \n <symbols>
  68.  
  69. brew(text=paste("</categories>
  70. <symbols>
  71. <%-%>", sep=""), output=xml.out)
  72.  
  73.  
  74. # Write the style definitions to the style file --------------------------------
  75.  
  76. for(i in 1:dim(legend_table)[1]){
  77.   Value2 <- Value[i]
  78.   ColorFill2 <- gsub(":",",", ColorFill[i],)
  79.   brew(text=paste("<symbol outputUnit=\"MM\" alpha=\"1\" type=\"fill\" name=\"<%=i%>\">
  80. <layer pass=\"0\" class=\"SimpleFill\" locked=\"0\">
  81. <prop k=\"color\" v=\"<%=ColorFill2%>\"/>
  82. <prop k=\"color_border\" v=\"<%=ColorBorder%>\"/>
  83. <prop k=\"offset\" v=\"0,0\"/>
  84. <prop k=\"style\" v=\"<%=FillStyle%>\"/>
  85. <prop k=\"style_border\" v=\"<%=BorderStyle%>\"/>
  86. <prop k=\"width_border\" v=\"<%=BorderWidth%>\"/>
  87. </layer>
  88. </symbol>
  89. <%-%>", sep=""), output=xml.out)
  90. }
  91.  
  92. # Write closing lines to the footer -----------------------------------------
  93.  
  94. brew(text=paste("</symbols>
  95. </renderer-v2>
  96. </qgis>", sep=""), output=xml.out)
  97.  
  98. # It works!! :D
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement