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?