2

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.

I found the following Python-script:

from collections import OrderedDict
root = QgsProject.instance().layerTreeRoot()
LayerNamesEnumDict=lambda listCh:{listCh[q[0]].name()+str(q[0]):q[1]
                                   for q in enumerate(listCh)}

mLNED = LayerNamesEnumDict(root.children())
mLNEDkeys = OrderedDict(sorted(LayerNamesEnumDict(root.children()).items())).keys()

mLNEDsorted = [mLNED[k].clone() for k in mLNEDkeys]
root.insertChildNodes(0,mLNEDsorted)
for n in mLNED.values():
    root.removeChildNode(n)

The script works principally fine, but it adresses only the top level of the TOC, what is not suitable in my case.

Does anybody has an idea how to modify this script for matching my purposes?

I just have some basic knowledge in R, but not in Python, unfortunately.

My conditions in detail:

  1. The top level layers should NOT be sorted, but remain unchanged.
  2. Exclusively the layers of one specific group should be sorted alphabetically, in case of the example only 'group1' (see example image below)
  3. The layers of other groups ('group2' in the example) should NOT be sorted and also remain unchanged.

The example is made with a couple of temporary layers. Actually I have to handle a satellite time series with a few hundred multi band raster images, which should be sorted by date. By the way, I'm aware of the loadthemall-tool. But this would not be useful to me, because I have to add further images with time.

Bild 1

enter image description here

2
3

Nice try, you're almost there!

You just need to use your group instead of the root for the ordering, like this:

from collections import OrderedDict

root = QgsProject.instance().layerTreeRoot()
group = root.findGroup("group1")  # We are interested in group1
reverse_order = False

LayerNamesEnumDict=lambda listCh:{listCh[q[0]].name()+str(q[0]):q[1]
                                   for q in enumerate(listCh)}
    
# group instead of root
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)  # group instead of root
for n in mLNED.values():
    group.removeChildNode(n)  # group instead of root

That's it!

enter image description here


Note: To reverse the order just set the reverse_order variable to True.

5
  • Wow, this is exactly what I intended to. thanks al lot. But there was one little problem left. After running the script group1 consists of a series of the correctly sorted layers, but then, in contrary to your illustration, followed by series of all the layers in the former order. I assumed that the last line of the script (group.removeChildNode(n)) will remove them. In your case it seems to work accordingly, but not with mine. Any idea why? Or what to do? (As I cannot post images in comments I edited my question, and added a result image, please see there)
    – jaysigg
    May 27 at 10:30
  • To be perfect the script should result in the reverse order: C, B, A instead of A, B, C. (As I said I'd like to sort raster images by date, and would like to have the latest one at top.) That would be fine!
    – jaysigg
    May 27 at 10:55
  • Meanwhile, just the issue with 'removing-the-old-layer-set-automatically' is left. All others are solved. Thanks again, @German! Thus I decided to post the 'deletion-issue' as separate question --> see gis.stackexchange.com/questions/398099/…
    – jaysigg
    May 29 at 13:38
  • I've been unable to replicate your problem. May 29 at 13:46
  • If yor refer to the deletion-issue, this is what I feared: It works in your environment, but - for what reason ever - not in mine. So, I'm just wondering. I have no doubt about your code!
    – jaysigg
    May 29 at 13:54

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.