Monday, 6 August 2018

symbology - How to create a style file for QGIS from a plain text file?


Using QGIS 1.7.



I have a plain text file, which lists 115 rgb values against a map code for a geological map. Viz.:


$ head gsv1Msymbology.txt
MAPCODE RED GREEN BLUE
Oc 143 255 255
WAT 255 255 255
Qa 244 250 202
Qdl 195 239 218
Na 248 255 238
Qd2 227 255 190
Qxw 248 255 238

Qns 255 148 83
Qn 255 202 190
....

Is there some tool to generate a QGIS style file from this? How much of the full style declaration is required? I have very little scripting-fu.


Here is a section from a style file with random colour-fill choices:



Daw
Daw


hard:circle
2
pixels




SolidLine
0.26


SolidPattern



If I can get away with just



Daw
Daw




and somehow merge the remainder, then I can probably do it in a spreadsheet.


Ben.



Answer



One solution would be (I've no idea how you could achieve this with GUI tools):


cat gsv1Msymbology.txt | grep -v MAPPCODE | while read line
do
echo $line | awk -F" " '{print "\n\t" $1 "\n\t " $1 "\n\t\n"}'
done


This yields:


diciu$ cat gsv1Msymbology.txt | grep -v MAPPCODE | while read line
> do
> echo $line | awk -F" " '{print "\n\t" $1 "\n\t " $1 "\n\t\n"}'
> done

Oc
Oc




WAT
WAT



The logic behind it is:



  • strip out the header line (grep -v)

  • for all the other lines, read them one at a time


  • run awk on each line with field separator set to " " -> this means your first field (value) is $1, the second is the red component ($2), and so on.

  • print the XML fragment replacing placeholders with actual values


The rest of the style section goes as plain text in the awk line, just like the rest (not very readable, I know):


diciu$ cat gsv1Msymbology.txt | grep -v MAPPCODE | while read line; do echo $line | awk -F" " '{print "\n\t" $1 "\n\t " $1 "\n\t\n\thard:circle\n\t2\n\tpixels\n\t\n\t\n\t\n\t\n\t\n\tSolidLine\n\t0.26\n\t\n\tSolidPattern\n\t\n"}'; done | head -n 18

Oc
Oc

hard:circle

2
pixels





SolidLine
0.26


SolidPattern


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...