1

I'm using a QgsSingleSymbolRenderer to display various points and lines etc for a Line geometry.

I have a Single symbol symbology to display the last vertex as a: Line > Marker line > Marker > Simple marker which I'd like to colour according to one of the pre-defined colour ramps (e.g. Blues, with range between 0 - 100, based on an attribute value "count"), using PyQGIS.

I can do this manually in QGIS by opening the layer's properties and going to > Symbology > Line > Marker line > Marker > Simple marker where I can use the colour assistant to set the symbols fill colour based on the value of "count". Is it possible to do this using PyQGIS? A snippet of my current code looks something like:

# Define symbol layers
symbol_layers = QgsSymbol.defaultSymbol(vector_layer.geometryType())
symbol_layers.changeSymbolLayer(0, simple_line_symbol_layer)
symbol_layers.insertSymbolLayer(1, last_vertex_simple_marker_symbol_layer)

# Create renderer based on symbol layers, and apply to vector layer
renderer = QgsSingleSymbolRenderer(symbol_layers)
vector_layer.setRenderer(renderer)
2

Add sample code for lines and point layers

# Line Layer
layer = QgsProject.instance().mapLayersByName('rivers')[0]

renderer = QgsLineSymbol()

# F_CODE is the column name when I have hexadecimals colors
renderer.symbolLayers()[0].setDataDefinedProperty(QgsSymbolLayer.PropertyStrokeColor, QgsProperty.fromField("F_CODE") );
layer.setRenderer(QgsSingleSymbolRenderer(renderer))

layer.triggerRepaint()

# Point Layer
layer = QgsProject.instance().mapLayersByName('airports')[0]

renderer = QgsMarkerSymbol()

# Use is the column name when I have hexadecimals colors
renderer.symbolLayers()[0].setDataDefinedProperty(QgsSymbolLayer.PropertyFillColor, QgsProperty.fromField("USE") );
layer.setRenderer(QgsSingleSymbolRenderer(renderer))

layer.triggerRepaint()

Result Lines

enter image description here

Result Points

enter image description here

I hope this helps you

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.