3

I have a Python script to change the color of some specific features (lines) using categories in a vector layer. When I import the script in the console, the first time through the console it works just fine. But (while qgis is open) when I edit the script and change the color of some category to 'red' instead of the previous 'green' and then import script again, it doesn't show the changes, the lines have the same color like before. Here's the script and some images:

from PyQt4.QtGui import *
from PyQt4.QtCore import *
from qgis.core import *
from qgis.utils import iface

def change_color():
    active_layer = iface.activeLayer()

    pipeline = [["1", "green"],["2","red"],
    ["3","green"],["4","green"],["5","green"]]

    categories=[]

    for label,color in pipeline:
        symbol = QgsSymbolV2.defaultSymbol(active_layer.geometryType())
        symbol.setColor(QColor(color))
        category = QgsRendererCategoryV2(int(label), symbol, label)
        categories.append(category)

    expression = 'id' 
    renderer = QgsCategorizedSymbolRendererV2(expression, categories)
    active_layer.setRendererV2(renderer)
    active_layer.setCacheImage(None)
    iface.mapCanvas().refresh()
    iface.legendInterface().refreshLayerSymbology(active_layer)

change_color()

enter image description here

Now let's say for example that I edit script and change pipeline to:

pipeline = [["1", "green"],["2","red"],
["3","red"],["4","green"],["5","red"]]

and run script again. Then it should show 3 red lines instead of 1. But it doesn't! Hope I was clear enough. (I'm not even close to a developer.)

  • that works for me..are you running it in the console or code window? – vinayan Mar 3 '15 at 9:11
  • Does it work after you change the categories on the script?I mean first time lets say you want one line to be red and the rest gree. You run it and it works. Then, WITHOUT CLOSING QGIS, you edit the script and set all lines to "red".Run script again in QGIS.Does it work that way?For me it does not :( I use the python console in QGIS and do import script – Stelios M Mar 3 '15 at 9:15
  • i copy pasted the script in the code editor window, and it works even after changing categories.. – vinayan Mar 3 '15 at 10:35
  • i am editing code like below i.imgur.com/0p9XKFz.png – vinayan Mar 3 '15 at 10:39
  • Thanks vinayan i was careless and i didnt use the execfile command. i just wrote import (name of script) – Stelios M Mar 3 '15 at 10:48
3

I think i found it. If i use >>>execfile instead of >>>import (name of script) it works as many times as i want without closing QGIS

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.