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?
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 layeriface.mapCanvas().currentLayer()
generates the same result2.- A list of the layers visible on the canvas
iface.mapCanvas().layers()
Answered by Luis Perez on February 28, 2021
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP