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()
.
-
Perhaps this post might help: Accessing layer's visibility presets?– JosephCommented Dec 5, 2017 at 10:56
Add a comment
|
2 Answers
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
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