Tuesday, 13 September 2016

Getting list of layer names using PyQGIS?


I need a list containing the names of all the layers in a QGIS session. I did the task as


layersNames = []
for i in self.iface.mapCanvas().layers():
layersNames.append(str(i.name()))

but this has the problem that only the names for the visible layers are extracted. How can I get a list with the names of all (visible or not) layers using PyQGIS?



Answer




QgsMapLayerRegistry.instance().mapLayers() will give you all layers that are opened.


If you want the names then:


names = [layer.name() for layer in QgsMapLayerRegistry.instance().mapLayers().values()]

names will be a list of layer names


or using a normal function:


for layer in QgsMapLayerRegistry.instance().mapLayers().values():
print layer.name()

No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...