[GIS] PyQGIS Use of predefined color ramps

color ramppyqgisqgis

I'm trying to apply one of predefined color ramps to a categorized field in a vector layer. To do this, I'm using the code below suggested by Irssvt in another post.

However, I can't see the result in map canvas unless I manually use the "Classify" button on the layer properties dialog. Is there something missing in my code? Is there a way do programmatically make the classify action?

myStyle = QgsStyleV2().defaultStyle()
defaultColorRampNames = myStyle.colorRampNames()
ramp = myStyle.colorRamp(defaultColorRampNames[4])
renderer = QgsCategorizedSymbolRendererV2("SETOR", [])
renderer.setSourceColorRamp(ramp)
sslyr.setRendererV2(renderer)                
# Publica e dá zoom
QgsMapLayerRegistry.instance().addMapLayer(sslyr)
iface.setActiveLayer(sslyr)
iface.zoomToActiveLayer()
canvas = iface.mapCanvas()

Best Answer

The problem is that you are using an empty list for categories, [], at the renderer constructor class:

renderer = QgsCategorizedSymbolRendererV2("SETOR", [])

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):

execfile(u'C:/pyqgis_scripts/categorized_vec.py'.encode('mbcs'))
[u'Blues', u'BrBG', u'BuGn', u'BuPu', u'GnBu', u'Greens', u'Greys', u'OrRd', u'Oranges', 
u'PRGn', u'PiYG', u'PuBu', u'PuBuGn', u'PuOr', u'PuRd', u'Purples', u'RdBu', u'RdGy', 
u'RdPu', u'RdYlBu', u'RdYlGn', u'Reds', u'Spectral', u'YlGn', u'YlGnBu', u'YlOrBr', u'YlOrRd']
Spectral
{u'color1': u'215,25,28,255', u'discrete': u'0', u'color2': u'43,131,186,255', u'stops':
 u'0.25;253,174,97,255:0.5;255,255,191,255:0.75;171,221,164,255'}

my style 54

and it works. This is the code:

from PyQt4.QtGui import QColor

layer = iface.activeLayer()


myStyle = QgsStyleV2().defaultStyle()

defaultColorRampNames = myStyle.colorRampNames()
print defaultColorRampNames
print defaultColorRampNames[22]

ramp = myStyle.colorRamp(defaultColorRampNames[22])  #Spectral name

rp = ramp.properties()
print rp
print "my style" , myStyle.symbolCount()

dictionary = {
    1: (QColor(215,25,28,255), '1'),
    2: (QColor(43,131,186,255), '2'),
    3: (QColor(253,174,97,255), '3'),
    4: (QColor(255,255,191,255), 'Unknown'),
}

categories = []

for item, (color, label) in dictionary.items():
    symbol = QgsSymbolV2.defaultSymbol(layer.geometryType())
    symbol.setColor(QColor(color))
    category = QgsRendererCategoryV2(item, symbol, label)
    categories.append(category)

renderer = QgsCategorizedSymbolRendererV2("SETOR", categories)

renderer.setSourceColorRamp(ramp)

layer.setRendererV2(renderer)     
layer.triggerRepaint()

I tested the code with this shapefile (single symbol):

enter image description here

and I got this another one when the code was ran at the Python Console:

enter image description here

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