0

I have a line type layer and I want to add an svg type marker, I managed to add a simple marker to the layer's symbology according to the image, with the attached code. Is it possible to change this simple marker with an svg icon?

layerName = 'rede_bombas'
vLayer = QgsProject.instance().mapLayersByName(layerName)[0]
ggsl = QgsGeometryGeneratorSymbolLayer.create({})
ggsl.setSymbolType(QgsSymbol.Marker)
ggsl.setGeometryExpression("centroid($geometry)")
ggsl.setColor(QColor('Blue'))
symbol = vLayer.renderer().symbols(QgsRenderContext())[0]
symbol.insertSymbolLayer(1, ggsl)
vLayer.triggerRepaint()

enter image description here

1 Answer 1

0

I think I got it, but if anyone has a better solution.

path = r"C:\Users\Aloísio\AppData\Roaming\QGIS\QGIS3\profiles\default\python\plugins\QWater\icons\qwater.svg"
layerName = 'rede_bombas'
vLayer = QgsProject.instance().mapLayersByName(layerName)[0]

symbol_svg = QgsSvgMarkerSymbolLayer(path)
symbol_svg.setSize(6)
symbol_svg.setFillColor(QColor('#0000ff'))
symbol_svg.setStrokeColor(QColor('#ff0000'))
symbol_svg.setStrokeWidth(1)

marker_symbol = QgsMarkerSymbol()
marker_symbol.changeSymbolLayer(0, symbol_svg)

ggsl = QgsGeometryGeneratorSymbolLayer.create({})
ggsl.setSubSymbol(marker_symbol)
ggsl.setGeometryExpression("centroid($geometry)")

symbol = vLayer.renderer().symbols(QgsRenderContext())[0]
symbol.insertSymbolLayer(1, ggsl)
vLayer.triggerRepaint()

enter image description here

Your Answer

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

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