Tell me please how to get the layer by name
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It only takes a minute to sign up.Sign up to join this community
Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
I get layer by name in that way in QGIS3:
layer_list = QgsProject.instance().layerTreeRoot().children() # returns QgsLayerTreeNode object list layer = [lyr.layer() for lyr in layer_list if lyr.name()=="LAYER_NAME"] print(layer) # OUT: <qgis._core.QgsVectorLayer object at ...>
I don't know if there is a shorter way. Maybe.
But in that way, you can get first layer named "LAYER_NAME".
children() function doesn't return layers in a group layer.
EDIT: the shortest way of getting layer by name:
layers = QgsProject.instance().mapLayersByName('LAYER_NAME') # list of layers named LAYER_NAME layer = layers # first layer named LAYER_NAME.
If you are sure there is one "LAYER_NAME" then:
layer = QgsProject.instance().mapLayersByName('LAYER_NAME')
Retrieve a list of matching registered layers by layer name.