3

With the code below I can add a marker to the symbology of a line type layer. But by default, this marker is a circle.

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()

How do I change the marker shape to ArrowHead?

Is it possible to adjust the marker rotation according to the line rotation?

enter image description here

1 Answer 1

5

I would recommend using a QgsMarkerLineSymbolLayer rather than a QgsGeometryGeneratorSymbolLayer.

For example:

layerName = 'rede_bombas'
vLayer = QgsProject.instance().mapLayersByName(layerName)[0]

sym = vLayer.renderer().symbol()
arrow_sym = QgsMarkerSymbol.createSimple({'name': 'arrowhead', 'outline_color': 'blue'})
marker_line_symbol_layer = QgsMarkerLineSymbolLayer()
marker_line_symbol_layer.setSubSymbol(arrow_sym)
marker_line_symbol_layer.setPlacement(Qgis.MarkerLinePlacement.SegmentCenter)
sym.appendSymbolLayer(marker_line_symbol_layer)
vLayer.triggerRepaint()

Expected result:

enter image description here

Though not about PyQGIS, this Q & A may be useful, as would the section on Appearance (Symbology) of Vector Layers from the PyQGIS Developer Cookbook.

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.