4

QgsVectorLayer Right click->Properties->Style->Single symbol->click on simple line in tree node ->symbol layer type -> Marker line

How do I set marker line symbol programmatically by using Python?

3 Answers 3

7

The only documentation I found is here, but it doesn't help much in practice.

Here is an example I used to set the renderer of the current layer to a symbol with two layers: one 'Simple Line' and one 'Marker Line'. The marker line is tricky, because you need to work with a subSymbol.

# Use the currently selected layer
layer = qgis.utils.iface.mapCanvas().currentLayer()

registry = QgsSymbolLayerV2Registry.instance()
lineMeta = registry.symbolLayerMetadata("SimpleLine")
markerMeta = registry.symbolLayerMetadata("MarkerLine")

symbol = QgsSymbolV2.defaultSymbol(layer.geometryType())

# Line layer
lineLayer = lineMeta.createSymbolLayer({'width': '0.26', 'color': '255,0,0', 'offset': '-1.0', 'penstyle': 'solid', 'use_custom_dash': '0', 'joinstyle': 'bevel', 'capstyle': 'square'})

# Marker layer
markerLayer = markerMeta.createSymbolLayer({'width': '0.26', 'color': '255,0,0', 'interval': '3', 'rotate': '1', 'placement': 'interval', 'offset': '-1.0'})
subSymbol = markerLayer.subSymbol()
# Replace the default layer with our own SimpleMarker
subSymbol.deleteSymbolLayer(0)
triangle = registry.symbolLayerMetadata("SimpleMarker").createSymbolLayer({'name': 'filled_arrowhead', 'color': '255,0,0', 'color_border': '0,0,0', 'offset': '0,0', 'size': '1.5', 'angle': '0'})
subSymbol.appendSymbolLayer(triangle)

# Replace the default layer with our two custom layers
symbol.deleteSymbolLayer(0)
symbol.appendSymbolLayer(lineLayer)
symbol.appendSymbolLayer(markerLayer)

# Replace the renderer of the current layer
renderer = QgsSingleSymbolRendererV2(symbol)
layer.setRendererV2(renderer)

You can also inspect (and probably modify) the symbols for a layer, for example:

layer.rendererV2().symbol().symbolLayer(0).properties()
1
  • Link to documentation is broken
    – PolyGeo
    Commented Oct 5, 2022 at 22:31
3

I did it this way mimicking the way I see QGIS Desktop doing it:

enter image description here

Using the same names as in the image the python code looks like this:

# Base style.    
line = QgsLineSymbolV2()    

# Create an marker line.
marker_line = QgsMarkerLineSymbolLayerV2()
marker_line.setInterval(10)

# Configure the marker.
simple_marker = QgsSimpleMarkerSymbolLayerV2()
simple_marker.setShape(QgsSimpleMarkerSymbolLayerBase.ArrowHeadFilled)
simple_marker.setSize(3)
simple_marker.setAngle(180)
simple_marker.setColor(QColor(255,255,255))
simple_marker.setOutlineColor(QColor(255,255,255))

# The marker has its own symbol layer.
marker = QgsMarkerSymbolV2()
marker.changeSymbolLayer(0, simple_marker)

# Add the layer to the marker layer.
marker_line.setSubSymbol(marker)

# Finally replace the symbol layer in the base style.
line.changeSymbolLayer(0, marker_line)

# Add the style to the line layer.        
renderer = QgsSingleSymbolRendererV2(line)
layer.setRendererV2(renderer)
2

A solution with QGIS 3 python classes:

## Create the marker symbol
marker_symb = QgsSimpleMarkerSymbolLayer.create()   

## Change marker attributes
marker_symb.setSize(symbol_size)       
## more attributes....

## Create the marker line symbol
main_symbol = QgsMarkerLineSymbolLayer.create()

## Replace the sub symbol with the new marker symbol
main_symbol.subSymbol().changeSymbolLayer(0, marker_symb)

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