2

Is there any way to create a symbol like on the screenshot in only one symbollayer in a PyQGIS-App (based on QGIS 2.8.6)? Or do I have to use multiple layers to create such a symbol?

enter image description here

4
  • What's the reason behind wanting to create a complex symbol in a single symbol layer? – underdark Nov 1 '16 at 22:17
  • Those points are representing ships and the line should show the current course, the different circles should show the type of ship and the current status. – Papierschnellboot Nov 1 '16 at 22:24
  • That's possible with one layer style consisting of multiple symbol layers. – underdark Nov 1 '16 at 22:40
  • Ok, thx. But how can I add multiple symbol layer. For my existing first symbol layer I use the following code: symbol = QgsMarkerSymbolV2.createSimple({'name': 'circle', 'color': 'grey'}) vl.rendererV2().setSymbol(symbol) – Papierschnellboot Nov 1 '16 at 22:57
5

If I understand your question right - You solved the Problem by creating Symbol in QGis, but you don't know how to write it in pyqgis, right?!

Try this:

symbol = QgsMarkerSymbolV2.createSimple(
    {'name': 'circle', 'color': 'grey'})

# Delete first default symbollayer:
symbol.deleteSymbolLayer(0)

# Create and insert multiple symbollayers (Example):
colors = ['red', 'green', 'blue']
for i, color in enumerate(colors):
    new_symbollayer = QgsSimpleMarkerSymbolLayerV2()
    new_symbollayer.setSize(40 - i*10)
    new_symbollayer.setFillColor(QColor(color))
    # See QgsSimpleMarkerSymbolLayer for more parameters...

    # Add symbollayer to the symbol:
    symbol.appendSymbolLayer(new_symbollayer)
layer.rendererV2().setSymbol(symbol)
2
  • thx dude. this is exactly what I was looking for. – Papierschnellboot Nov 2 '16 at 23:04
  • I'm trying to do the same thing, but for lines, using QGIS 3.8. One problem is that the QgsLineSymbol() class doesn't have methods like .setSize, and .setFillerColor. Does anyone have an example of creating multilpe symbols for a line layer? – grego Jun 15 '20 at 22:55
2

If you want to have more control with the size of the symbols, you can use this code:

layer = iface.activeLayer()
renderer = layer.rendererV2()

symbol1 = QgsMarkerSymbolV2.createSimple({'color': 'orange',
                                          'size':'5'}) 

symbol2 = QgsMarkerSymbolV2.createSimple({'color': 'blue',
                                          'size':'3'}) 

#Create Symbol Layers
symbol_layer1 = symbol1.symbolLayer(0)
symbol_layer2 = symbol2.symbolLayer(0)

#Append Symbol Layer 2 to symbol1 (now with two Symbol Layers)
symbol1.appendSymbolLayer(symbol_layer2.clone())

renderer.setSymbol(symbol1) 

layer.triggerRepaint()
iface.legendInterface().refreshLayerSymbology(layer)

After running it at the Python Console of QGIS, with a point layer shapefile, I got:

enter image description here

However, with Henhuy's code modified as follows, you can get the same result.

from qgis.PyQt.QtGui import QColor

layer = iface.activeLayer()
symbol = layer.rendererV2().symbol()

# Create and insert multiple symbollayers
colors = ['orange', 'blue']
size = [5, 3]

for i, color in enumerate(colors):
    new_symbollayer = QgsSimpleMarkerSymbolLayerV2()
    new_symbollayer.setSize(size[i])
    new_symbollayer.setFillColor(QColor(color))

    # Add symbollayer to the symbol:
    symbol.appendSymbolLayer(new_symbollayer)

layer.rendererV2().setSymbol(symbol)

layer.triggerRepaint()
iface.legendInterface().refreshLayerSymbology(layer)
1
  • your solution produces much overhead! For each QgsSymbolLayer you create a whole new QgsSymbol and you also clone the underlying QgsSymbolLayer - afterwards the symbol and its symbollayer are dumped. Suggest you want to create a symbol with 5 underlying symbollayers - in your case this would lead to 4 unused QgsSymbols and 4 QgsSymbolLayers... I also don't get, why you should have "more control" on the size in your solution. – Henhuy Nov 3 '16 at 9:37

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.