3

From some other posts I found that i can determine a layers visiblity by something like

#assume lyr_name the layers name to determine visibity for
l = QgsMapLayerRegistry.instance().mapLayersByName(lyr_name)[0]
vis = QgsProject.instance().layerTreeRoot().findLayer(l.id()).isVisible()

Apart from this being put in a method like

def isVis(lyr_name):
    l = QgsMapLayerRegistry.instance().mapLayersByName(lyr_name)[0]
    return QgsProject.instance().layerTreeRoot().findLayer(l.id()).isVisible()

Is this the recommendend way to check if a layer is visible, whichs name is known or is there a more simpler approach?

  • 1
    Could you please provide some context as to what you are trying to achieve? :) – Joseph Jan 19 '18 at 12:07
2

You are right. That's the way to check the layer visibility in QGIS 3.x, provided by QgsLayerTreeNode.

You can make it more easy to read, if you assign variables, like:

project = QgsProject.instance()
root = project.layerTreeRoot()
layer = iface.activeLayer()
root.findLayer(layer.id()).isVisible();

There are other methods related to visibility in QgsLayerTreeNode. This isVisible() also checks if some group above is unchecked.

In the following case, the isVisible() applied to House numbers layer will be false, although the layer is checked.

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.