Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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.

share|improve this question
    
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. –  a different ben Sep 30 '11 at 2:07
1  
This would make a good feature request for QGIS. Shouldn't to hard to do. –  Nathan W Sep 30 '11 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. –  a different ben Oct 1 '11 at 10:39
    
see the Python solution –  gene Oct 3 '11 at 15:26
add comment

1 Answer 1

up vote 7 down vote accepted

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>
share|improve this answer
    
Thanks, that is cool! –  a different ben Oct 1 '11 at 10:36
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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