7

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:

<symbol>
  <lowervalue>Daw</lowervalue>
  <uppervalue>Daw</uppervalue>
  <label></label>
  <pointsymbol>hard:circle</pointsymbol>
  <pointsize>2</pointsize>
  <pointsizeunits>pixels</pointsizeunits>
  <rotationclassificationfieldname></rotationclassificationfieldname>
  <scaleclassificationfieldname></scaleclassificationfieldname>
  <symbolfieldname></symbolfieldname>
  <outlinecolor red="0" blue="0" green="0"/>
  <outlinestyle>SolidLine</outlinestyle>
  <outlinewidth>0.26</outlinewidth>
  <fillcolor red="215" blue="97" green="224"/>
  <fillpattern>SolidPattern</fillpattern>
  <texturepath></texturepath>
</symbol>

If I can get away with just

<symbol>
  <lowervalue>Daw</lowervalue>
  <uppervalue>Daw</uppervalue>
  <fillcolor red="215" blue="97" green="224"/>
</symbol>

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

Ben.

4
  • i've just made a spreadsheet to create one, actually was very easy. I started by classifying all of the features in my vector layer of interest, randomised the colours, then saved it out as a style file. Used a spreadsheet and lots of concatenations to produce the correct colour mapping. Sep 30, 2011 at 2:07
  • 1
    This would make a good feature request for QGIS. Shouldn't to hard to do.
    – Nathan W
    Sep 30, 2011 at 2:44
  • Far out, I didn't realise there were wars to see who can rewrite the question titles the best! Sorry I'll try harder next time; I wasn't aware of the format. Perhaps I should rtfm. Oct 1, 2011 at 10:39
  • see the Python solution
    – gene
    Oct 3, 2011 at 15:26

1 Answer 1

7

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 "<symbol>\n\t<lowervalue>" $1 "</lowervalue>\n\t<uppervalue> " $1 "</uppervalue>\n\t<fillcolor red=\"" $2 "\" blue=\"" $3 "\" green=\"" $4 "\"/>\n</symbol>"}'
done

This yields:

diciu$ cat gsv1Msymbology.txt | grep -v MAPPCODE | while read line
> do
> echo $line | awk -F" " '{print "<symbol>\n\t<lowervalue>" $1 "</lowervalue>\n\t<uppervalue> " $1 "</uppervalue>\n\t<fillcolor red=\"" $2 "\" blue=\"" $3 "\" green=\"" $4 "\"/>\n</symbol>"}'
> done
<symbol>
    <lowervalue>Oc</lowervalue>
    <uppervalue> Oc</uppervalue>
    <fillcolor red="143" blue="255" green="255"/>
</symbol>
<symbol>
    <lowervalue>WAT</lowervalue>
    <uppervalue> WAT</uppervalue>
    <fillcolor red="255" blue="255" green="255"/>
</symbol>

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 "<symbol>\n\t<lowervalue>" $1 "</lowervalue>\n\t<uppervalue> " $1 "</uppervalue>\n\t<label></label>\n\t<pointsymbol>hard:circle</pointsymbol>\n\t<pointsize>2</pointsize>\n\t<pointsizeunits>pixels</pointsizeunits>\n\t<rotationclassificationfieldname></rotationclassificationfieldname>\n\t<scaleclassificationfieldname>\n\t</scaleclassificationfieldname>\n\t<symbolfieldname></symbolfieldname>\n\t<outlinecolor red=\"" $2 "\" blue=\"" $3 "\" green=\"" $4 "\"/>\n\t<outlinestyle>SolidLine</outlinestyle>\n\t<outlinewidth>0.26</outlinewidth>\n\t<fillcolor red=\"" $2 "\" blue=\"" $3 "\" green=\"" $4 "\"/>\n\t<fillpattern>SolidPattern</fillpattern>\n\t<texturepath></texturepath>\n</symbol>"}'; done | head -n 18
<symbol>
    <lowervalue>Oc</lowervalue>
    <uppervalue> Oc</uppervalue>
    <label></label>
    <pointsymbol>hard:circle</pointsymbol>
    <pointsize>2</pointsize>
    <pointsizeunits>pixels</pointsizeunits>
    <rotationclassificationfieldname></rotationclassificationfieldname>
    <scaleclassificationfieldname>
    </scaleclassificationfieldname>
    <symbolfieldname></symbolfieldname>
    <outlinecolor red="143" blue="255" green="255"/>
    <outlinestyle>SolidLine</outlinestyle>
    <outlinewidth>0.26</outlinewidth>
    <fillcolor red="143" blue="255" green="255"/>
    <fillpattern>SolidPattern</fillpattern>
    <texturepath></texturepath>
</symbol>
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.