1

I'm trying to create a renderer with SVG symbols and I can't seem to find any example on this. Inspired from categorized renderer for regular symbols, I wrote this, but I get an error saying that the arguments in QgsSvgMarkerSymbolLayer() have the wrong type. I tried to change the data type but it still won't accept it.

The name of my categories are in the field "situation" and are "potentielle" and "existante", which need to match the icons in "statut_red" and "statut_blue".

Can anyone help?

#define the markers
statut_red = QgsSvgMarkerSymbolLayer(""+ path +"bloc1_typologieR.svg")
statut_blue = QgsSvgMarkerSymbolLayer(""+ path +"bloc1_typologieB.svg")

#define categories
c1_statut = QgsRendererCategory(statut_blue, "existante", True)
c2_statut = QgsRendererCategory(statut_red, "potentielle", True)

#create the renderer
statut_renderer = QgsCategorizedSymbolRenderer("situation", [c1_statut, c2_statut])
gares_asof0305.setRenderer(statut_renderer)

gares_asof0305.triggerRepaint()
0

1 Answer 1

2

First of all you need to construct your QgsSvgMarkerSymbolLayer with parameters, a dictionnary with the path and other caracteristics such as size or color. For the QgsRenderCategory you need to specify the value, the QgsSymbol and then the string. Depending on the type of your layer I give you two snippets of code that should work.

My answer is based on the answer of the post Automatic pyqgis categorized renderer classification [duplicate] by fuzzysolutions

If your layer is a point layer :

values = {"existante", "potentielle"}
categories = []
for value in values:    
    svgStyle = {}
    svgStyle['fill'] = '#000000'
    if value == "existante" :
        svgStyle['name'] = path + "bloc1_typologieB.svg"
    elif value == "potentielle" :
        svgStyle['name'] = path + "bloc1_typologieR.svg"
    svgStyle['outline'] = '#000000'
    svgStyle['outline-width'] = '0.3'
    svgStyle['size'] = '7'
    statut = QgsSvgMarkerSymbolLayer.create(svgStyle)

    symbol = QgsSymbol.defaultSymbol(gares_asof0305.geometryType())
    symbol.appendSymbolLayer(statut)
    
    category = QgsRendererCategory(value, symbol, str(value))
    categories.append(category)

renderer = QgsCategorizedSymbolRenderer('situation', categories)

# assign the created renderer to the layer
if renderer is not None:
   gares_asof0305.setRenderer(renderer)

gares_asof0305.triggerRepaint()

point_layer

if your layer is a polygon layer :

values = {"existante", "potentielle"}
categories = []
for value in values:    
    svgStyle = {}
    svgStyle['fill'] = '#000000'
    if value == "existante" :
        svgStyle['name'] = path + "bloc1_typologieB.svg"
    elif value == "potentielle" :
        svgStyle['name'] = path + "bloc1_typologieR.svg"
    svgStyle['outline'] = '#000000'
    svgStyle['outline-width'] = '0.3'
    svgStyle['size'] = '7'
    statut = QgsSvgMarkerSymbolLayer.create(svgStyle)

    symbol = QgsSymbol.defaultSymbol(gares_asof0305.geometryType())
    centroid_symbol = QgsCentroidFillSymbolLayer.create()
    centroid_symbol.setColor(QColor("transparent"))
    colorP = QtGui.QColor(255, 255, 255, 255)
    marker_symbol = QgsMarkerSymbol()
    marker_symbol.changeSymbolLayer(0, statut)
    centroid_symbol.setSubSymbol(marker_symbol)
    symbol.appendSymbolLayer(centroid_symbol)
    
    category = QgsRendererCategory(value, symbol, str(value))
    categories.append(category)

renderer = QgsCategorizedSymbolRenderer('situation', categories)

# assign the created renderer to the layer
if renderer is not None:
   gares_asof0305.setRenderer(renderer)

gares_asof0305.triggerRepaint()

polygon_layer

2
  • 1
    Thank you so much for explaining & providing these solutions !! It works perfectly, it saves me.
    – cam_gis
    Jul 21, 2021 at 12:05
  • Pas de quoi ! ;)
    – JULESG
    Jul 21, 2021 at 12:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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