Skip to main content
added extra explanations
Source Link
Kadir Şahbaz
  • 78k
  • 57
  • 257
  • 404

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())

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().values():

for  layer in layers:
     if layer.name()=='layer_name':
         ...

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.items():
    print(layer.name())

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:
    print(layer.name())

If you prefer, you can iterate in this way:

layers = QgsProject.instance().mapLayers() # dictionary

for layer in layers.values():
    print(layer.name())

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().values():

for layer_name, layer in layers.items():
     ..if layer.name()=='layer_name':
         ...

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()

for layer_name, layer in layers.items():
     ...
     ...

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().values():

for  layer in layers:
     if layer.name()=='layer_name':
         ...
Source Link
Kadir Şahbaz
  • 78k
  • 57
  • 257
  • 404

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()

for layer_name, layer in layers.items():
     ...
     ...