For QGIS 3, QgsMapLayerRegistry
's functionality has been moved to QgsProject
.
So, for iterating over map layers, you should use that structure:
layers = QgsProject.instance().mapLayers()
.mapLayers()
returns a dictionary structured as {layer_x_id: layer_x, layer_y_id: layer_y, ....}
. Then you can iterate over layers like:
for layer_id, layer in layers.valuesitems():
print(layer.name())
for
Because .mapLayers()
returns a dictionary (dict
), you can also use .values()
method of dictionary which returns just a list
which contains layers in that case. Then you can iterate over layers like:
layers = QgsProject.instance().mapLayers().values()
for layer in layers:
if print(layer.name()=='layer_name':)
If you prefer, you can iterate in this way:
layers = QgsProject.instance().mapLayers() # dictionary
for layer in layers.values():
.. print(layer.name())