0

I would like to ask if it's somehow possible to create script with rule-based symbology in QGIS which would be based on expressions given from separate table (for example CSV, or Excel sheet). I used code given from user eurojam:

def rule_based_style(layer, symbol, label, expression, color):
    root_rule = renderer.rootRule()
    rule = root_rule.children()[0].clone()
    rule.setLabel(label)
    rule.setFilterExpression(expression)
    rule.symbol().setColor(QColor(color))
    root_rule.appendChild(rule)

layer = iface.activeLayer() 
symbol = QgsSymbol.defaultSymbol(layer.geometryType())
renderer = QgsRuleBasedRenderer(symbol)
f1 = open("d:/test.csv", "r", encoding='utf-8', errors='ignore')
count = 1
for line in f1.readlines():
   rule_based_style(layer, symbol, str(count), line, "red")
   count = count + 1

layer.setRenderer(renderer)
layer.triggerRepaint()
iface.layerTreeView().refreshLayerSymbology(layer.id())

Problem is that my expression which is for example: "Benzinka" = 'ano' AND "typ_silnic" = 'trunk' AND "Corine" = '121' now gets another quotation marks and the result is: """Benzinka"" = 'ano' AND ""typ_silnic"" = 'trunk' AND ""Corine"" = '121'"

  • 2
    it would be good to provide an example of your table structure and content – eurojam Feb 12 at 9:30
  • Sorry, my mistake. Each row is one expression which is relate to my dataset. for example one row: "Benzinka" = 'ano' AND "typ_silnic" = 'trunk' AND "Corine" = '121' – LenkaT Feb 12 at 9:34
  • So, you would your rules read like "row1 @excel", "row2 @excel" etc? – Erik Feb 12 at 9:41
  • Yeah! I am totaly beginner to using own script in QGIS, I just work with Processing Modeller. – LenkaT Feb 12 at 9:47
  • 1
    Please use the edit button beneath your question to revise it with any requested clarifications. – PolyGeo Feb 12 at 10:16
2

A basic script like the one below would do the job (based on the solution posted here Adding new rule by using QgsRuleBasedRenderer to existing one in PyQGIS?). The csv file in the example below is d:/test.csv:

def rule_based_style(layer, symbol, label, expression, color):
    root_rule = renderer.rootRule()
    rule = root_rule.children()[0].clone()
    rule.setLabel(label)
    rule.setFilterExpression(expression)
    rule.symbol().setColor(QColor(color))
    root_rule.appendChild(rule)

layer = iface.activeLayer() 
symbol = QgsSymbol.defaultSymbol(layer.geometryType())
renderer = QgsRuleBasedRenderer(symbol)
f1 = open("d:/test.csv", "r", encoding='utf-8', errors='ignore')
count = 1
for line in f1.readlines():
   rule_based_style(layer, symbol, str(count), line, "red")
   count = count + 1

layer.setRenderer(renderer)
layer.triggerRepaint()
iface.layerTreeView().refreshLayerSymbology(layer.id())
| improve this answer | |
  • It works, but problem is, that each rule has added quotation marks there: """Benzinka"" = 'ano' AND ""typ_silnic"" = 'trunk' AND ""Corine"" = '121'" – LenkaT Feb 12 at 10:12
  • in my testdata it worked...the data was like: "fclass" = 'farm' "fclass" = 'town' "fclass" = 'town' and "population" > 10000 – eurojam Feb 12 at 10:27

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.