1

I am trying to create a rule-based renderer with different SVG icons for each class. For some reason, I keep getting the error :

Rule' object has no attribute 'QgsSvgMarkerSymbolLayer'

Why do I get this error?

Here's the code :

#create root rule

svgStyle = {}
svgStyle['name'] = 'C:/PROGRA~1/QGIS3~1.16/apps/qgis-ltr/svg/transport/transport_train_station2.svg'
svgStyle['size'] = '7'
symbolLayer = QgsSvgMarkerSymbolLayer.create(svgStyle)
symbol = QgsSymbol.defaultSymbol(gares_toutes.geometryType()) 
symbol.changeSymbolLayer(0, symbolLayer)
renderer = QgsRuleBasedRenderer(symbol)
root = renderer.rootRule()

#rule 1 symbology
symbolvoc1 = root.children()[0].clone()
symbolvoc1.QgsSvgMarkerSymbolLayer(symbolvoc)
symbolvoc1.setSize(25)
#rule 2 symbology
symbolvoc2 = root.children()[0].clone()
symbolvoc2.QgsSvgMarkerSymbolLayer('C:/Users/zcami/Documents/STAGE M1/zooms/icones/centraleblue.svg')
symbolvoc2.setSize(15)
symbolvoc7.xOffset = -0.2
symbolvoc7.yOffset = -1.4

#same thing with 5 other rules...

#filter rules
gare = 'whatever'
symbolvoc1.setFilterExpression("\"Name\" = '{}'".format(gare))
symbolvoc2.setFilterExpression("\"Name\" != '{}' AND \"_vocation\" = 'Gare centrale'".format(gare))

#add rules
root.appendChild(symbolvoc1)
root.appendChild(symbolvoc2)

#apply the renderer
layer.setRenderer(renderer)
symborules = QgsRuleBasedRenderer(root)
layer.renderer().symbol().changeSymbolLayer(0, symborules)
layer.triggerRepaint()
0

1 Answer 1

1

symbolvoc1.QgsSvgMarkerSymbolLayer is not a method. It will always fails and throw your mentioned error

Rule' object has no attribute 'QgsSvgMarkerSymbolLayer'

Not sure how you debug but cut & paste each line to find where is your issue. For this error, replace

symbolvoc1 = root.children()[0].clone()
symbolvoc1.QgsSvgMarkerSymbolLayer(symbolvoc)
symbolvoc1.setSize(25)

by

symbolvoc1 = root.children()[0].clone()
symbolvoc1.symbol().setSize(25)

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.