Categorized symbols and graduated symbols are applied in pretty much the same way. The only difference is that graduated symbols are taking into account certain ranges, whereas, categorized is simply taking in the actual field value. Quite honestly, you could literally replicate the code given from the PyQGis cookbook site: http://docs.qgis.org/testing/en/docs/pyqgis_developer_cookbook/vector.html#id19 and instead of inserting "upper" and "lower" ranges values, just put the 6 values you have: 'A-6', 'B-4', 'G-5', 'D-2', 'P-7', 'C-3'. It works the exact same way.
# create graduated marker symbol
blocks = (('Label_Name1', 'A-6', 'A-6', 'red'), \
('Label_Name2', 'B-4', 'B-4', 'green'), \
('Label_Name3', 'G-5', 'G-5' 'red'), \
('Label_Name4', 'D-2', 'D-2', 'red'), \
('Label_name5', 'P-7', 'P-7', 'red'), \
('Label_Name6', 'C-3', 'C-3', 'red'))
ranges = []
for label, lower, upper, color in blocks:
rng = QgsRendererRangeV2(lower, upper, label)
ranges.append(rng)
field = "blocks"
renderer = QgsGraduatedSymbolRendererV2(field, ranges)
layer.setRendererV2(renderer)
I can't help on the details of QML and how to make colourmaps in QGIS, but I can give some info on the cpt-city gradients --- the "height information" is contained only in the CPT files, the values in the SVG files are normalised to be in the range from 0 -- 1. This is required by the SVG standard (and reflects the "graphic design" nature of SVG gradients). The CPT format is rather simple
z0 r g b z1 r g b
z1 r g b z2 r g b
:
so you should be able to extract the values fairly easily (if tediously).
Some time ago, Etienne Tourigny created a "Color Ramp Manager" plugin for QGIS which contains a lot of cpt-city gradients, perhaps you could try that route first?
Best Answer
The problem is that you are using an empty list for categories, [], at the renderer constructor class:
It needs a QgsRendererCategoryV2 object class. I don't know how to do that precisely but, I modified your code for creating the categories from a dictionary, by using the colors of ramp named Spectral (taken from ramp properties; see below):
and it works. This is the code:
I tested the code with this shapefile (single symbol):
and I got this another one when the code was ran at the Python Console:
The colors belong to the Spectral ramp but they are not in the correct order.
Editing note:
The algorithm for interpolating the colors and to generate the categories based in the ramp color can be observed here:
https://svn.osgeo.org/qgis/branches/Release-1_5_0/src/core/symbology-ng/qgsvectorcolorrampv2.cpp