Geographic Information Systems Asked on August 6, 2021
I’d like to reorder the layers within ONE specific group of the TOC alphabetically by layer name, but without reordering the top level layers or the layers of other groups.
@GérmanCarillo helped me to solve this (see link below).
I’m now struggling with removing old layers automatically (I just have some basic knowledge in R, but none in Python):
In the sample of Gérman Carillo the old layer structure is automatically removed, and only the layers in the intended order remain (see animation on Sorting layers by name in one specific group of QGIS layer tree). This is exactly the result I’d like to have but if I run the script in my QGIS (3.16), always both remain, one set of all layers in the origin order and another set in the new/intended order (see image below).
Does anybody have an idea what’s going wrong here or know how to modify the script to delete the former layer set?
This is the current code:
from collections import OrderedDict
root = QgsProject.instance().layerTreeRoot()
group = root.findGroup("group1") # name of the to-be-sorted-group
reverse_order = False
LayerNamesEnumDict=lambda listCh:{listCh[q[0]].name()+str(q[0]):q[1]
for q in enumerate(listCh)}
mLNED = LayerNamesEnumDict(group.children())
mLNEDkeys = OrderedDict(sorted(LayerNamesEnumDict(group.children()).items(), reverse=reverse_order)).keys()
mLNEDsorted = [mLNED[k].clone() for k in mLNEDkeys]
group.insertChildNodes(0,mLNEDsorted)
for n in mLNED.values():
group.removeChildNode(n)
This is the image of the result after running the script
Get help from others!
Recent Questions
Recent Answers
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP