1

I would like to add my own SVG symbol instead of a dot. I tried the following.

layer = iface.activeLayer()
QgsProject.instance().addMapLayer(layer)
svgStyle = {}
svgStyle['fill'] = '#0000ff'
svgStyle['name'] = path
svgStyle['outline'] = '#000000'
svgStyle['outline-width'] = '6.8'
svgStyle['size'] = '6'
symbol = QgsSvgMarkerSymbolLayer.create(svgStyle)
layer.renderer().setSymbol(symbol)

Unfortunately the following error occurs:

TypeError: QgsSingleSymbolRenderer.setSymbol(): argument 1 has unexpected type 'QgsSvgMarkerSymbolLayer'
2

QgsSingleSymbolRenderer.setSymbol() need a QgsSymbol object not QgsSvgMarkerSymbolLayer.

To set the symbol you can use this, where we are changing the default style (0).

symbol = QgsSvgMarkerSymbolLayer(path)
symbol.setSize(6)
symbol.setFillColor(QColor('#0000ff'))
symbol.setStrokeColor(QColor('#ff0000'))
symbol.setStrokeWidth(1)
layer.renderer().symbol().changeSymbolLayer(0, symbol )
  • Thanks for your quick answer. Now there is no more error, but a question mark appears instead of my symbol. I used this path 'landmark/tourism=museum.svg' with a new temporary layer with dots. – daniel95 Jul 22 at 8:24
  • 1
    @daniel95 I updated the answer showing how to show path and properties. Please check. – spatialthoughts Jul 22 at 8:57

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.