1

I have some Sentinel derived rasters (time series) divided in tiles. In my QGIS (3.16) each tile is represented by a group. Each group contains rasters from different dates and according layernames. The layername is equal to the filename (because of drag&drop the source files into QGIS). The structure in my Win-Explorer is analogous: All rasters which belong to the same tile are placed in one folder.

My issue is now that there are rasters with the same timestamp for different tiles, and their filenames and accordingly layernames are identical. That means, that in each group you will find layers with the same layername as in other groups. This causes problems in the further workflow for me. For other reasons I cannot alter the source file name, because this would result in other issues.

So, I'd like to rename all layers within a group by adding the group name to each layer as a prefix with an additonal "_" (with not changing the source filename).

Here an example of the current situation (group, layername, source foldername/filename). The only difference is between the groupnames and the source foldernames:

Tile0101 # =Group
- Sent_Date1   <-- source file name: ../Tile0101/Sent_Date1.tif
- Sent_Date2   <-- source file name: ../Tile0101/Sent_Date2.tif
- Sent_Date3   <-- source file name: ../Tile0101/Sent_Date3.tif
Tile0102
- Sent_Date1   <-- source file name: ../Tile0102/Sent_Date1.tif
- Sent_Date2   <-- source file name: ../Tile0102/Sent_Date2.tif
- Sent_Date3   <-- source file name: ../Tile0102/Sent_Date3.tif

Intended should-be-structure after renaming:

Tile0101
- Tile0101_Sent_Date1   <-- source file name: ../Tile0101/Sent_Date1.tif
- Tile0101_Sent_Date2   <-- source file name: ../Tile0101/Sent_Date2.tif
- Tile0101_Sent_Date3   <-- source file name: ../Tile0101/Sent_Date3.tif
Tile0102
- Tile0102_Sent_Date1   <-- source file name: ../Tile0102/Sent_Date1.tif
- Tile0102_Sent_Date2   <-- source file name: ../Tile0102/Sent_Date2.tif
- Tile0102_Sent_Date3   <-- source file name: ../Tile0102/Sent_Date3.tif

I guess this can be only done by Python, and I have nearly no knowledge with Python.

Could anybody show me how to rename the layers by using Python, (or by an according plugin, if there is)?


Referring to Ben's first answer: If possible a script should take into account the following aspects which I had not reflected in advance:

  • The concerned groups are subgroups at the second level of the TOC. (Bens script works fine, but only for groups at the first TOC level.)
  • As there will come additional/new layers to each group with time, once renamed layers should not get a double, triple ... prefix, if running the script again in order to rename the new layers. So, would it be possible to implement kind of a barrier which prevents from adding the prefix if it's already assigned?
  • The group names in the given example were just fictitious (This way, I hoped it would be easier to explain). My actual ones are "T00_00" upto "T15_20" (T=Tile, the first two digits represent the tile position in an x-direction, the last to digits the y-direction). May it be possible to adress the groupname not by a simple string, but rather to say: look for a string consisting first out of "T", then followed by two digits, then a "_" and again two digits?

2) Would there additionally be an option to name the layers accordingly during the loading process (not after having loaded the rasters to QGIS), and how would that work?

2
  • 1
    Questions with bullet lists of questions are likely to be closed for violating the One question per Question principle.
    – Vince
    Aug 4, 2021 at 10:47
  • 2
    @jaysigg, please see my answer for your first question. As pointed out by Vince you would be advised to remove the second part. You can ask it as new question if you still want to pursue that approach.
    – Ben W
    Aug 4, 2021 at 12:08

1 Answer 1

3

You can try the script below in the Python console. This will rename every layer in each sub-group in your layer tree only if the group name contains a string matching a capital T followed by 2 digits followed by an underscore followed by 2 more digits e.g. 'T09_15'. Already renamed layers inside groups will be skipped. Note: the names will only be changed in the layer tree panel- the source file names will not be altered.

Following comments below, I have rewritten the script to use regex to look for the substrings in the sub-group and layer names.

import re

root = QgsProject.instance().layerTreeRoot()

for group in root.findGroups():
    for subgroup in group.findGroups():
        # look for sub-groups names matching 'Tdigitdigit_digitdigit'
        subgroup_name_found = re.search('[T]\d{2}[_]\d{2}', subgroup.name())
        if subgroup_name_found:
            for layer in subgroup.findLayers():
                # look for layers which have already been renamed
                layer_name_found = re.search('[T]\d{2}[_]\d{2}', layer.name())
                if not layer_name_found:
                    # only rename layers which don't already contain 'T01_01' etc.
                    layer.setName('{}_{}'.format(subgroup.name(), layer.name()))
8
  • Thanks @Ben, works principally fine. But as so often: new solutions, new questions, or new issues which I had not reflected in advance. In this case concerning the TOC-Level of my groups, "multiple prefexing" and with the groupname string. I amended my question accordingly. May you (or anyone else) have a look?
    – jaysigg
    Aug 4, 2021 at 16:03
  • 1
    @jaysigg, I have edited my answer. In future it is really important to provide complete and accurate information in your question. An image of your actual layer tree structure showing the real names and exactly what nodes you wanted to rename would have helped a lot.
    – Ben W
    Aug 5, 2021 at 3:31
  • _ @Ben, I tried your code, but unfortunatly doesn't work. 1) I am not absolutely sure about your intention, but I guess you want to split the subgroupname, e.g. 'T09_13' into 'T' and '09' and '13'. But listing 'sg_prefix will return: list(sg_prefix) ['T', '0', '9']. So, I tried around: Only using split('_') instead of split('_')[0] will of course result in list(sg_prefix) ['T09', '13']. But I am not able to combine it the right way to get list(sg_prefix) ['T', '09', '13']. May you ...?
    – jaysigg
    Aug 8, 2021 at 6:39
  • _ @Ben And afte trying around I guess: If the splitting issue of my last comment would be solved, there would probably come up two further issues: 1) As my TOC contains other subgroups which not contain a '_' the code will stop as soon as reaching those soubgroups (--> 'index out of range' error, as no second split part is available). 2) Splitting the layer name could also result in returning TRUE for 'isdigit', if the second or third substring is a digit of any lenght (as a date for example would be). So, how would it be possible to look only for substrings with a length of two digits?
    – jaysigg
    Aug 8, 2021 at 7:01
  • 1
    @jaysigg, please see my edited answer. I have re-written the script to use regex for string matching.
    – Ben W
    Aug 8, 2021 at 8:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.