Using QGIS version 1.7.
I have a plain text file that lists a set of rgb values against a code. I want to use this colour table to colour a polygon layer by mapping one of its attribute fields ('map_symb') to a code in the text file.
the colour table is very long, and looks like this:
$ 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
....
I want to match my 'map_symb' attribute to a value in MAPCODE, and use the corresponding RGB values to colour the polygons.
Is there a gui way to do this?
Answer
You can use Python with ElementTree module :
from string import *
from xml.etree import cElementTree as ET
class symbol:
def __init__(self,b=[]):
self.typec= typec
self.b = b
self.key = ['MAPCODE','R','G','B']
self.data = dict(zip(self.key,self.b))
self.symb = ET.SubElement(typec,"symbol")
self.lower = ET.SubElement(self.symb, "lowervalue")
self.upper = ET.SubElement(self.symb, "uppervalue")
self.outline = ET.SubElement(self.symb,"outlinecolor")
self.outsty = ET.SubElement(self.symb, "outlinestyle")
self.outtail = ET.SubElement(self.symb, "outlinewidth")
self.fillc = ET.SubElement(self.symb,"fillcolor")
self.fillp = ET.SubElement(self.symb,"fillpattern")
def creation(self):
self.lower.text = self.data['MAPCODE']
self.upper.text = self.data['MAPCODE']
self.outsty.text="SolidLine"
self.outtail.text="0.26"
self.outline.set("red",str(self.data['R']))
self.outline.set("green",str(self.data['G']))
self.outline.set("blue",str(self.data['B']))
self.fillc.set("red",str(self.data['R']))
self.fillc.set("green",str(self.data['G']))
self.fillc.set("blue",str(self.data['B']))
self.fillp.text = "SolidPattern"
# QML file creation
intro = ET.Element("qgis")
transp = ET.SubElement(intro,"transparencyLevelInt")
transp.text = '255'
classatr = ET.SubElement(intro, "classificationattribute")
classatr.text= "MAPCODE"
typec = ET.SubElement(intro,"uniquevalue")
classif = ET.SubElement(typec,"classificationfield")
classif.text="MAPCODE"
# RGB file processing
def main():
file = "RGB.txt"
f= open(file,"r")
while 1 :
line = f.readline()
if not line :
break
elem = split(line,',') #or tab, or space, or
symboltag = symbol(elem)
symboltag.creation()
result = ET.ElementTree(intro)
result.write("RGB.qml")
if __name__ == '__main__':
main()
The style file generated by this script is (and it works) :
255
MAPCODE
MAPCODE
Oc
Oc
SolidLine
0.26
SolidPattern
WAT
WAT
SolidLine
0.26
SolidPattern
and so...
You can also use the shapefile module ([shapefile])1 for shapefiles with RGB columns
import shapefile ....
[....]
noduplicates = []
def main():
sf = shapefile.Reader("RGBshape")
for rec in enumerate(sf.records()):
if rec[1][0] not in noduplicates:
noduplicates.append(rec[1][0])
symboltag = symbol(rec[1])
symboltag.creation()
else:
continue
and so...
No comments:
Post a Comment