My QGIS ToC contains layer groups and some layers that do not belong to any group.
I would like to create a text file containing the name of those layers that do not belong to any group.
Is there an automated way to do it?
1 Answer 1
You can do that from the QGIS Python console. Follow this workflow:
Open the QGIS Python console
Adjust the following line to match the path of your text file and copy the whole line to the QGIS Python console:
textFilePath = '/tmp/non_grouped_layers.txt'
As you can see, in the example above, I want to create a text file named 'non_grouped_layers.txt' in the folder
/tmp/
, I'm on a GNU/Linux machine.Press Enter.
Copy the following code snippet to your QGIS Python console and, after that, press Enter a couple of times:
# Get a reference of the layer tree root = QgsProject.instance().layerTreeRoot() tree = root.children() # Get names of non-grouped layers nonGrouped = [] for node in tree: if isinstance( node, QgsLayerTreeLayer ): nonGrouped.append( node.layerName() ) #Save the nonGrouped list as text file f = open( textFilePath, 'wt') f.write( '\n'.join( nonGrouped ) ) f.close()
You should now have your text file with the list of non-grouped layers.
In the screenshot you can see my QGIS ToC arrangement and the text file I get from running the code snippet.
enter image description here
If you face troubles, tell me.
-
On trying this in QGIS3 console I get the following error -is there some difference in running this line? f = open( textFilePath, 'wt') Traceback (most recent call last): File "C:\OSGEO4~1\apps\Python37\lib\codeop.py", line 133, in call codeob = compile(source, filename, symbol, self.flags, 1) File "<input>", line 4 f = open( textFilePath, 'wt') ^GeorgeC– GeorgeC2019年02月16日 12:05:09 +00:00Commented Feb 16, 2019 at 12:05
-
2
node.layerName()
has changed in QGIS 3, it should be nownode.name()
. Other than that, everything should work. Just tested it and it runs smoothly.Germán Carrillo– Germán Carrillo2019年02月16日 13:44:52 +00:00Commented Feb 16, 2019 at 13:44 -
Having this little tool as a plugin would be very useful.user2955884– user29558842024年10月11日 06:56:08 +00:00Commented Oct 11, 2024 at 6:56
-
...and having the list structured by groups.user2955884– user29558842024年10月11日 07:06:30 +00:00Commented Oct 11, 2024 at 7:06
Explore related questions
See similar questions with these tags.
txt
file to contain?