3

I have loaded multiples duplicated layers in QGIS with different filters. I wonder if it is possible to find the layers that haven't any filter (setSubsetString method) with PyQGIS?

2 Answers 2

3

PyQGIS 2

Loop over all registered layers and check if subsetString is empty.

layers = QgsMapLayerRegistry.instance().mapLayers()

for name, layer in layers.items():
    if not layer.subsetString():
        print 'layer %s has empty subsetString'% name
3

PyQGIS 3

Adopted version of @Detlev's answer:

from qgis.core import QgsProject, QgsVectorLayer

layers = QgsProject.instance().mapLayers()

for layer in layers.values():
    if isinstance(layer, QgsVectorLayer):
        if not layer.subsetString():
            print(f"Layer '{layer.sourceName()}' has empty subsetString")

or

from qgis.core import QgsProject, QgsVectorLayer

layers_not_filtered = [
    lyr for lyr in QgsProject.instance().mapLayers()
    if isinstance(lyr, QgsVectorLayer) and
    not lyr.subsetString()
]

fmt_print = lambda lyr: print(f"Layer '{lyr.name()}' has empty subsetString")
_ = list(map(fmt_print, layers_not_filtered))
2
  • @J.Monticolo, thank you for your magic (even if it is a bit longer ^_^)
    – Taras
    Dec 13, 2022 at 10:24
  • I disagree, 4 lines. And the goal is to get a list of not filtered layers, achieved in one line instead of 5. Dec 13, 2022 at 10:34

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.