2

I am trying to switch visibility preset using python but I cannot find any relevant method in the documentation . Does anyone knows how I can do it? The VisibilityPresetCollection class is accessible using the following code QgsProject.instance().visibilityPresetCollection().enter image description here

1

2 Answers 2

2
def to_print():
    layers= iface.mapCanvas().layers()
    for layer in layers:
        iface.legendInterface().setLayerVisible(layer, False)

    layer_list = QgsProject.instance().visibilityPresetCollection().presetVisibleLayers(u'to_print')
    for layer in layer_list:
        iface.legendInterface().setLayerVisible(QgsMapLayerRegistry.instance().mapLayer(layer), True)



def to_work():
    layers= iface.mapCanvas().layers()
    for layer in layers:
        iface.legendInterface().setLayerVisible(layer, False)

    layer_list = QgsProject.instance().visibilityPresetCollection().presetVisibleLayers(u'to_work')
    for layer in layer_list:
        iface.legendInterface().setLayerVisible(QgsMapLayerRegistry.instance().mapLayer(layer), True)

Here you go! i think this will work for you.

At first you hide every layer, then extract the layers of the selected preset and set them to visible one-by-one though the loop

0

Please view my answer here: https://gis.stackexchange.com/a/255304/44921

Usage:

>>> visibility_presets()
['Test-01']
>>> activate_visibility_preset('Test-01')
True
>>> active_visibility_preset()
'Test-01'

Tested on QGIS 3.10.14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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