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

PolyGeo
65.5k29 gold badges115 silver badges349 bronze badges
asked Mar 3, 2015 at 9:01
5
  • that works for me..are you running it in the console or code window? Commented Mar 3, 2015 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 Commented Mar 3, 2015 at 9:15
  • i copy pasted the script in the code editor window, and it works even after changing categories.. Commented Mar 3, 2015 at 10:35
  • i am editing code like below i.imgur.com/0p9XKFz.png Commented Mar 3, 2015 at 10:39
  • Thanks vinayan i was careless and i didnt use the execfile command. i just wrote import (name of script) Commented Mar 3, 2015 at 10:48

1 Answer 1

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

answered Mar 3, 2015 at 10:45

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.