1

I try to swich off remove layers from a legend in a qgis standalone application (QGIS 2.18) and found that the one way is to manipulate the QgsLayerTreeGroup (GIS.SE 142016).

# remove two unwanted layers from the legend set
mapset.remove(lyrBathy.id())
mapset.remove(lyrIso.id())

# create the legend
cleg = tk.createLegend( page, mapset, LEG_LEFT, LEG_TOP, 'Legend')

If I render the stuff at least the program crashes:

To be sure that the map is not influenced I make a copy of the current tree only the expected layers:

# ----------------------------------------------
# Remove layers not in list by copy on write
# ----------------------------------------------
def updateLegendLayerTree(srcTree, layers):
    print '---- BEFORE -----' 
    print srcTree.dump()

    print  '--- CLEAN UP ----'
    for child in srcTree.children():
        if child.layer().id() in layers:
            result.addChildNode(child)
            print "..added", child.layer().name()
        else:    
            print "..removed", child.layer().name()
    print '---- AFTER -----' 
    print result.dump()
    return(result)

Create the legend with the filtered tree:

# ----------------------------------------------
# Create the legend
# ----------------------------------------------
def createLegend (page, layers,  x, y, title, \
                  titleFont = QFont('Arial',12,2), \
                  groupFont = QFont('Arial',11,1), 
                  itemFont  = QFont('Arial',10,1) ):
    #print "Erstelle Legende: %s %d %d " % (title, x, y)
    cleg = QgsComposerLegend(page)
    cleg.setTitle(title)
    cleg.setAutoUpdateModel(False) 
    cleg.rstyle(QgsComposerLegendStyle.Symbol).setMargin(QgsComposerLegendStyle.Top, 1)
    cleg.setStyleFont(QgsComposerLegendStyle.Title, titleFont)
    cleg.setStyleFont(QgsComposerLegendStyle.Subgroup, groupFont)
    cleg.setStyleFont(QgsComposerLegendStyle.SymbolLabel, itemFont)
    cleg.setX(x)
    cleg.setY(y)
    # Update the list 
    srcRoot = QgsProject.instance().layerTreeRoot()
    newTree = updateLegendLayerTree(srcRoot, layers)
    # Set the new tree
    cleg.modelV2().setRootGroup(newTree)
    return cleg

I got this debug messages and the fatal error:

---- BEFORE -----
GROUP:  visible=2 expanded=1
  LAYER: Wassertiefe visible=2 expanded=1 id=Wassertiefe20170922114344525
  LAYER: Schutzgebiete visible=2 expanded=1 id=Schutzgebiete20170922114344676
  LAYER: AWZ visible=2 expanded=1 id=AWZ20170922114344696
  LAYER: Windpark visible=2 expanded=1 id=Windpark20170922114344504
  LAYER: Tiefenlinie visible=2 expanded=1 id=Tiefenlinie20170922114344550
  LAYER: Eisente visible=2 expanded=1 id=Eisente20170922114344818

--- CLEAN UP ----
..removed Wassertiefe
..added Schutzgebiete
..added AWZ
..added Windpark
..removed Tiefenlinie
..added Eisente

---- AFTER -----
GROUP: Legend visible=2 expanded=1
  LAYER: Schutzgebiete visible=2 expanded=1 id=Schutzgebiete20170922114344676
  LAYER: AWZ visible=2 expanded=1 id=AWZ20170922114344696
  LAYER: Windpark visible=2 expanded=1 id=Windpark20170922114344504
  LAYER: Eisente visible=2 expanded=1 id=Eisente20170922114344818

...write PDF: clc.2013-01-01.2017-01-01.cla.map-points.pdf
Speicherzugriffsfehler # (german... memory access error)

I assume the legend render tries to get things from dependenies which are not covered by the model, created in:

   def updateLegendLayerTree(srcTree, layers):
         .....
         result  = QgsLayerTreeGroup('Legend', True).

Any ideas what is the right statement? I tried to manipulate the visibility as well with negative results.

0

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.