1

I work with QGIS 2.18 under linux, writing an automated mapping tool. I use the PyQGIS API and the corresponding symbology steps. For me it seems that the straight forward declaration for colouring a polygon is strange, in symbol layer creation and access (create, config, add and embed... and so on). To get things work I've encapsulated creation of the symbology for a simple "polygon fill" like this:

def makeFillRenderer(width, fcolor, bcolor, bstyle = Qt.SolidPattern):
    if bcolor == -1:
       bcolor = fcolor
    symbol = QgsFillSymbolV2()
    layer = symbol.symbolLayer(0)
    layer.setColor(fcolor)
    layer.setFillColor(fcolor)
    layer.setBorderColor(bcolor)
    layer.setBorderWidth(width)
    layer.setBrushStyle(bstyle)
    renderer = QgsSingleSymbolRendererV2(symbol)
    renderer.symbols()[0].changeSymbolLayer(0, layer)
    return renderer

Question: Is there a simple (natural) possibility to create a renderer without the implicid switch between hidden sublayer creation on symbol/ renderer levels and layer changing on symbol/ renderer on that levels aftermath?

I mean something (like pseudo code):

    symbol = QgsFillSymbolV2() 
    symbol.setColor(fcolor)
    symbol.setFillColor(fcolor)
    symbol.setBorderColor(bcolor)
    symbol.setBorderWidth(width)
    symbol.setBrushStyle(bstyle)

    ...more symbols 

    symbolLayer = QgsSymbolLayerV2()
    symbolLayer.add(symbol)

    ... more symbol layers

    renderer = QgsSingleSymbolRendererV2() 
    renderer.addlayer(symbolLayer)

    ...

ADDITIONAL INFO:

# ------------------------------------------------
# Rendern der Windparks
# ------------------------------------------------
lyrPark.setRendererV2(tk.makeFillRenderer(0.02, QColor('#bbbbbb'), QColor('#444444')))


# -----------------------------------------------------
# Draw definition bathymetry 
# -----------------------------------------------------
def makeBathyRenderer(field):
    levelList = []
    levelList.append(tk.makeLevelFill('Land','Land', 0.2 ,QColor('#ffee94'),QColor('#ffee94')))
    levelList.append(tk.makeLevelFill( '0-10',  ' 0 - 10 m', 0.2 ,QColor('#f8fcff'),-1))
    levelList.append(tk.makeLevelFill( '10-20', '10 - 20 m', 0.2 ,QColor('#d8ecff'),-1))
    levelList.append(tk.makeLevelFill( '20-30', '20 - 30 m', 0.2 ,QColor('#c8deff'),-1))
    levelList.append(tk.makeLevelFill( '30-40', '30 - 40 m', 0.2 ,QColor('#b8ceff'),-1))
    levelList.append(tk.makeLevelFill( '40-50', '40 - 50 m', 0.2 ,QColor('#a8beff'),-1))
    levelList.append(tk.makeLevelFill( '50-60', ' > 50   m', 0.2 ,QColor('#98aeff'),-1))
    renderer = QgsCategorizedSymbolRendererV2( field, levelList )
    return renderer

rdrBathy = makeBathyRenderer('level')
lyrBathy.setRendererV2(rdrBathy)

# -----------------------------------------------------
# Draw definitio iso-lines
# -----------------------------------------------------
def makeIsoRenderer(field):
    rangeList = []
    rangeList.append(tk.makeRangeLine( -10.1,  -9.9, '10 m', 0.35 ,QColor('#0000ff'), Qt.SolidLine))
    rangeList.append(tk.makeRangeLine( -15.1, -14.9, '15 m', 0.1 , QColor('#0000ff'), Qt.SolidLine))
    rangeList.append(tk.makeRangeLine( -20.1, -19.9, '20 m', 0.35 ,QColor('#0000ff'), Qt.DashLine))
    rangeList.append(tk.makeRangeLine( -24.1, -23.9, '25 m', 0.1 , QColor('#0000ff'), Qt.SolidLine))
    rangeList.append(tk.makeRangeLine( -30.1, -29.9, '30 m', 0.35 ,QColor('#0000ff'), Qt.DashDotLine))
    rangeList.append(tk.makeRangeLine( -34.1, -33.9, '35 m', 0.1 , QColor('#0000ff'), Qt.SolidLine))
    rangeList.append(tk.makeRangeLine( -40.1, -39.1, '40 m', 0.35 ,QColor('#0000ff'), Qt.DotLine))
    rangeList.append(tk.makeRangeLine( -44.1, -43.9, '45 m', 0.1 , QColor('#0000ff'), Qt.SolidLine))
    renderer = QgsGraduatedSymbolRendererV2( field, rangeList )
    renderer.setMode( QgsGraduatedSymbolRendererV2.Custom )
    return renderer

rdrIso = makeIsoRenderer('height')
lyrIso.setRendererV2(rdrIso)

# ---------------------------------------------------
# Draw definition roi
# ---------------------------------------------------
symRoi = QgsSimpleLineSymbolLayerV2()
symRoi.setColor(QColor('#dd4433'))
symRoi.setWidth(1)
lyrRoi.rendererV2().symbols()[0].changeSymbolLayer(0, symRoi)

# ---------------------------------------------------
# Draw defnition AWZ
# ---------------------------------------------------
symAWZ = QgsSimpleLineSymbolLayerV2()
symAWZ.setColor(QColor('#dd44dd'))
lyrAWZ.rendererV2().symbols()[0].changeSymbolLayer(0, symAWZ)
  • Do you just want to add a new symbol layer without having to go through the rendering process? – Joseph Sep 22 '17 at 11:04
  • @Joseph The subroutines working inside a "living" map rendering context. Please look at the additional infos. Im truggeling at the moment with the embedding structures inside the renderer. – huckfinn Sep 22 '17 at 11:33
  • @Joseph sorry for beeing so.. ..frustrated – huckfinn Oct 16 '17 at 22:29
  • No need to apologise! Unfortunately, I'm not so sure about your question. Does it not work for you, does it crash, or are you looking to refine your code? You could consider placing a bounty to attract potential answerers :) – Joseph Oct 17 '17 at 15:02
  • @Joseph ..at least I'm looking for a straight forward way to assemble and setup a renderer. The configuration is strange structured, allocation works implicid and is very difficult to handle. If you leave the sketched path it crashes some times. The howto is poor documented, oneway and I often have to go back to the C++ code or adopt examples from the web and I'm not sure that I scratch out the right things. a bounty is a good idea. – huckfinn Oct 22 '17 at 21:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.