QGIS – How to Save Color Value to Attribute Table for Vector Layer with Graduated Symbol Renderer

fields-attributespyqgispythonqgisvector-grid

I am trying to achieve this -> How to copy the color styling to a color column in a layer's attribute table?

script by @Germán Carrillo

prefix = "'"
layer = iface.activeLayer()
attr = layer.rendererV2().classAttribute()
attrColor = 'color' # Name of the field to store colors
fieldIndex = layer.dataProvider().fieldNameIndex(attrColor)
attrFeatMap = {}

for cat in layer.rendererV2().categories(): 
  expr = "\""+attr+"\"="+prefix+unicode(cat.value())+prefix
  for f in layer.getFeatures(QgsFeatureRequest(QgsExpression(expr))):
    attrMap = { fieldIndex : cat.symbol().color().name()}
    attrFeatMap[ f.id() ] = attrMap

layer.dataProvider().changeAttributeValues( attrFeatMap )

but after using that script I am getting an error

Traceback (most recent call last):
  File "<input>", line 1, in <module>
AttributeError: 'QgsGraduatedSymbolRendererV2' object has no attribute 'categories'

because it is for a layer classified with a Categorized Symbol Renderer.

So problem is that I can use this script only with random color…
But how to make same thing gradient colors like ndvi -1 / 1

could this help me? -> http://docs.qgis.org/testing/en/docs/pyqgis_developer_cookbook/vector.html#graduated-symbol-renderer

First time working with python…..

EDIT: Qgis 2.10.1 – PISA
EDIT: Iam adding picture

enter image description here

Best Answer

The issue is that the renderer class is QgsCategorizedSymbolRendererV2 instead of QgsGraduatedSymbolRendererV2. So, I categorized next shapefile by FID field:

enter image description here

and identical code (Germán Carrillo' script ) was run at the Python Console of QGIS:

prefix = "'"
layer = iface.activeLayer()
attr = layer.rendererV2().classAttribute()
attrColor = 'color' # Name of the field to store colors
fieldIndex = layer.dataProvider().fieldNameIndex(attrColor)
attrFeatMap = {}

for cat in layer.rendererV2().categories(): 
  expr = "\""+attr+"\"="+prefix+unicode(cat.value())+prefix
  for f in layer.getFeatures(QgsFeatureRequest(QgsExpression(expr))):
    attrMap = { fieldIndex : cat.symbol().color().name()}
    attrFeatMap[ f.id() ] = attrMap

layer.dataProvider().changeAttributeValues( attrFeatMap ) 

As you can see at the next image, result was completely satisfactory (I am not getting any error):

enter image description here

Editing Note:

This is the code for Graduated Symbol Renderer:

layer = iface.activeLayer()

provider = layer.dataProvider()

renderer = layer.rendererV2()

feats = [ feat for feat in layer.getFeatures() ]
values = [ feat.attribute('FID')  for feat in feats ] #change for 'NDVI' in case of NDVI field

n = len(feats)

color_feats = [ renderer.symbolForValue(value).color().name() for value in values ]

idx = layer.fieldNameIndex('color')

for i, feat in enumerate(feats):
    new_values = {idx : color_feats[i]}
    provider.changeAttributeValues({i:new_values})

After running it at the Python Console of QGIS, values at 'color' field were placed as expected.

enter image description here

By using 'NDVI' field it also works perfectly:

enter image description here