2

How to save changing of colors?

For example, we change color for all features of a layer in a loop. But as a result, I see only latest changing.

My code does not work perfectly:

for k in masLevel:          
    iface.mapCanvas().setSelectionColor( QColor(colours[k-1]) )
    #filter features to select
    expr = QgsExpression( "\"Order\"='%d'"%int(k) )
    it = rivers.getFeatures( QgsFeatureRequest( expr ) )
    ids = [i.id() for i in it]
    rivers.setSelectedFeatures( ids )
    rivers.triggerRepaint()
    rivers.commitChanges()

Could you help me?

  • You should post a reproducible snippet of code: for example, which are the increasing variables in the loop? – mgri May 11 '17 at 13:10
  • colours = ["red","yellow","cyan","green","blue","magenta","grey","darkRed","black","darkGreen","darkBlue","darkCyan","darkMagenta","darkYellow","white"] k=1 maxlevel= 7 while k <= maxlevel: iface.mapCanvas().setSelectionColor( QColor(colours[k-1]) ) #filter features to select expr = QgsExpression( "\"Order\"='%s'"%k ) it = layer.getFeatures( QgsFeatureRequest( expr ) ) ids = [i.id() for i in it] layer.setSelectedFeatures( ids ) layer.triggerRepaint() layer.commitChanges() k+=1 @mgri – Евгения Сидоренко May 11 '17 at 13:28
  • Is seems like you want to apply a categorized renderer, i.e. applying a specific color when a feature has a certain value. Am I right? (Please, add the code in your question instead of copy-and-paste it as a comment). – mgri May 11 '17 at 14:08
  • @mgri I edit code in my question. Yes, u r right.. – Евгения Сидоренко May 12 '17 at 4:39
  • @mgri Could u please help me with my code?? – Евгения Сидоренко May 12 '17 at 5:58
2

From comments, it seems that you want to apply a categorized renderer. A loop like yours is not necessary (unless there are other needs that you haven't explained in your original question).

You may use the following code as a reference (adapt it to your specific case):

from qgis.core import *
from PyQt4.QtGui import QColor

layer = iface.activeLayer() # load the layer as you want

# define the lookup >> value : (color, label)
colors = {1: ('red', 'some_text_for_red'), 2: ('yellow', 'some_text_for_yellow'), 3: ('cyan', 'some_text_for_cyan'), 4: ('green', 'some_text_for_green'), 5: ('blue', 'some_text_for_blue'), 6: ('magenta', 'some_text_for_magenta'), 7: ('grey', 'some_text_for_grey')}

# create a category for each item in your layer
categories = []
for value, (color, label) in colors.items():
    symbol = QgsSymbolV2.defaultSymbol(layer.geometryType())
    symbol.setColor(QColor(color))
    category = QgsRendererCategoryV2(value, symbol, label)
    categories.append(category)

# create the renderer and assign it to the layer
expression = 'my_field' # field name
renderer = QgsCategorizedSymbolRendererV2(expression, categories)

renderer = QgsCategorizedSymbolRendererV2(expression, categories)
layer.setRendererV2(renderer)
layer.triggerRepaint()

It will return something like this:

enter image description here

  • 1
    Thanks a lot! Here is my analog to your variant: k=1 while k<=maxlevel: symbol = QgsSymbolV2.defaultSymbol(rivers.geometryType()) symbol.setColor(QColor(colours[int(k-1)])) cat = QgsRendererCategoryV2(k, symbol, str(k)) renderer.addCategory(cat) k+=1 rivers.triggerRepaint() iface.mapCanvas().refresh() iface.legendInterface().refreshLayerSymbology(rivers) – Евгения Сидоренко May 12 '17 at 9:33
  • @ЕвгенияСидоренко Well done! If my answer helped you to find a solution, please consider accepting it (it will also help future users having the same issue). – mgri May 12 '17 at 9:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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