TransWikia.com

Remove LayoutItemLegend entry PyQGIS

Geographic Information Systems Asked by Mathys WOHL on June 1, 2021

I work on QGIS Plugin in python. This plugin load several Print layout in a QGIS project. These print layout contain a legend. I would like to be able to remove a layer from the composer legend without this layer being deleted in the main map. For that I found the code of Ben W on GIS Stack Exchange Hide legend entries in PyQGIS and it works very well. But in my case the layers are organized in groups like in the photo below. When I use groups the code doesn’t work anymore. There is no error but the layer is not deleted from the legend. How could I arrange this code to work with layers that are in groups ?

Code of Ben W that I tried:

project = QgsProject().instance()
layer_to_remove = project.mapLayersByName('Your Layer Name')[0]
layout = project.layoutManager().layoutByName('Your Layout Name')
legend = [i for i in layout.items() if isinstance(i, QgsLayoutItemLegend)][0]
legend.setAutoUpdateModel(False)
legend.model().rootGroup().removeLayer(layer_to_remove)
legend.adjustBoxSize()
layout.refresh()

Organisation of my layers:

enter image description here

2 Answers

There is only a small modification to be made to the code. You just need to get a reference to your legend group and remove the layer from the QgsLayerTreeGroup rather than the root group which is a QgsLayerTree object.

Please try the example below. I have used a group and layer name from your screenshot but you will need to change the layout name to match yours (I don't know what your layout is called!)

project = QgsProject().instance()

layout_name = 'Layout 1'#Change to match the name of your layout
group_name = 'Limites administratives'#Name of a group in your legend
layer_name = 'Limites régionnales'#Name of the layer you want to remove


layer_to_remove = project.mapLayersByName(layer_name)[0]
layout = project.layoutManager().layoutByName(layout_name)
legend = [i for i in layout.items() if isinstance(i, QgsLayoutItemLegend)][0]
root_group = legend.model().rootGroup()#QgsLayerTree object
group = root_group.findGroup(group_name)#get reference to group (QgsLayerTreeGroup object)
legend.setAutoUpdateModel(False)
group.removeLayer(layer_to_remove)#remove layer from group
legend.adjustBoxSize()
layout.refresh()

Correct answer by Ben W on June 1, 2021

Thank you Ben this code work ! To my side I tried this code and It work also

project = QgsProject().instance()
layout = project.layoutManager().layoutByName('Your Layout Name')
legend = [i for i in layout.items() if isinstance(i, QgsLayoutItemLegend)][0]
legend.setAutoUpdateModel(False)
root_admin = legend.model().rootGroup().findGroup('Limites administratives')
root_admin.removeLayer(dep_layer)
legend.adjustBoxSize()
layout.refresh() 

Answered by Mathys WOHL on June 1, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP