TransWikia.com

What is the second argument in QgsProcessingUtils.mapLayerFromString()?

Geographic Information Systems Asked by Francisco Bonnefoy on February 28, 2021

I am new to Qgis3. I am trying to get a layer in the Python console. And I have to put to arguments in the function QgsProcessingUtils.mapLayerFromString(). The first argument is self-explanatory, the string. But I don’t understand what the second argument is (the QgsProject).

Could someone clarify me what that second argument is?

One Answer

There are several ways to access the layers. As @christoph tells you, it is easier and more direct to use the methods of QgsProject.

We start by instantiating pry=QgsProject.instance()

1.- To access a layer by its name you can use

pry.mapLayersByName()

It returns a list, because you can have several layers with the same name. If only one layer matches, you can access it by its index:

pry.mapLayersByName("transport")[0]

2.- From QGIS3.10 you also have the mapLayersByShortName() method

3.- If you want a list with all the layers:

pry.mapLayers()

Returns a dictionary, the layer id is the key

On the other hand, you can combine with the methods of iface (QgisInterface) and QgsMapCanvas

1.- Get the active layer**:

  • iface.activeLayer() returns the active layer
  • iface.mapCanvas().currentLayer() generates the same result

2.- A list of the layers visible on the canvas

iface.mapCanvas().layers()

Answered by Luis Perez on February 28, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP